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…
Read this post ‘unless’ you’re not a Ruby developer
191–200 of 326 posts
Re: Read this post ‘unless’ you’re not a Ruby developer
#192Re: Read this post ‘unless’ you’re not a Ruby developer
#193Earlier 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…
Re: Read this post ‘unless’ you’re not a Ruby developer
#194Earlier quoted context omitted.
"if" is almost always easier to reason with, "unless" can be elegant, but it's so often misused that it doesn't really matter
I agree entirely that it can be misused. I'm not sure how often it's misused versus used properly. But I don't see why I should give up a useful logic tool just because others don't know how to use it well? Let me put it this way: Are there _any_ programming paradigms that don't get poorly used? We need to learn how to use our tools well, not reduce them to banality.
raise(“a long error string”) unless valid
This reads much worse, when the string pushes the line near max width, than: valid or raise(“..
Of course it’s another idiom to learn but it’s not a difficult one when used simply.Re: Read this post ‘unless’ you’re not a Ruby developer
#195I 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,…
Re: Read this post ‘unless’ you’re not a Ruby developer
#196As 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…
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?"
Re: Read this post ‘unless’ you’re not a Ruby developer
#197Unless 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
#198Earlier quoted context omitted.
Negation makes code harder to read and understand.
I'm getting the impression that this is not universally true. It's true for me. I find the word "unless" helpful when it eliminates double negatives. But there are enough people on this thread who feel differently that it makes me wonder if maybe we're wrong. If code is meant to be read and understood by all, and if "unless" is confusing to a large number of people, maybe those of us who like it should knock it off.…
Could you give an example of a double negative in this case?
I ask because using "if" does not lend itself to double negatives in my experience, but just thinking about "unless" I see double negatives being an issue (since unless is already negating the operand).
Re: Read this post ‘unless’ you’re not a Ruby developer
#199I 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,…
Re: Read this post ‘unless’ you’re not a Ruby developer
#200Earlier quoted context omitted.
"if" is almost always easier to reason with, "unless" can be elegant, but it's so often misused that it doesn't really matter
I agree entirely that it can be misused. I'm not sure how often it's misused versus used properly. But I don't see why I should give up a useful logic tool just because others don't know how to use it well? Let me put it this way: Are there _any_ programming paradigms that don't get poorly used? We need to learn how to use our tools well, not reduce them to banality.
Everyone's lazy or overwhelmed at some point, and it made mistakes too easy to make (the problem is that it overtightens the lug nuts because the ease of use removes the proper physical feedback).
In situations where you don't have the bandwidth to perfectly educate everyone and enforce rules, sometimes it's easier to limit the toolset. A tire iron can be misused too, but it's a little harder.