DEV Community

Discussion on: What language features/concepts do insiders of the language love and outsiders hate?

Collapse
 
ben profile image
Ben Halpern

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.

Collapse
 
blazselih profile image
Blaž Šelih

Coming from Perl, I really miss unless in other languages (I don't use Ruby).

Collapse
 
databasesponge profile image
MetaDave 🇪🇺

if thing_is_false would be better of course.

Collapse
 
val_baca profile image
Valentin Baca

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)

unless has_coffee:
  throw UncaffeinatedDeveloperError()
Collapse
 
bobbypriambodo profile image
Bobby Priambodo

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.

Collapse
 
databasesponge profile image
MetaDave 🇪🇺 • Edited

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

I think the latter reads much better.

Collapse
 
evanoman profile image
Evan Oman

Wow, that unless throws me off waaay more than it should.

Collapse
 
ben profile image
Ben Halpern

How do you feel about this:

If I write

[].count == 0

My linter will ask me to change it to

[].count.zero?
Thread Thread
 
evanoman profile image
Evan Oman

wat

Thread Thread
 
6temes profile image
Daniel • Edited

Actually, it your be more idiomatic to write:

[].empty?

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.

Thread Thread
 
ben profile image
Ben Halpern

Yeah, this was a bad example

Thread Thread
 
databasesponge profile image
MetaDave 🇪🇺

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.