Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

God that's such a landmine when reading code. Seeing a return without an explicit change of scope... Why ? To save one line ?

Yep that's why I hate ruby - worked on one mature codebase for a year and after seeing various such gems used across the project - from >10 devs - I'm confident I will never touch the language again.



Those one line returns are particularly handy at the beginning of methods, to validate or process arguments and return immediately for simple cases.


Short circuiting is such a beautiful way of avoiding giant nested "if" mountains. I wish it was more widely used.


right but

    if condition { return } 
works well enough in other languages and shows actual condition (the important part) to programmer first. if that if and extra brackets is really too long Perl way is also option.

    condition || return


If you're returning who cares about the scope? Are you saying visually you'd like to have indentation inside the if body? If that's the case there is also nothing stopping you in most languages from doing something as nasty as:

  a=1;b=2;c=3;d=a+b==c?4:5;return d


I do because return means end of scope. Except in your example it hides the fact that it's creating it's own scope.

I'm not arguing other languages can't produce bad code, just that ruby is particularly suited for it especially as number of developers working on code increases. I've seen people propose a linter to enforce consistency - but at that point I might as well chose a language with better design choices - plenty of alternatives these days.


> I've seen people propose a linter to enforce consistency - but at that point I might as well chose a language with better design choices - plenty of alternatives these days.

You're seriously proposing that adding a linter has the same organizational costs as changing languages entirely. Meanwhile, back in the real world...

Ruby has major advantages over many other languages and a linter is basic tooling you should have in every development environment.


It doesn't create no scope. Even for variables.


Ruby doesn't create a new scope for if/unless.


Those are called Guard clauses and they are implement in plenty of systems.. this paradigm has nothing to do with "Ruby".

In any case, the Ruby community already has good guidelines on the "Unless" usage, there are few scenarios where they are useful but it's not like you find them everywhere in a codebase.

For example, we don't use "Unless" with "Else", or use Unless with negation (like the article), or use Unless in nested If statement, etc. Rubocop will catch many of these and warn you.

My point is that experienced engineers will use the language as it was intended to and not abuse its features.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: