Live data from Hacker News

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

jesseduffield.com

241–250 of 326 posts

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

#241

Earlier quoted context omitted.

That strikes me as a premature optimization. If you have a single condition, and unless makes it more readable, use unless. If the conditions do pile up in the future, change it to an if.

Great, now you've got this weird logic on your style guide that your code reviewers will have to point to, and hopefully the new engineers on your team will remember. But maybe they're in a hurry and will quickly add another expression just this one time, multiplied by many engineers. It's far simpler to ban use of unless.

It is neither weird, nor complex. Use unless in cases where the if has a single, negative condition.

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

#242

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…

'if !' clicks instantly. for 'unless' I have to read out the statement in my head, and draw mental logic lines about what condition this is checking.

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

#243
post #194

Earlier quoted context omitted.

I agree entirely that it can be misused. I'm not sure how often it's misused versus used properly. But I don't see why I should give up a useful logic tool just because others don't know how to use it well? Let me put it this way: Are there _any_ programming paradigms that don't get poorly used? We need to learn how to use our tools well, not reduce them to banality.

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

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

#244

Earlier quoted context omitted.

Only if you definition of elegance is "please express yourself in as convoluted a way as possible, so as to minimize the number of words I need to know." The truth is there is a balance in all languages. Nearly every word was invented to prevent having to say a string of other words to mean the same thing. "Unless" is a single word to mean "If not". I consider that elegant. However, some languages take this too far,…

> German famously has a word for nearly everything. I'm not sure that's elegant, in that it requires learning a far greater number of words. That's not quite how it works though, right? Compound words are exactly that, combining two or more words to narrow down the meaning, without having to invent a new word. It's almost like "if not" vs "unless"...

Correct. The chief difference between English and German in this respect is the use of spaces (and what is considered a word). In German, you can basically drop the spaces from a noun phrase and call it a word, but it's basically a low-consequence surface syntax difference.

Side note: ancient Greek didn't use spaces, and many other languages (Thai and Written Chinese are the ones with which I'm most familiar) either don't use spaces or make spaces optional. The distinction between a phrase and a word gets a bit blurry at times, and I find the distinction is seldom useful, particularly when making comparisons across languages.

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

#245
post #226

Earlier quoted context omitted.

For me honestly it's one of the (many) things I dislike the most about Ruby. Sure, I understand the "elegant" aspect, two symbols are shortened to one (although I'd argue that's not that important when it comes to clarity and legibility). But every time I read "unless" in code it's quite jarring. I have to consciously translate it to "if not", and even then seeing the "unless" keeps tripping me off, perhaps because i…

It's a matter of taste, but I tend to agree that the more features or constructs a language has, no matter how terse or convenient, the more unnecessary complexity is generated by using that language. The `unless` keyword makes the interpreter slightly more complicated and is another piece of language the human brain needs to recognize. It may seem inconsequential, but grains of sand make a hill, as they say. My idea…

Many language implementations internally translate source into a simplified de-sugared dialect as you describe as an early compilation step. I believe both ghc and rustc currently do this.

Though, I think it's rare to expand a pattern match/switch into an if-else tree, and much more common to expand an if-else into a 2-way pattern match/switch.

If you're really keen on such a language, you could fairly easily implement such a language as a pre-commit hook source code re-formatter so that all of the source in your repository is in your favorite language subset.

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

#246
post #240

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…

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…

That's a really powerful tool. I can force you to slow down an make sure that you the reader focus on one section of code I've written?

A statement that forces you to switch out of inaccurate skiming mode is insanely useful

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

#247
hmm, you're doing `unless` with `!` in condition you're doing it wrong.

    unless user.has_errors?
       user.save
    end
IMO better than `if !user.has_errors?`, at least for me: I will read it as "unless" in my head even in languages that only have `if`. You know what? I will just start using `unless` even harder, I will even make a rust macro for that.

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

#249
post #204
post #203

Earlier quoted context omitted.

The `for` is a very ergonomic syntax to mapping a computation to every element of a sequence. The `while` does a really different thing, it's not about sequences at all, but about checking some bit of mutable state repeatedly.

> The `for` is a very ergonomic syntax to mapping a computation to every element of a sequence. The very ergonomic solution to map a computation to every element of a sequence is `map`. In this case, `for` is filled with bookkeeping that does not matter.

[deleted]

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

#250
post #226

Earlier quoted context omitted.

For me honestly it's one of the (many) things I dislike the most about Ruby. Sure, I understand the "elegant" aspect, two symbols are shortened to one (although I'd argue that's not that important when it comes to clarity and legibility). But every time I read "unless" in code it's quite jarring. I have to consciously translate it to "if not", and even then seeing the "unless" keeps tripping me off, perhaps because i…

It's a matter of taste, but I tend to agree that the more features or constructs a language has, no matter how terse or convenient, the more unnecessary complexity is generated by using that language. The `unless` keyword makes the interpreter slightly more complicated and is another piece of language the human brain needs to recognize. It may seem inconsequential, but grains of sand make a hill, as they say. My idea…

Why bother with loop, continue, and break when you can have a more generic if+goto that can handle all the cases?

You can simulate the loop behaviour if that's what you really want

Post reply on HN