Live data from Hacker News

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

jesseduffield.com

61–70 of 326 posts

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

#61
post #35

Earlier quoted context omitted.

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)

I'll admit I'm not a Ruby developer, but wow it allows return conditionally inside an expression? What were the language designers thinking?! I can't think of any other language that allows return inside an expression (or break/continue). Let's see: C, C++, C#, Python, Javascript, Object Pascal, Java, Rust; all nope. That is indefensible.

If and unless can be used as post-fix clauses. Also, in ruby, -everything- is an expression, so... of course you can return from there

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

#62
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 future requirements” is a terrible argument.

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

#64
post #35

Earlier quoted context omitted.

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)

I'll admit I'm not a Ruby developer, but wow it allows return conditionally inside an expression? What were the language designers thinking?! I can't think of any other language that allows return inside an expression (or break/continue). Let's see: C, C++, C#, Python, Javascript, Object Pascal, Java, Rust; all nope. That is indefensible.

Nah, it's pretty cool once you get used to it

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

#65
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.

I'd never use an 'unless !something', always an 'if something'

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

#66
post #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…

I agree guard classes are great with unless...other places not so much

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

#67

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.

Wow, I didn't realize Python did this. So, for example, does it enforce functions with single returns or ones with multiple return sites? Also, does it prefer shallowly nested functions with guard clauses or highly nested if / else clauses?

Really curious to see the one and only one obvious way these are handled.

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

#68
post #15

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.

To be fair, Ruby inherited "unless" from Perl. And Perl literally has the opposite design principle, TIMTOWTDI or "there is more than one way to do it". I'm not arguing here -- I prefer the "one obvious way" principle. But you can design a beautiful programming language without regard for the long-term learnings of software engineering. I always liked Perl's "unless", and I always made sure to not abuse it with doubl…

I only know `unless` from Perl, and it can make your code clearer if used sparingly. For example it allows you to express preconditions positively:

    die "You may only use port numbers 1024 and higher"
        unless $port >= 1024;

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

#69
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.

> In Italian a double negative is still negative. Same in Spanish! I think you're making a very good point here – understanding "unless" (quickly, i.e. without having to think about it) very much depends on one's understanding of the English language and/or one's mother tongue. Sure, one can probably get used to it (like one gets used to what "if/else" means etc.), but it definitely increases the mental effort needed…

I think we're going to have problems if we try to make all languages consistent for users of all languages.

If and unless are English language keywords besides being Ruby keywords; and in Spanish, the word for "if" and the word for "yes" only differ by an accent mark.

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

#70

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…

That’s exactly how I use ‘unless’. Simple on condition (no else branch) use cases in one liners, anything more complex, I move to if. Best of both world I’d say!
Post reply on HN