Rubyists love the expressiveness and readability but I've definitely heard people call it unreadable, or lament that they have no idea what's going on. Readability seems to apply less to folks with strong backgrounds in entirely different areas. Things like unless thing_is_true as an option instead of if !thing_is_true is a feature many Rubyists love, but outsiders have a real aversion to.
As a non-native English speaker, when I read an unless on a codebase it gives my brain extra work to make sure when the condition will evaluate to true. It's possible that it is due to unfamiliarity, though.
Yes, and the same is true for English speakers as well I think.
I like to avoid #unless by ensuring that there is an inverse logical test available, just as #any? and #empty? are inverses.
I think that some Rails developers don't realise that just as you can call #valid? on a model, you can also call #invalid?, so you can avoid do_this unless my_model.valid? in favour of do_this if my_model.invalid?.
But I like #zero?, and have found situations where x == 0 was not "working" and x.zero? was. Don't ask me what it was though – I can barely remember yesterday – but maybe someone can explain that.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rubyists love the expressiveness and readability but I've definitely heard people call it unreadable, or lament that they have no idea what's going on. Readability seems to apply less to folks with strong backgrounds in entirely different areas. Things like
unless thing_is_true
as an option instead ofif !thing_is_true
is a feature many Rubyists love, but outsiders have a real aversion to.Coming from Perl, I really miss
unless
in other languages (I don't use Ruby).if thing_is_false
would be better of course.It depends on context of course, but in general I tend to disagree. Keeping booleans as the "positive" form seems to make things easier.
I really like to use unless for checking state and arguments at the beginning of methods:
(pseudocode)
As a non-native English speaker, when I read an
unless
on a codebase it gives my brain extra work to make sure when the condition will evaluate to true. It's possible that it is due to unfamiliarity, though.Yes, and the same is true for English speakers as well I think.
I like to avoid
#unless
by ensuring that there is an inverse logical test available, just as#any?
and#empty?
are inverses.I think that some Rails developers don't realise that just as you can call
#valid?
on a model, you can also call#invalid?
, so you can avoiddo_this unless my_model.valid?
in favour ofdo_this if my_model.invalid?
.I think the latter reads much better.
Wow, that
unless
throws me off waaay more than it should.How do you feel about this:
If I write
My linter will ask me to change it to
Actually, it your be more idiomatic to write:
I do love Ruby for those things. I can write business code in a way that is perfectly understandable the first time I read it.
Yeah, this was a bad example
But I like
#zero?
, and have found situations wherex == 0
was not "working" andx.zero?
was. Don't ask me what it was though – I can barely remember yesterday – but maybe someone can explain that.