Thread overview
2.0: Call self by recursion with 'self' or something like that
Jan 18, 2005
Russ Lewis
Jan 18, 2005
parabolis
Jan 19, 2005
Andy Friesen
Jan 19, 2005
Russ Lewis
January 18, 2005
I just had a case where I had a function that didn't need to be named, except that I wanted it to be recursive.  It would have been really nice if I could have called it from itself with a keyword like 'self':

uint function(uint) func =
  function uint(uint i) {
    if(i == 0)
      return 0;
    else
      return i+self(i-1);
  };

Of course, I could have written it as a for loop, but sometimes recursion is really the thing that is most readable.

I have this weird feeling that I might have mentioned this before...but I didn't find it with a search.

January 18, 2005
Russ Lewis wrote:
> Of course, I could have written it as a for loop, but sometimes recursion is really the thing that is most readable.
> 
> I have this weird feeling that I might have mentioned this before...but I didn't find it with a search.

rofl

Recursive people tend to have the feeling they have done something before.
January 19, 2005
Russ Lewis wrote:
> I just had a case where I had a function that didn't need to be named, except that I wanted it to be recursive.  It would have been really nice if I could have called it from itself with a keyword like 'self':
> 
> uint function(uint) func =
>   function uint(uint i) {
>     if(i == 0)
>       return 0;
>     else
>       return i+self(i-1);
>   };
> 
> Of course, I could have written it as a for loop, but sometimes recursion is really the thing that is most readable.
> 
> I have this weird feeling that I might have mentioned this before...but I didn't find it with a search.

Why not just give it a name?

 -- andy
January 19, 2005
Andy Friesen wrote:
> Why not just give it a name?

This is basically your question to both of my messages here.  I think it deserves a good answer. :)

I generally agree with the idea that declaring a function, and giving it a name, is good practice.  But the thing that that does is it tends to clutter up the "mindspace" of the reader.  The reader has to ask himself: is this function used in more than one place?  What sorts of arguments might it have?  Does the guy store function pointers to it anyhow?

Unnamed functions have the advantage of being quite obvious - they are only used once (unless they are stored in a function pointer, but then it's clear which function pointer they are).  Their purpose is (or, at least, can be in certain contexts) clear from the context, and you don't have to give it a long name to describe it.  (Yes, you can use short names, or even "Foo", but that doesn't help readability, IMHO.)

I know that unnamed functions also make the code harder to read, in certain situations.  I think it's a balance.  Either way has its downsides, and in this particular situation I thought that an unnamed function would be more readable.

That's just MHO, of course.  There's nothing that says that anybody else has to agree with me :)