Return statement where it is usefull?

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

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-forum.com/\.

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

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

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.

···

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

--
une bévue

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)

return stops execution of a method.

def m
  return [nil]
  p "here"
end
m

See, "here" never gets printed, if you didn't have the return keyword:
def m
  [nil]
  p "here"
end
m
"here" does get printed.

j`ey
http://www.eachmapinject.com

···

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.

(my 0.02 €)

Patrick

--
Posted via http://www.ruby-forum.com/\.

or a glass of your prefered liquid when u stop by Paris ? :wink:

···

Le 20 juil. 06 à 10:58, Patrick Gundlach a écrit :

(my 0.02 €)

thanks very much, much more rubyish :wink:

···

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

not at all :wink:

···

Le 20 juil. 06 à 13:25, F. Senault a écrit :

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 ?

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

···

On Thu, 20 Jul 2006, Julian 'Julik' Tarkhanov wrote:

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
Ruby for Rails => RUBY FOR RAILS (reviewed on
                                     Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net => me

[...]

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

and what's the returned value in that case ? nil, i guess...

···

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.

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

thanks very much, much more rubyish :wink:

the solution i like the best :wink:

···

Le 20 juil. 06 à 14:01, dblack@wobblini.net a écrit :

at which point it would probably cease to be necessary.

Yvon Thoraval wrote:

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.

···

Le 20 juil. 06 à 11:52, Joey a écrit :

--
Alex

fine, thanks, i've learned something this hot afternoon :wink:

···

Le 20 juil. 06 à 14:42, Matthew Smillie a écrit :

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