Live data from Hacker News

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

jesseduffield.com

271–280 of 326 posts

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

#271
post #240

Earlier quoted context omitted.

There’s a cost-benefit analysis here that you are refusing to do: Of the two variants you’ve posted, the first will click immediately with a generalist dev who doesn’t know Ruby. The second will have them reasoning out loud, and then reaching for the docs to double check that their common-sense intuitions are correct. This is not a contrived example. I regularly find myself having to read some code in a language in w…

Personally, I don't think "readable by people that don't know the language" is a reasonable feature to optimize a language around. And if you go on that direction, almost the everything on the language is a larger roadblock than an oddly placed conditional.

I wrote Ruby for years (for fun). I always had to stop and think twice about 'unless' and translate it into 'if not'.

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

#273
Common Lisp has an unless macro, but it lacks an else clause, implicitly taking the value "nil" if the test expression is true. It's probably most useful for aborting the flow of the code, e.g.:

  (unless foo (error "Foo should not be nil"))
In less functional code, you sometimes see it for default values (though "OR" seems to be more idiomatic for that case):

  (unless foo (setf foo 3))
Regardless it has few of the downsides that TFA mentions (the sole exception being introducing a double-negative for unplanned compound test statements), but I don't find something like

  (unless (and foo (not bar)) (error ...)))
To be that unclear; we want both "foo" and "!bar" or error.

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

#274
I find `if not` to be a far superior version, simply because it reads similarly to it's `if` counterpart, specifically for how conventionally condition methods are written in ruby.

`return if not valid?` or `return if invalid?`

Both make sense in my brain, whereas

`return unless valid?`

Feels like I need to make another connection to understand it

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

#275

Unless would drive me insane. I do use IF NOT but when reading code my brain understands this format most clearly: IF some-expression ELSE Do-something

`unless` is generally used for guard clauses (eg `return unless authorized?`) or when there’s only one branch. You don’t tend to see `unless` used with multiple branches.

This, I’ve mostly used unless with guard clauses

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

#277

I've always felt that language features like 'unless' in Ruby and also things like 'on' and 'off' in Coffeescript fall squarely into the category of too clever. Simplicity will always win until the end of time.

To me your two statements here are in conflict with each other. I use "unless" in Ruby when it leads to simpler code. That ability to write code which reads simpler is its sole reason for existing.

(the headline however, is an exceedingly bad example)

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

#278

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…

I have problems with 'unless' even in plain English text. I'm pretty good at English, I live in the USA, but I was not born here and my native language does not have a word for 'unless' (you have to use 3 words to express that idea!). I love Ruby, but the 'if' variation always get immediately parsed by my brain, while the 'unless' variation requires many seconds of thinking.

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

#279
The whole premise - that double negatives don't work for humans - is invalid. Natural languages have and continue to make use of double negatives and double positives (and in some cases, the positive-negative).

Obviously if your specific language doesn't make use of it, or makes use of it in a different way to ruby (e.g. is your natural language treating the second negative as a negation of the negative, or an intensifier of it?) then ruby's grammar will seem unnatural. But that's going to be true for anyone who's trying to map their natural language onto a programming language whose grammar is different to theirs.

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

#280
post #267

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…

Nope. It's needless confusion. We're programmers who have stuff to do, and syntactic stuff like unless are not helpful. if (true) and if not(true) is simple and elegant, and doesn't require any person on my team to stop their flow to puzzle out what the code is supposed to be doing at that point.

But inventing totally new languages even for billion dollar projects is totally fine?

You want a well-justified, inclusive, and consistent yet adaptable style when collaborating with people. It would be selfish to gatekeep a policy without regard for those principles.

Post reply on HN