[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

return statement where it is usefull ?

pere.noel

7/20/2006 8:36:00 AM

i've the habit (from java ;-)) to put a return statement in a method
like that :

def get_server_index(server)
if self.servers.include?(server)
return self.servers.index(server)
end
return -1
end

in that case, is it usefull ? (i think not)

where could be it usefull ?
never ???
--
une bévue
9 Answers

Patrick Gundlach

7/20/2006 8:58:00 AM

0

Hi,

> i've the habit (from java ;-)) to put a return statement in a method
> like that :
>
> def get_server_index(server)
> if self.servers.include?(server)
> return self.servers.index(server)
> end
> return -1
> end
>
> in that case, is it usefull ? (i think not)


I think that both cases are usefull: return indicates that you intention
to return something and you don't do it by accident. It's more for the
human who reads the code.

(my 0.02 â?¬)

Patrick

--
Posted via http://www.ruby-....

Alex Young

7/20/2006 11:00:00 AM

0

Yvon Thoraval wrote:
>
> Le 20 juil. 06 à 11:52, Joey a écrit :
>
>>
>> See, "here" never gets printed, if you didn't have the return keyword:
>> def m
>> [nil]
>> p "here"
>> end
>> m
>> "here" does get printed.
>
> and what's the returned value in that case ? nil, i guess...
It's the return value of the p method, which happens to be nil in this
case. In general, it's the value of the last expression in the method.

--
Alex

Julian 'Julik' Tarkhanov

7/20/2006 11:17:00 AM

0


On 20-jul-2006, at 10:40, Une bévue wrote:

> i've the habit (from java ;-)) to put a return statement in a method
> like that :
>
> def get_server_index(server)
> if self.servers.include?(server)
> return self.servers.index(server)
> end
> return -1
> end
>
> in that case, is it usefull ? (i think not)

Ruby implicitly returns from the last expression in the method, but
if it's inside a condition
it might not return. For instance:

def foo
case bar
when "x"
1
when "y"
2
else
3
end
end

In this case it will return 1, 2 or 3 implicitly.

As for your method, why not use a conditional assignment? Array#index
returns nil when the element is not in the
array, so you can get a nil and suppress it with -1.

def get_server_index(server)
self.servers.index(server) || -1
end


F. Senault

7/20/2006 11:23:00 AM

0

Le 20 juillet à 10:35, Une bévue a écrit :

> i've the habit (from java ;-)) to put a return statement in a method
> like that :
>
> def get_server_index(server)
> if self.servers.include?(server)
> return self.servers.index(server)
> end
> return -1
> end
>
> in that case, is it usefull ? (i think not)

Emmm, in this precise case, it is - if you don't put your -1 in a else
statement, without the return, you'll always get -1...

Now, I'd probably write it like this :

def get_server_index(server)
if self.servers.include?(server)
self.servers.index(server)
else
-1
end
end

Fred
Stating the obvious ?
--
Imagine a stegosaurus wearing rocket powered roller skates, & you'll
get a fair idea of its elegance, stability & ease of crash recovery.
(Lionel Lauer in the SDM)

Dave Baldwin

7/20/2006 11:23:00 AM

0


On 20 Jul 2006, at 09:40, Une bévue wrote:

> i've the habit (from java ;-)) to put a return statement in a method
> like that :
>
> def get_server_index(server)
> if self.servers.include?(server)
> return self.servers.index(server)
> end
> return -1
> end
>
> in that case, is it usefull ? (i think not)
>
> where could be it usefull ?
> never ???
>

As a documentation aid.
From the middle of a condition without falling through to the bottom
of the method
To return more than one value

Dave.



> --
> une bévue
>


dblack

7/20/2006 12:02:00 PM

0

Kroeger, Simon (ext)

7/20/2006 12:06:00 PM

0


> [...]
>
> Now, I'd probably write it like this :
>
> def get_server_index(server)
> if self.servers.include?(server)
> self.servers.index(server)
> else
> -1
> end
> end

This starts to be off topic, but

def get_server_index(server)
servers.fetch(server, -1)
end

should do the same. To say something more
on topic, return is a method call (or sort
of) and not using it (if you don't need)
is faster - or at least it was last time
I checked.

cheers

Simon

Matthew Smillie

7/20/2006 12:42:00 PM

0

On Jul 20, 2006, at 12:30, Yvon Thoraval wrote:

>
> Le 20 juil. 06 à 13:16, Julian 'Julik' Tarkhanov a écrit :
>
>> As for your method, why not use a conditional assignment?
>> Array#index returns nil when the element is not in the
>> array, so you can get a nil and suppress it with -1.
>>
>> def get_server_index(server)
>> self.servers.index(server) || -1
>> end
>
> thanks very much, much more rubyish ;-)

Well, as long as we're talking about Rubyish, then -1 is probably not
the best choice as an indicator of failure, nil would be preferred.

One of the reasons for this is that -1 is a valid array index in
Ruby, while in the programmer's mind, -1 means failure. This sort of
disagreement can lead to some very annoying errors:

servers[some_index].do_something_important

If some_index is nil, you get an immediate error. If it's -1, then
you would actually perform the operation, but on an unexpected
object, which is potentially catastrophic. Tracking down answers to
"how did this data get corrupted?" is a vastly unrewarding task.

Obviously, this may not be an issue in your particular code, but it
is something to watch out for.

matthew smillie.

Tim Hoolihan

7/20/2006 1:56:00 PM

0

No, it won't always return -1. A return statement returns control to
the caller. Try the following code:

def test
if 1==1
return 5
end
return 0
end

puts test


It will return 5.
-Tim

F. Senault wrote:
> Le 20 juillet à 10:35, Une bévue a écrit :
>
>> i've the habit (from java ;-)) to put a return statement in a method
>> like that :
>>
>> def get_server_index(server)
>> if self.servers.include?(server)
>> return self.servers.index(server)
>> end
>> return -1
>> end
>>
>> in that case, is it usefull ? (i think not)
>
> Emmm, in this precise case, it is - if you don't put your -1 in a else
> statement, without the return, you'll always get -1...
>
> Now, I'd probably write it like this :
>
> def get_server_index(server)
> if self.servers.include?(server)
> self.servers.index(server)
> else
> -1
> end
> end
>
> Fred
> Stating the obvious ?