Live data from Hacker News

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

jesseduffield.com

211–220 of 326 posts

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

#211
post #196

As the article starts to explore, it's more useful in the context of a single line conditional with a single boolean to evaluate, or what I sometimes call a "dangling conditional". This is valid ruby: do_something if boolean_expression All unless does is allow you to negate it using an expression that's more natural to most people do_something unless boolean_expression To me the negation of an if with ! is less clear…

Maybe these dangling conditionals are easier for you to read, but not for me. Were I reading through real-world code that did things like that: "OK, then we do this, and then we do that, and then-- Oh wait! Backtrack! We didn't do that thing at all! ... Now where were we, on our actual bug, before some very special person's syntactic speed bumps?"

But these are not absolutes. You can learn to use and read `unless` just as effortlessly as you did with `if`. I didn't learn to use/read `unless` on college. But I did focus on trying to learn these things and today I have another tool in my belt.

It's an opportunity to get better at something versus banning it from usage because we aren't used to it.

Growing is, for me, almost always the right choice.

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

#213
post #133

Earlier quoted context omitted.

I treat the 'if' statement and the '!ruby_dev' conditional as separate entities. That allows thinking of the conditional alone in boolean logic terms, instead of having to consider the outside prefix statement too. Therefore, coming from a computer science background I don't like 'unless'.

I could see that. Maybe it really is because I'm more of a writer than a computer scientist. (For context: I dropped out of comp sci and took a job as a web dev ~18 years ago. Never looked back. But have ended up learning more about copywriting and marketing than computer science along the way.)

I'm a computer scientist and not much of a writer yet I prefer the use of "unless" over a negated "if". Then again, at the start of my career I used quite a bit of Perl and it was created by a linguist.

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

#214

I'd consider myself a writer more than a developer, but I've been working in Rails for over 15 years, and one of my absolute favourite things is "unless". Why? Because it allows you to express yourself more elegantly. The click-bait title is misleading. It's meant to ridicule "unless", but actually achieves the opposite. If you were to write the title of the post as code, it would be: unless !ruby_dev read article en…

as a non-native speaker, unless requires additional time to parse than "if not"

Also note that unless itself contains another negative ( It's just easy to see if more than one conditions are evaluated:

if (!ruby_dev && cpp_dev)

vs

unless (ruby_dev && !cpp_dev)

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

#216

Earlier quoted context omitted.

The author's point is that conditions in code tend to add up. What may start as a simple single condition, may not remain that way a year later. Since there's always the possibility of a certain conditional becoming more complicated, it makes sense to opt for the equivalent solution that makes adding those additional conditions easier. The alternative is to change the keyword when you add those additional conditions,…

That strikes me as a premature optimization. If you have a single condition, and unless makes it more readable, use unless. If the conditions do pile up in the future, change it to an if.

Great, now you've got this weird logic on your style guide that your code reviewers will have to point to, and hopefully the new engineers on your team will remember. But maybe they're in a hurry and will quickly add another expression just this one time, multiplied by many engineers.

It's far simpler to ban use of unless.

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

#218

I'd consider myself a writer more than a developer, but I've been working in Rails for over 15 years, and one of my absolute favourite things is "unless". Why? Because it allows you to express yourself more elegantly. The click-bait title is misleading. It's meant to ridicule "unless", but actually achieves the opposite. If you were to write the title of the post as code, it would be: unless !ruby_dev read article en…

unless is one of those things I have to read 2-3 times and it totally bogs me down.

I have a similar problem with list.filterNot() in Kotlin

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

#219
> The human brain, impressive as it is, grinds to a halt when parsing with double negatives.

Things like this sound like: "I have this characteristic, therefore everyone has this characteristic."

Some of us ain't got no problem with double negatives, and often find that "unless" tightens up code nicely, especially for single negatives.

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

#220
When I see unless in the prefix position (unless x then y) I have mentally read it twice. I reread it as `if not` because otherwise my brain can’t grok it. But if it is in the postfix position (y unless x) then it is super easy for me to grok. I think that is because how we would use it in natural language.
Post reply on HN