Live data from Hacker News

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

jesseduffield.com

311–320 of 326 posts

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

#311

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 down…

This can be served by:

  (or foo (error "Foo should not be nil")) 
plus it propagates the value of foo in the non-error case.

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

#312

Earlier quoted context omitted.

If it's not a shift+ key on a popular keyboard layout, it's not going to gain widespread adoption in programming.

¬ is shift+` on the most common UK keyboard layout, but yeah, it would probably need to be popular in America .

I think most of the world either uses a US layout, or other layouts that don't have that key.

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

#313
To my mind, the point of `unless` is that is combined well with something that is unlikely but still possible.

  return the_obvious unless something_improbable_holds

  the_most_astonishing
This also align well with the lake of scalability of the number of condition: I doubt that in usual prosaic English its common to use "unless first-condition or second-condition or … ultimate-condition".

Of course it is logically equivalent to `if not`, but it is pragmatically not conveying the same information at human interpretation level.

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

#315
post #13

Earlier quoted context omitted.

There are two different kinds of double negatives and English actually has both. E.g. `I can't get no satisfaction` is structurally equivalent to French `ne ... pas`, i.e the second `no` is a reaffirmation, not a negation. On the other hand `read this unless you're not a developer` is a logical double negation and thus equivalent to `read this if you're a developer`. In practice of course there are usually implied su…

> any true "double negative" in English that isn't indirect A: Do you like your hometown? B: Well, I don't not like my hometown In boolean logic, not false is true, but in English there's often a middle ground. See also https://en.wikipedia.org/wiki/Law_of_excluded_middle

True! But that still falls into what I said about true double negations usually having subtext that justifies their existence. Compare "I don't hate it".

I'd argue that in "I don't not like my hometown" not-liking acts almost as a compound verb, similar to "disliking" (which is just using a Latin prefix to say "not"). Resolving the double negative results in information loss because it omits the subtext (which in this case is the important part of the answer).

In other words, saying "I don't not like my hometown" makes sense because "I don't like my hometown" implies negative emotions (disliking) rather than an absence of positive emotions (liking). The difference could also be conveyed in emphasis ("I don't like my hometown") but this is more subtle and easier to misunderstand.

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

#316
post #270

Earlier quoted context omitted.

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 never suggested we ”optimize the language around this feature.” I only suggested that it is a higher virtue than some user on HN’s subjective concept of ”elegance.” What do you think is more important: the speed at which C programmers can navigate your code base, or one person’s subjective idea of ”elegance”?

[deleted]

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

#317
post #121
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)

return if !User.exists(id=100) reads just fine and takes less letters. I don't think unless is "bad", it's just unnecessary > 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 unless `git status -s | grep -v 'RAILS_VERSION\\|CHANGELOG\\|Gemfile.lock\\|package.json\\|version.rb\\|tasks/release.rb'`.s…

Flowers are not necessary either.

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

#318
post #243
post #194

Earlier quoted context omitted.

On this note: I’m sad ruby style guides generally ban “and” and “or”. 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.

the 2nd form implicitly returns the value of valid if it's truthy and nil if it's falsey; the 1st form always returns nil

Indeed, but the return value of a guard clause shouldn't be important!

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

#319
post #194

Earlier quoted context omitted.

On this note: I’m sad ruby style guides generally ban “and” and “or”. 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.

That’s a very Perl idiom, did you previously write Perl?

No, I just think for all ruby is bought into these guard clauses, it's sad that we generally reject the feature that allows putting either the condition or the consequence first, depending on which reads better.

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

#320

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…

> Which of the following is better?

  if !ruby_dev
or

  unless ruby_dev

As a ruby dev of 5+ years, I still have an easier time with `if !ruby_dev`. I have to translate `unless` to `if !` every single time to grok something. Almost like an extra step in an algebraic expression being simplified.
Post reply on HN