Live data from Hacker News

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

jesseduffield.com

171–180 of 326 posts

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

#171
post #149

Earlier quoted context omitted.

Only false and nil are falsey, and the language shines for it. There is nothing weird about zero being a truthy value.

In what other languages does 0 evaluate to true?

Lua, Elixir, Common Lisp, Scheme, Racket, Clojure.

Most statically-typed languages don't let you evaluate 0 as a boolean.

IMO, 0 is a value, and values should be truthy. 0 being falsy is only a wart from C.

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

#172

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.

That makes sense!

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

#173

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…

In lisp, "unless" is a great way to signal that there will not be a dual branch. Similarly, they have "when" for the positive case only. They also have the advantage of not needing a "progn" segment if you want to do more than 1 thing on the case that they are "true".

I confess at first I thought they were somewhat superfluous, but they are more readable now that I'm used to them. Completely agreed on it being stylistic, mainly.

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

#174
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 in this context. For example:

    puts "That's a prime" if is_prime?(variable)
makes sense. So does:

    puts "That's not a prime!" unless is_prime?(variable)
Far more so than:

    puts "That's not a prime!" if !is_prime?(variable)
The author decides however to add more conditionals. Fine, don't do that. Or that it's unreadable in some contexts. Fine, don't do that either.

The whole point of Ruby - the only reason it really exists - is that it should be fun and easy and not require you to spend too much time stressing over rules. If you read a piece of Ruby and don't like it, change it. It's not Python - there is more than one way. Use the way that makes sense in the context you're using it.

If you can't trust yourself or your team to do that wisely in a project, consider changing languages, because guess what? The language isn't going to change because you don't like that thing.

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

#175
post #30

Earlier quoted context omitted.

Sadly `¬` never caught on. But I suppose being visually similar to `-` isn't much better.

The "logical negation" sign used to have the vertical line as long as "|", so e.g. "¬A" looked more like "‾|A" but fully vertically aligned with the letter. Sadly, this glyph fell out of usage in fonts in favour of "minus with cedilla/descender".

I'd always understood it came from Frege's Begriffsschrift, a small descender from a horizontal line (part of a larger diagram), so like a short wide T, then lost its right arm later. Would you have some examples? (a quick google images shows nothing like that ...)

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

#177

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.

Negation makes code harder to read and understand.

Unless has negation built in.

Which is why unless is such a hard concept for people to grasp, not just in code, but also in regular English.

At least the "if not" makes the negation explicit, which can then be easily eliminated using several different techniques (early return, swap with the else condition) if the negation is confusing enough.

However, "unless" necessarily includes negation because that's how the word is defined.

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

#178
My guess is what's putting me off on unless is the baked in negative. If I look at a condition and imagine "if true" I can follow along, but "unless true" makes me think "what does that even mean". Another issue might be that not everyone is a native english speaker (like me), and that we underestimate that thinking in boolean logic might not be the most natural thing in the world, and once you learn it, you usually learn it in a foreign language. Substituting "if !" with "unless" just throws a wrench in the patterns you're used to using.

I disliked it as well in CoffeeScript. The use of else blocks is especially annoying, but even otherwise I don't like it. Some might say you shouldn't use it with an else block, but this is not realistic if you work in a team.

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

#179

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.

Y'know, I've written a lot of python these past years, and python is just full of aliases and different ways to do things.

Just an example: "if x is not None:" is syntactic sugar for "if not x is None:".

String formatting: there are f-strings, % replacement, .format, probably more methods.

And that's fine, languages evolve over time, add better ways to do things, and don't deprecate the old way.

It's the hypocrisy of flaunting that motto while the language is so full of the exact opposite that always annoys me.

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

#180

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]
Post reply on HN