andrewducker (
andrewducker) wrote2010-02-11 03:11 pm
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
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
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