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.
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
···
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
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 ?
···
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)
--
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)
On 7/20/06, Patrick Gundlach <rubyforum@contextgarden.net> wrote:
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.
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
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
Definitely (and even "self." can be eliminated) -- also, I would guess
that the return value is being tested for negativeness, in which case
maybe the test could be changed to test for nil, and then the method
could be:
def get_server_index(server)
servers.index(server)
end
at which point it would probably cease to be necessary.
David
···
On Thu, 20 Jul 2006, Julian 'Julik' Tarkhanov wrote:
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.
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.
···
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