return statement where it is usefull ?

U

Une bévue

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 ???
 
P

Patrick Gundlach

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
 
A

Alex Young

Yvon said:
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.
 
J

Julian 'Julik' Tarkhanov

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 =20
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 =20=

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

F. Senault

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 ?
 
D

Dave Baldwin

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.
=46rom the middle of a condition without falling through to the bottom =20=

of the method
To return more than one value

Dave.
 
D

dblack

Hi --

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

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS (reviewed on
Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
(e-mail address removed) => me
 
K

Kroeger, Simon (ext)

=20
[...]
=20
Now, I'd probably write it like this :
=20
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
 
M

Matthew Smillie

Le 20 juil. 06 =E0 13:16, Julian 'Julik' Tarkhanov a =E9crit :


thanks very much, much more rubyish ;-)

Well, as long as we're talking about Rubyish, then -1 is probably not =20=

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 =20
Ruby, while in the programmer's mind, -1 means failure. This sort of =20=

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 =20
you would actually perform the operation, but on an unexpected =20
object, which is potentially catastrophic. Tracking down answers to =20
"how did this data get corrupted?" is a vastly unrewarding task.

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

matthew smillie.=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,209
Messages
2,571,087
Members
47,684
Latest member
Rashi Yadav

Latest Threads

Top