Live data from Hacker News

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

jesseduffield.com

231–240 of 326 posts

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

#231
common-lisp has functions called remove-if-not (applicative) and delete-if-not (destructive version).

The spec says that [t]he functions delete-if-not and remove-if-not are deprecated.

But all that is wrong is their double negative name. remove-if-not is precisely the same thing as keep-if, which is too useful to deprecate. Numerous languages have a function like this, sometimes called filter or similar.

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

#232

Earlier quoted context omitted.

unless is one of those things I have to read 2-3 times and it totally bogs me down.

Interesting! I wouldn't want to argue against your experience. I do wonder if it's an issue of "unless" being misused? Programmers using it just for fun rather than considering whether it's the best choice in context?

I think it is also an issue of familiarity with English. Not all developers are fluent in English.

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

#233
So:

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

But:

> Read this post if you’re a Ruby developer

Surely?

Seriously though, the main way Ruby went wrong was an obsession with this entirely superficial kind of expressiveness and simplicity. Rubyists will lard their code with all sorts of "convenient" and "easy" features, to save a few keystrokes here and there, at the expense of bloating their APIs and hiding weird, hard-to-reason about magic.

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

#234
Use rubocop. Ban the use of `unless` except as a trailing conditional and ban double-negatives. Then the only thing you can do is `foo unless whatever?` and if someone comes along and modifies that to something horrible rubocop will yell at them that it is horrible (and `rubocop -a` will auto-fix it).

Use the tools.

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

#235

Earlier quoted context omitted.

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

> 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).

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

#236

Earlier quoted context omitted.

Interesting! I wouldn't want to argue against your experience. I do wonder if it's an issue of "unless" being misused? Programmers using it just for fun rather than considering whether it's the best choice in context?

I feel like I have read if statements so many times that I have fast pattern matching circuits in my brain for them. When I come across an `unless`, I can't use them; I have to come back out into "conscious reading" mode, or something like that. Makes me crazy.

Man, I feel like you hit the nail on the head here... this is definitely the best way I could think to articulate why `unless` throws me off so much.

Weirdly, Ruby was one of the first languages I learned early in my career, and at the time I had no problem with `unless`. But after years of experience with other languages, I similarly feel that `if` statements now trigger the fast pattern matching circuits in my brain, while `unless` makes me do a double-take and basically translate it into `if not`

At first I thought I was just becoming dumber with age, but I like your explanation better :P

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

#237
post #226

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…

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 ideal language would leave out pretty much anything that can be achieved with more basic constructs. It would of course have if-else, and leave out `unless`, but there would also be no switch statements or ternaries. Branching must be done with if-else or by looking up a value in a hash. No `for` or `while` loops because a simple `loop` construct with `continue` and `break` statements can do everything that `for` and `while` could do in other languages. No classes or inheritance because they're magical and they can be effectively simulated by the end developer if that's what they really want.

What I think would be really cool is to have a language with a syntax like Ruby but with only the most basic of programming language constructs.

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

#238

As the article starts to explore, it's more useful in the context of a single line conditional with a single boolean to evaluate, or what I sometimes call a "dangling conditional". This is valid ruby: do_something if boolean_expression All unless does is allow you to negate it using an expression that's more natural to most people do_something unless boolean_expression To me the negation of an if with ! is less clear…

I agree with this - I always found the single ! in conditionals to be too easy for your eyes to slide past and miss. Using `unless` makes it jump out harder that you're only doing the thing if the condition is `false`.

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

#239
post #93

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.

I love Ruby for this kind of stuff. You don't have to have arguments about which syntax you prefer, you can just use the one you prefer and let your coworkers use theirs. However, a lot of developers love very strict style guidelines. I've never understood why, but it's a discussion I get into very very often.

Hm, I have the exact opposite interpretation. This is exactly the kind of feature that leads to arguments. If `unless` didn't exist, what argument could people be having about using `if !`?

In a shared codebase, I think there should be as few (equally effective) ways to express an idea as possible. (Obviously there can be more- and less-efficient ways to write a function; I'm referring only to style.) The more arbitrary choices people have, the more time is wasted on choosing one.

If you're coding alone, that's fine - you just pick what you like, and spend as long as you like making that decision. But in a shared codebase, developers writing in different styles is a net negative on quality & readability. Spending time debating which style to prefer is a worthwhile, but unnecessary time sink. Automatically enforcing a pre-defined style is efficient and effective - that's why Prettier exists, is widely used, and has few configuration options.

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

#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 which I am not proficient. Stuff like ’unless’ is such a pointless obstacle in these scenarios.

So ”elegance” doesn’t come free. The cost is a loss of transferable knowledge from other languages. Your intuition is that this cost is low relative to the benefit of ”elegance,” I have the opposite intuition. Why stray from such universal conventions in favour of one person’s subjective aesthetic preference?

Post reply on HN