Live data from Hacker News

Read this post ‘unless’ you’re not a Ruby developer

jesseduffield.com

111–120 of 326 posts

Re: Read this post ‘unless’ you’re not a Ruby developer

#112
post #44

Earlier quoted context omitted.

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 - pl…

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

Re: Read this post ‘unless’ you’re not a Ruby developer

#114
post #44

Earlier quoted context omitted.

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 - pl…

> 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.

Re: Read this post ‘unless’ you’re not a Ruby developer

#115
post #43

In Common Lisp, there is `unless' and `when' but they don't allow `else' branch. (when CONDITION BODY) (unless CONDITION BODY) The value of the statement is BODY or NIL if it wasn't executed. I can't remember a time when it wasn't clear (though you can always build usage that are unclear).

I think they mainly exist because `if' otherwise requires a `progn' if you want more than one expression in the true branch.

And using `cond' for 1 clause is a bit silly.

  (cond ((massivep building)
         (print-mass-warning building)
         (install-antigrav-module building)))

Re: Read this post ‘unless’ you’re not a Ruby developer

#116

statement unless condition is a wonderful bit of syntactic sugar. It reads like natural English if the variable or method it is evaluating is named well. Yes, sometimes you have to refactor into a traditional if. Sometimes you make the conditional its own method (which you’d end up doing anyway, assuming it didn’t stop at two or three conditions.) These are light tasks as the project grows. Starting a line with unles…

>It reads like natural English

That's a problem for those who learn it as a second language and have to translate "unless" to "if not" every single time.

Re: Read this post ‘unless’ you’re not a Ruby developer

#118

I don't have a problem with double negation but I hate ruby for this kind of design - pointless aliases for everything. It's the exact opposite of pythons "There should be one– and preferably only one –obvious way to do it" - they intentionally create solutions that have zero practical benefit - it's just fuels arguments based on preferences and introduces mental overhead due to inconsistency.

In Perl I once saw someone write; unless predicate do this else unless impossible_thing do the impossible What it did at runtime I have no idea, but it broke my brain for the rest of the week.

    if !cond1
        do this
    elsif !cond2
        do that
?

it's literally just `!` equivalent. Honestly I haven't seen much code using unless in wild. Especially that it is literally shorter to just !

Re: Read this post ‘unless’ you’re not a Ruby developer

#119
I'm not a very experienced Ruby developer but RuboCop does a pretty good job at guiding and teaching you to use `unless` as a guard in method definitions, e.g.:

  def my_method(required_thing)
    raise 'required_thing is required' unless required_thing.present?
    
    ...
  end
In practice, this hasn't been as issue as a consequence of the tooling and ecosystem around Ruby.
Post reply on HN