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…
Read this post ‘unless’ you’re not a Ruby developer
281–290 of 326 posts
Re: Read this post ‘unless’ you’re not a Ruby developer
#282Earlier 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.…
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
#283Earlier 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…
Re: Read this post ‘unless’ you’re not a Ruby developer
#284I'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, if not we already have some.
Re: Read this post ‘unless’ you’re not a Ruby developer
#285Earlier 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
Re: Read this post ‘unless’ you’re not a Ruby developer
#286Earlier 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…
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
#287Earlier 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.
Re: Read this post ‘unless’ you’re not a Ruby developer
#288Re: Read this post ‘unless’ you’re not a Ruby developer
#289I'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.
Re: Read this post ‘unless’ you’re not a Ruby developer
#290I'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.