Live data from Hacker News

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

jesseduffield.com

31–40 of 326 posts

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

#31
"Unless" should only be used when the imperative condition is simple.

You don't understand Ruby if you think there is something wrong with trying to have more than one option to do things, imperfect shortcuts and more human-readable code.

Unless works really well when there is one named boolean variable after it and it's named correctly or if the condition is simple to understand.

It also works well when used inline and without an else statement. For other cases "if" is better-suited. But then again "unless" is another option in your tool chain that makes for a more elegant programming language if you're not a stickler and not constantly finding yourself pining for a platonic language.

  user = User.find(2000)
  return unless user
It'd be easier to miss the "!" character in the case of "if !".

It also makes code read better in English.

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

#32
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 unless, on the other hand, does not work for me. It is also awkward in short English sentences. And long English sentences, where beginning with unless is more appropriate, don’t have analogies in single-line ruby statements.

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

#33
post #10

The visual difference between a ! and an l is not that great, send_email if !user.suspended? or send_email if luser.suspended? The latter could easily be an aggressive dev sending abusive emails to users they dislike enough to call lusers, at first glance at least ...

Funny, when I read your point, I was sure and ready counter with an argument that code editors must highlight symbols and unary operators differently (colours, maybe spacing as well). But I just checked this on a couple of Ruby and JS highlighters and they are actually not! :scream:

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

#34
The real problem is that all negatives make it harder to understand things. They open the door for double (and triple) negatives to find their way into the code, and then bang: The only person who can read it is the person who wrote it.

Since unless has a not built into it, it has a lot of potential to confuse people. In my experience, guard clauses are the only place where they make sense.

  def mute_mic  
    return unless mic_active?
  
    ..
  end
In this sense, you know that the entirety of the function is an expected (positive) case.

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

#35

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.

It's not pointless, it makes code read better. It's another option to have. It's not meant for every use case. It indicates you don't understand Ruby if you find yourself using an "unless" in a complex boolean operation. It's meant for simple cases like an inline return statement

  return unless User.exists(id=100)

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

#36
post #18
post #5

The worst thing about `unless` and double negatives, is having been raised in a culture with different double negative rules than English. In Italian a double negative is still negative. I know boolean logic pretty well, but `unless !something` still trips me up to this day.

Ain't nobody got time for that!

Funny how human languages converge.

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

#37
post #4

I don’t disagree with the essay, and I do generally dislike “unless” as it’s a cutesy statement which does not pull its weight (it would probably be better if there was no “else” clause at all). However I find some of the examples / justifications unfortunate e.g. > I find the second option less readable because it suggests that raising the error would be the normal thing to do, when in fact it’s the exceptional thin…

Fair enough argument for that particular example, but I think the author's point still stands for the other example (if !user.suspended do send_email end)

That example makes more sense as:

  send_email unless user.suspended?
Here we see that we normally send email - except for the exceptional case of the user being suspended.

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

#38
post #35

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.

It's not pointless, it makes code read better. It's another option to have. It's not meant for every use case. It indicates you don't understand Ruby if you find yourself using an "unless" in a complex boolean operation. It's meant for simple cases like an inline return statement return unless User.exists(id=100)

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.

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

#39
post #11

I'm mildly surprised the article doesn't mention that Perl, one of the languages that influenced Ruby, also has unless.

In Perl you can write postfix conditionals: `thing() unless $condition`, which at least is more natural usage of "unless" to a native English speaker than the other way around. That's the best way to use it IMO.

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

#40
post #23

Earlier quoted context omitted.

Perhaps a Python style spelt-out `not` for boolean negation would have been a better solution for this than `unless` in deep retrospect? send_email if not user_suspended?

Ruby has "not", but the precedence rules between "not" and "!" differ

Well I really showed myself up as commenting on a topic that I know nothing about! :-)

But that's actually ideal in this context, a really low precedence operator is exactly what you want because you can swap `unless` for `if not` without worring about extra parentheses:

send_email if not user_suspended? || user_opt_out?

Post reply on HN