DEV Community

Discussion on: Ruby as elegant as ballet

Collapse
 
kenbellows profile image
Ken Bellows

Sure, I know it's possible, but it's not the common idiom in the Ruby community. And like I said above, my main worry with the always-return-the-last-expression idiom is that it can sometime be a bit less clear what's going on. In particular, I have seen (and occasionally been confused by) code like this:

def complex_business_logic(some_arg)
  if (first_condition(some_arg))
    some_arg.some_method()
  else
    x1 = do_something_else(some_arg)
    do_another_thing(x2)
    and_another_thing(some_arg, x2)
    #
    # many more lines of complex business logic here
    #
    # lots of indentation an loops and conditional branches
    #
    # so logic wow very enterprise many code
    #
    # ...
    #
    # ...........
    #
    final_result
  end
end
Enter fullscreen mode Exit fullscreen mode

I have seen code like this, the point being that the vast majority of the code happens after the first possible return value, only utilized if the first conditional passes. In these cases, I have sometimes had a hard time tracking down where exactly the returned value is coming from, just because the structure of the function obscures it. It seems to me that there's no easy way to visually scan a function and see quickly all of the possible return points. Conversely, in a language that enforces the return keyword or something similar, I can always just Ctrl+F for the word "return" ad find them all (or just look for that word.

Now obviously, there are two clear objections here. First, this is probably not the best way to write code, and it should probably be refactored into some smaller, eaasier to digest helper functions. However, this sort of code does in fact exist all over the place in the wild, so it's still a problem. And second, probably the bigger one, I am by no means a pro Ruby developer. I'm sure that very experience Ruby devs have a much easier time picking out the return points in complex functions. And, fine, I get that, there's stuff like that in any language. I still see it as a barrier to new community members, but again, every language has those.

Idk, it's not a huge problem or anything, and I actually did like working with it when I wrote Ruby years ago; just something I wonder about at times