Live data from Hacker News

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

jesseduffield.com

281–290 of 326 posts

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

#281

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…

Should I read the damn article or not? I still have no idea

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

#282

Earlier quoted context omitted.

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

People are different, so different things click with how they think.

I would likely not be a programmer if it weren’t for Ruby and it doing things different.

I had tried to learn programming for years (JavaScript, Python, Java, C#), none of it stuck or progressed beyond following tutorials and one of the things I had serious issues with was negation.

Then I came across Ruby and it just kinda clicked into place like no other language had. With Ruby I was able to do my own things within a month.

Now I don’t really have a problem picking up other languages anymore. But there’s others out there right now struggling to learn programming and there should be as many programming languages doing things different as possible to increase the chance that they come across one that is obvious for the way they think.

This idea of there should only be one way of doing things is poison in my opinion.

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

#283
post #44

Earlier quoted context omitted.

If you're returning who cares about the scope? Are you saying visually you'd like to have indentation inside the if body? If that's the case there is also nothing stopping you in most languages from doing something as nasty as: a=1;b=2;c=3;d=a+b==c?4:5;return d

I do because return means end of scope. Except in your example it hides the fact that it's creating it's own scope. I'm not arguing other languages can't produce bad code, just that ruby is particularly suited for it especially as number of developers working on code increases. I've seen people propose a linter to enforce consistency - but at that point I might as well chose a language with better design choices - pl…

Ruby doesn't create a new scope for if/unless.

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

#284

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.

Go to the store and get milk, unless we already have some.

Go to the store and get milk, if not we already have some.

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

#285
post #260

Earlier quoted context omitted.

In Ruby (and Crystal), I tend to use `unless` for guards at the top of the method, or returns. These are typically `return foo unless baz`. When I write Elixir, `unless` gets awkward in a functional style of programming, and Elixir has guard clauses and pattern matching. I pretty much never use `unless` in Elixir despite using it in Ruby for years. Sometimes, I'll add extra methods with a negation in the name itself.…

Perl also had that delayed syntax check style as an option and I HATED IT. THING if logical condition Filter First is far better self-documentation: if condition THING

It works well in cases where you have an operation that consists of a certain sequence of statements, where one of the statements has to be omitted in a particular atypical situation. Instead of having interrupt the sequence with an if, you just tag the particular statement with the omitting condition at the end. That’s also when unless tends to be intuitive, because it suggests an atypical condition.

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

#286
post #259
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…

It is not all about cost-benefit. You say there is a difficult time transferring knowledge from other languages when they see the `unless`. That's fair. My response to that is that, to idiomatically think in Ruby is write code that reads well, and `unless` fits that. Every language has an idiomatic way in which one thinks and reasons with the language. While there are principles that can be transferred over across la…

This is a great comment. The language I use most is Python, and I am definitely biased toward its conventions. It's interesting to hear from the other side of the fence.

I want to suggest a slight reframing: instead of saying: "It is not all about cost-benefit," you could say "here is an addition to the 'benefits' column that hasn't been considered yet."

It's a totally reasonable argument; much better than "elegance." This was my main problem: that the critique of `unless` was much more plausible than the defence.

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

#287
post #171
post #149

Earlier quoted context omitted.

In what other languages does 0 evaluate to true?

Lua, Elixir, Common Lisp, Scheme, Racket, Clojure. Most statically-typed languages don't let you evaluate 0 as a boolean. IMO, 0 is a value, and values should be truthy. 0 being falsy is only a wart from C.

Thanks for that, I had no idea. Do you know if zero is truthy in erlang? It makes sense for elixir (based on ruby) but I'm wondering if erlang is that way too.

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

#288
post #261

Earlier quoted context omitted.

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

Just reread until you get it.

Admittedly, until (as the negation of while for loops) tends to be more straightforward to grasp than unless.

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

#289

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.

How about if you could write except if instead of unless?

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

#290

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.

By the same token, using until instead of while and negation would be inelegant, but I think until is less of a stumble block than unless.
Post reply on HN