3 ms·
> return if !some_condition?` is far more readable Maybe it is easier to understand the condition or the boolean logic. I could agree with that. But readable?
by gls2ro 2y ago
> return if !some_condition?` is far more readable
Maybe it is easier to understand the condition or the boolean logic. I could agree with that.
But readable? No. It is very easy to miss the `!` when reading multiple lines of code. While missing `unless` it way hardwer.
Compare:
return if organisation_exists?
return if team_exists?
return if !user_exists?
return if project_exists?
with:
return if organisation_exists?
return if team_exists?
return unless user_exists?
return if project_exists?
Where can you see quicker that it will exit when the user does not exists? For me I need to read twice the lines that only have `if` and I can pick at the first read the `unless`.
Not saying you should use `unless` but it is a keyword, like any other keyword that you learn when you learn a new language. We should not confuse lack of familiarity with readability.
- CooCooCaCha 2y agoThe purpose of reading is to understand. And I don't know what to tell you other than when I see "unless" I have to pause for a moment to flip it in my brain. This doesn't happen with "!".
- gls2ro 2y agoI dont disagree with the idea that the purpose of reading is to understand. My assertion is that lack of familiarity (or better said lack of seeing unless used often) is the cause of the hard to understand issue and not that unless is a word hard to understand on its own. Here is a question to reflect: How come ! is easy to understand while actually "not" is better (short enough to not consume reading speed while being explicit)? But a lot of languages are using ! so it seems to be good enough for reading. My answer to this is: because it is used so much => familiarity.