Live data from Hacker News

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

jesseduffield.com

181–190 of 326 posts

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

#181

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…

Doing Rails for half a year now and usually are pretty good at mental models.

I always have to stop at an unless because the double-negative is really hard to evaluate in the mind IMHO.

IMHO unless leads to unnecessary nesting. In your example, it reads like _most_ people are ruby dev. I try to keep the most-often excecuted flow as flat as possible (happy path) and just try to nest for additional/special cases. Unless just tries to do... different.

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

#182

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.

The problem is that what is obvious depends upon your way of thinking. To me very little in Python makes sense, but I understand that it does to someone and that’s cool. There’s no reason to hate on a language just because it doesn’t click with your way of thinking.

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

#183
post #159

Earlier quoted context omitted.

You've demonstrated that it is trivially replaced by 'if' and negation. I think that's almost the definition of inelegant.

It's Ruby though, where Array has "size", "length", and "count", all of which are equivalent. The programmer chooses which one they prefer based on the context or their style or the phase of the moon. Ruby inherits the Perl philosophy of "There's more than one way to do it". Compare with Python, and its "There should be one — and preferably only one — obvious way to do it." It's true that it makes the compiler less e…

Equivalent in output but not in implementation / performance. Does that matter? Not usually, no, but it depends.

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

#184

I really don’t understand the author’s point about adding extra conditions. They admit that many people will find a single condition with `unless` more readable. They then complain that it becomes unreadable when adding another condition. OK, so swap it out for an `if` at that point. No one is forcing you to keep using `unless` if the requirements change. “You should use a suboptimal solution to cater for unknown fut…

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, but in most workplaces it's very possible that someone with less rigor than you might come in and add those new conditions and not make the change.

And then if you want to add a new condition you need to parse and understand the more complicated new conditional first, before you can change it.

This adds a lot of maintainability risk for what appears to be, in the best case, a very minor benefit.

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

#185

Unless and ternaries are great signals in a pull request that you should build a truth table and double check it. They're backwards all the time, especially in less tested, error handling codepaths.

ternaries are expressions.

Just because of that they are far superior to if/else/unless, unless ;-) you're using the ternaries to execute statements with side effects instead of just returning a value.

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

#186

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…

The impetus to write the article seems to be Rubocop, whose rules are suggesting a specific way of doing things. But specific instances of a specific rule can be overridden, and if a rule proves itself to be more trouble than it's worth it can be disabled entirely.

I would go so far as to suggest jumping into Rubocop "cold turkey", meaning all rules on, is not very helpful on an existing codebase. The way my team approached Rubocop on a particular large legacy codebase was to start with a few hand picked rules that were non-controversially good, and discuss new rules one at a time as we gradually fixed the existing issues.

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

#187

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…

The unless is still harder for me to grok. The if !ruby_dev (read as "if not ruby_dev") is so much easier.

For some reason this one feels right to express with if-not to me too.

The one place I’ll often use unless is things like:

    return unless valid

    raise unless h.key?(k)
I think it might have something to do with:

1. How abundantly clear it is that the condition is a Boolean; and

2. The “false” condition indicating a “no work to do” situation.

I think I only use them for postfix return, break, next, and raise.

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

#188

Earlier quoted context omitted.

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

Interesting! I wouldn't want to argue against your experience. I do wonder if it's an issue of "unless" being misused? Programmers using it just for fun rather than considering whether it's the best choice in context?

[deleted]

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

#189

Earlier quoted context omitted.

Interesting! I wouldn't want to argue against your experience. I do wonder if it's an issue of "unless" being misused? Programmers using it just for fun rather than considering whether it's the best choice in context?

I feel like I have read if statements so many times that I have fast pattern matching circuits in my brain for them. When I come across an `unless`, I can't use them; I have to come back out into "conscious reading" mode, or something like that. Makes me crazy.

I'm a recovering rubyist (actually I still love ruby, just never get to write it anymore). The only times I felt `unless` was truly more readable than `if !something` was when it was used in the conditional suffix form like

    do_something unless already_done?

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

#190
As you learn more programming languages, these cute little operators become less cute and appealing, as you realize that !condition would be far more ubiquitous and transferable than something like unless condition.

I "grew up" in my career as a rails dev myself, but I favor things that are immediately clear to the broadest amount of people possible these days. And if you truly miss these constructs, in any language where you have metaprogramming, which is most these days amongst the broadly used ones, it should be trivially to implement them, should you insist on continuing to use them.

I use Elixir for my "fun" language these days, and they provide unless/2 as a macro, (if/2 is also a macro), but I haven't found myself reaching to use it once.

Post reply on HN