News for hackers. Stuff that matters.
Re:Read this post ‘unless’ you’re not a Ruby developer by ricardobeat on Tuesday December 13, 2022 @09:31AM (#33969342)
That's a lot of words for a simple matter of cleanliness. Do not use 'unless' for complex expressions, only single conditions. I personally hate grouped negatives like the one shown:

    !(valid_token && !expired)
This expression is also a double (triple?) negative, and a lot harder to parse. The correct approach is to make the `valid_token` flag take `expired` into account, or add a third variable that represents validity, not append `expired` to the unless clause. This keeps everything readable, if the variable names are descriptive enough:

    valid_token = token.valid? && !expired
    return 'invalid' unless valid_token
It's also damning that the author uses `unless ... else` as his starting point for the critique, while he is aware that the style guide says "Do not use unless with else" (briefly acknowledged in the last section).

There are no comments on this comment yet.