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…
Personally, I don't think "readable by people that don't know the language" is a reasonable feature to optimize a language around. And if you go on that direction, almost the everything on the language is a larger roadblock than an oddly placed conditional.
Read this post ‘unless’ you’re not a Ruby developer
271–280 of 326 posts
Re: Read this post ‘unless’ you’re not a Ruby developer
#272Re: Read this post ‘unless’ you’re not a Ruby developer
#273 (unless foo (error "Foo should not be nil"))
In less functional code, you sometimes see it for default values (though "OR" seems to be more idiomatic for that case): (unless foo (setf foo 3))
Regardless it has few of the downsides that TFA mentions (the sole exception being introducing a double-negative for unplanned compound test statements), but I don't find something like (unless (and foo (not bar)) (error ...)))
To be that unclear; we want both "foo" and "!bar" or error.Re: Read this post ‘unless’ you’re not a Ruby developer
#274`return if not valid?` or `return if invalid?`
Both make sense in my brain, whereas
`return unless valid?`
Feels like I need to make another connection to understand it
Re: Read this post ‘unless’ you’re not a Ruby developer
#275Unless would drive me insane. I do use IF NOT but when reading code my brain understands this format most clearly: IF some-expression ELSE Do-something
`unless` is generally used for guard clauses (eg `return unless authorized?`) or when there’s only one branch. You don’t tend to see `unless` used with multiple branches.
Re: Read this post ‘unless’ you’re not a Ruby developer
#276Re: Read this post ‘unless’ you’re not a Ruby developer
#277I've always felt that language features like 'unless' in Ruby and also things like 'on' and 'off' in Coffeescript fall squarely into the category of too clever. Simplicity will always win until the end of time.
(the headline however, is an exceedingly bad example)
Re: Read this post ‘unless’ you’re not a Ruby developer
#278I'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…
Re: Read this post ‘unless’ you’re not a Ruby developer
#279Obviously if your specific language doesn't make use of it, or makes use of it in a different way to ruby (e.g. is your natural language treating the second negative as a negation of the negative, or an intensifier of it?) then ruby's grammar will seem unnatural. But that's going to be true for anyone who's trying to map their natural language onto a programming language whose grammar is different to theirs.
Re: Read this post ‘unless’ you’re not a Ruby developer
#280I'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…
Nope. It's needless confusion. We're programmers who have stuff to do, and syntactic stuff like unless are not helpful. if (true) and if not(true) is simple and elegant, and doesn't require any person on my team to stop their flow to puzzle out what the code is supposed to be doing at that point.
You want a well-justified, inclusive, and consistent yet adaptable style when collaborating with people. It would be selfish to gatekeep a policy without regard for those principles.