News for hackers. Stuff that matters.
Re:Read this post ‘unless’ you’re not a Ruby developer by chillfox on Tuesday December 13, 2022 @10:12AM (#33969866)
Negation makes code harder to read and understand.
Re:Read this post ‘unless’ you’re not a Ruby developer by nickelcitymario on Tuesday December 13, 2022 @10:18AM (#33969956)
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.

I do find it more elegant and easier to read, but maybe you and I can process double negatives easier than the anti-unless crowd are able to process "unless" logic.

If that's the case, I'd be willing to sacrifice my preference for "unless" for the greater good.

Re:Read this post ‘unless’ you’re not a Ruby developer by lsaferite on Tuesday December 13, 2022 @10:51AM (#33970463)
> double negatives

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 by petre on Tuesday December 13, 2022 @12:25PM (#33972224)
Maybe he means unless this and|or|xor that. That's what I use it for rather than negate every paraneter.
Re:Read this post ‘unless’ you’re not a Ruby developer by layer8 on Tuesday December 13, 2022 @04:30PM (#33976141)
Sometimes adding a temporary boolean variable is a good way to resolve a double (or single) negative.
Re:Read this post ‘unless’ you’re not a Ruby developer by chillfox on Tuesday December 13, 2022 @03:56PM (#33975653)
People are different, so different things click with how they think.

I would likely not be a programmer if it weren’t for Ruby and it doing things different.

I had tried to learn programming for years (JavaScript, Python, Java, C#), none of it stuck or progressed beyond following tutorials and one of the things I had serious issues with was negation.

Then I came across Ruby and it just kinda clicked into place like no other language had. With Ruby I was able to do my own things within a month.

Now I don’t really have a problem picking up other languages anymore. But there’s others out there right now struggling to learn programming and there should be as many programming languages doing things different as possible to increase the chance that they come across one that is obvious for the way they think.

This idea of there should only be one way of doing things is poison in my opinion.

Re:Read this post ‘unless’ you’re not a Ruby developer by asdajksah2123 on Tuesday December 13, 2022 @10:27AM (#33970080)
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 by mattarm on Tuesday December 13, 2022 @12:04PM (#33971761)
> Which is why unless is such a hard concept for people to grasp, not just in code, but also in regular English.

This is the first I've heard about this confusion over the word "unless" in English.

In spoken English I do notice that most often the "unless" comes after a statement. Example: "I'll be there on time, unless the bus is late." That is more clear (to me) than "I'll be there on time, if the bus is not late." Maybe not by coincidence, I tend to like "unless" when it follows the action in in Ruby code, too.