andrewducker (
andrewducker) wrote2010-02-11 03:11 pm
Thinking about code
If I have a method:
int DoSomething(string someStuff)
{
//Do Stuff
return 42;
}
then effectively I have an unnamed variable that gets set by the return statement, yes?
That being the case, wouldn't it be in some ways clearer to have an explicit, named, variable that gets set instead?
int DoSomething(string someStuff)
{
//Do Stuff
returnValue = 42;
}
where "returnValue" is a keyword that's used to return the value.
As it is I frequently end up with code that creates (or sets) a variable at various points through the code just so it can be returned at the end. Making this an explicit part of the language just makes sense to me.
I assume there are languages out there that do this.
int DoSomething(string someStuff)
{
//Do Stuff
return 42;
}
then effectively I have an unnamed variable that gets set by the return statement, yes?
That being the case, wouldn't it be in some ways clearer to have an explicit, named, variable that gets set instead?
int DoSomething(string someStuff)
{
//Do Stuff
returnValue = 42;
}
where "returnValue" is a keyword that's used to return the value.
As it is I frequently end up with code that creates (or sets) a variable at various points through the code just so it can be returned at the end. Making this an explicit part of the language just makes sense to me.
I assume there are languages out there that do this.
no subject
no subject
If you have code like this:
Messages DoStuffAndReturnMessages()
{
Messages messages = new Messages();
//Stuff
messages.Add("Got here");
//More Stuff
messages.Add("And here");
//Yet more stuff
return messages;
}
then why make me create a "nessages" variables? We know we're going to need one (to return), why not have it exist automatically?
no subject
e.g. a fibonacci function in Perl.
{
my @fib = (0, 1, 1);
sub fib {
my ($num) = @_;
return if $num < 0 || $num != int($num);
return $fib[$num] if $fib[$num];
return fib[$num] = $fib[$num - 2] + $fib[$num -1];
}
}
No need for a return variable there.
no subject
no subject
I mean, "Messages messages = new Messages();" -- what the fuck is that meant to say? Gibberish!
I never got on with programming back when I was trying to do VB macros. Then I found perl....
no subject
#:: ::-| ::-| .-. :||-:: 0-| .-| ::||-| .:|-. :||
open(Q,$0);while(
no subject
no subject
But, sadly, it looks like some of the scripts I have to debug at work...
no subject
I agree, some HORRIBLE things can be written in Perl. Probably more so than in other languages. I still like it though :)
no subject
I can separate it out into:
MessageList myMessageList;
myMessageList = new MessageList();
Where the first line says that there is a variable called myMessageList of type MessageList, and the second line puts a new MessageList into it. But the combination is more compact.
As to not declaring variables - I'm not giving up the massive benefits of statically typed variables - including IntelliSense, refactoring support and that compiling the code tells me whether I've spelled anything wrong, or got my types confused.
no subject
For that matter, why do you need a type as specific as a message list? Why is that not just an array?
no subject
In C# 3 I can actually say:
var myErrorMessages = new MessageList();
and it will use type inference to work out what type it is at compile time.
no subject