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
Function DoSomething(someStuff as String) as Integer
'Do Stuff
Return 42
End Function
or:
Function DoSomething(someStuff as String) as Integer
'Do Stuff
DoSomething = 42
End Function
where it lets you use the function name as a variable of the same type as your explicit named variable that will be returned.
no subject
no subject
no subject
no subject
no subject
no subject
no subject
Ah. Yes. It's silly because if you set $return halfway down your function, you can't tell at the end that you are returning something. Potentially hard to work out what is going on.
I'm not sure what you mean by an unnamed variable being set though... maybe that's a C# thing?
no subject
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
no subject
Now, having syntactic sugar where saying functionname = might be useful in some cases, although the fact that it looks and behaves like a normal variable, but has special behaviour based on the fact that it's called the same thing as the function, is something that would make me uncomfortable: you have to remember to update that variable name if you change the function name, or cut and paste some code from one function to another.
Also, if your function doesn't have a name (in the case of anonymous functions and/or closures), you can't do that. So you have to fall back to the far more obvious and readable return statement.
I think if you've got a number of different return values, that you'll decide according to various criteria, scattered through the code of the function, and you then finally return the value after having done all your processing, then it makes sense to declare a return variable before all of your ifs and switches etc. and then return it.
But I don't see why you'd want the overhead of doing that in all cases.
(I'm coming at this from a Perl, and occasionally Javascript, background, FWIW.)
no subject
no subject
As the calling function, you'll assign the result to a variable of your choice (or not assign it at all, if you can do that sort of thing and you only care whether the function returned e.g. a true value, or you'll pass the value to a switch statement).
no subject
no subject
no subject
Let's have another example:
void MyMethod(string input, out string output)
{
output = input + " was processed";
}
That's perfectly valid C# syntax - and works fine. No need for a return - all I have to do is make sure that I set "output" sometime before the end of the method.
What I'd like is for _all_ output variables (including the implicit one) to work that way.
no subject
As an extreme example, imagine that the function is called in the context
if (DoSomething(x)) {
... stuff
} else {
... other stuff
}
The compiler should never actually store "42" but will instead elide the else branch of the test.
no subject
no subject
One variant of this optimisation which is more common, though, is returning an object by value and assigning to a class constructed at that point:
Foo x = somefunction();
For some compilers the object will be constructed once, at the higher level, and there will be no temporary variable of that type in the returned data on the stack (which will be data required to initialize the object -- the difference obviously being that operations in the constructor are called only once).
no subject
For example (in Scheme), if we have a function:
Each time fib is called, the value returned is that of ult (set! returns an unspecified value).
If we wrote this in Common Lisp instead (where setq returns the value used to set the variable) we could write this instead:
Here, the value returned by fib is the value of the final setq, which is the value of tmp.
no subject
no subject
no subject
Languages with a return variable:
function monkeys() {
if (foobar) {
return = 42;
} else {
// ...
if (dagnabit) {
return = 13;
} else {
// ...
if (consarnit()) {
return = e^(-i*pi)
} else {
// ...
}
}
}
}
// fall out of function
}
Language with a return statement:
function monkeys() {
if (foobar) return 42;
// ...
if (dagnabit) return 13;
// ...
if (consarnit()) return = e^(-i*pi);
// ...
}
I find the latter preferable.
no subject
For nRow = 1 To nLastRow
If ws.Cells(nRow, nColumn).Address = "$A$7" Then
Debug.Print "Found cell. Exiting for loop."
Exit For
Else
Debug.Print ws.Cells(nRow, nColumn).Address
End If
Next
In some ways I like having the function name as the return variable, since the calling line will have something like
b = f(a) + 4;
and the code will have
int f(int a){
f = 3*a;
}
so mentally f is a function that returns an int *and* is an int. Of course in some languages you'll then have problems if you ask for the address of f (is it a pointer to the nominal variable, or a function pointer?)
And it gets even more fun with recursive calls!
I quite often have code like (vastly simplified)
string center(string astring) {
string mystring;
mystring = "";
if len(astring) > 0 then mystring = "<center>" + astring + "</center>";
return mystring;
}
no subject
no subject
no subject
In matlab you declare the name of your result variable in the function definition:
function outvar=func_name(in1, in2)
outvar = in1 + in2;
multiple results are returned like this:
function [out1 out2]=func2(in1, in2)
out1 = in+1;
out2 = in+2;
In Eiffel, you set the result of a function by assigning to a compiler-created variable called Result.
no subject
nope. not today. Brain is goo, again.
no subject