Live data from Hacker News

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

jesseduffield.com

161–170 of 326 posts

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

#162

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…

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?

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

#163
post #123

I'm not a very experienced Ruby developer but RuboCop does a pretty good job at guiding and teaching you to use `unless` as a guard in method definitions, e.g.: def my_method(required_thing) raise 'required_thing is required' unless required_thing.present? ... end In practice, this hasn't been as issue as a consequence of the tooling and ecosystem around Ruby.

and why def my_method(required_thing) raise 'required_thing is required' if !required_thing.present? is a problem ?

I don't think it's a problem necessarily. "unless" is more obvious to me since since it visually stands out more than "if !required_thing" while I'm casually scanning the code. So, in practice, it acts as an indication from the author of the code to the reader that "this line of code is a guard since we're using an unless".

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

#165

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…

You've demonstrated that it is trivially replaced by 'if' and negation. I think that's almost the definition of inelegant.

A for can be replaced by a while and a jump, that does not make it more or less elegant.

IMHO, something ‶elegant″ in programming is not something that can't be built form something else (or we will all end up in writing only CMOVs), but something that conveys the meaning of its author precisely and concisely – which makes it inherently subjective. I'm in the `unless` team, I can perfectly understand if you are in the `no unless` team, but this argument does not make a lot of sense.

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

#166

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

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

#167

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…

The unless is still harder for me to grok.

The if !ruby_dev (read as "if not ruby_dev") is so much easier.

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

#168

Earlier quoted context omitted.

You've demonstrated that it is trivially replaced by 'if' and negation. I think that's almost the definition of inelegant.

Negation makes code harder to read and understand.

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

#169

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…

The unless is still harder for me to grok. The if !ruby_dev (read as "if not ruby_dev") is so much easier.

Fascinating! I find it slightly easier to understand "unless" than "if not". But only oh-so slightly, and probably not enough to justify making it harder for others to read my code.

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

#170

When I write ruby, and I love ruby, I don't do too much condition branching of logic. - `if` and `unless` are for guards. The meat of every method, (the happy path) is at the bottom of that method. - If there are legitimately two options that branch on a conditional, I try to make sure that both branches return the same type. (feature flags, a/b feature testing, etc.) - If I can refactor code to a case statement, I w…

I don't write ruby at all, but this is how I write my Java, Kotlin, and Javascript; fail-fast at the top of the method, with the happy path following.

And I'll take it one step further: I see a multi-line branch as a code smell in and of itself. If something in a branch takes multiple statements, I am of the opinion that the body of that branch should probably broken out into a method.

So if we're going off the article title, my code might look something like

  val isNotRubyDev = !user.isRubyDev
  if(isNotRubyDev) return
  
  // ...read article
Post reply on HN