andrewducker: (Default)
andrewducker ([personal profile] 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.

[identity profile] momentsmusicaux.livejournal.com 2010-02-11 03:54 pm (UTC)(link)
Why does that matter though?

[identity profile] skington.livejournal.com 2010-02-11 04:23 pm (UTC)(link)
You could be writing in a language which either doesn't need to declare variables for pretty much everything, or can return more than one variable.

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.

[identity profile] momentsmusicaux.livejournal.com 2010-02-11 05:12 pm (UTC)(link)
Oh that's because you're in a silly language that makes you declare variables!

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....

[identity profile] andrewhickey.livejournal.com 2010-02-11 06:01 pm (UTC)(link)
Yep, because

#:: ::-| ::-| .-. :||-:: 0-| .-| ::||-| .:|-. :||
open(Q,$0);while(){if(/^#(.*)$/){for(split('-',$1)){$q=0;for(split){s/\|
/:.:/xg;s/:/../g;$Q=$_?length:$_;$q+=$q?$Q:$Q*20;}print chr($q);}}}print"\n";
#.: ::||-| .||-| :|||-| ::||-| ||-:: :|||-| .:|

Makes *SO* much more sense... ;)


(I have to use Perl at work, and just did a Java module on my Master's course. A pox on both their houses - give me a *proper* language any day...)

[identity profile] momentsmusicaux.livejournal.com 2010-02-11 06:11 pm (UTC)(link)
Firstly -- the lines starting with # are just comments, surely... oh hang on, you're opening your own script. That's just silly. Second, of course it's illegible -- you haven't spaced it out at all! Third, use the idiom. Don't nest an if directly in a loop; use unless - next to first skip over things you don't care about. Use m// in list context to capture to named variables rather than rely on $1 if you want to use it beyond the immediate syntactic vicinity of the m// (or the s///). You can write crap in any language ;)

[identity profile] andrewhickey.livejournal.com 2010-02-11 07:16 pm (UTC)(link)
That's not *my* code - that was the winner of the 2000 Obfuscated Perl Contest ;)

But, sadly, it looks like some of the scripts I have to debug at work...

[identity profile] momentsmusicaux.livejournal.com 2010-02-11 07:36 pm (UTC)(link)
That'll be why it's crazy then!!! ;)

I agree, some HORRIBLE things can be written in Perl. Probably more so than in other languages. I still like it though :)

[identity profile] momentsmusicaux.livejournal.com 2010-02-11 10:27 pm (UTC)(link)
Yes, but it's called the same as its type! Arg! And you are having to repeat the type name -- that's a total violation of DRY, surely.

For that matter, why do you need a type as specific as a message list? Why is that not just an array?

[identity profile] momentsmusicaux.livejournal.com 2010-02-12 11:02 am (UTC)(link)
Ah, so it's an object rather than just a data type? I can see that would be useful.