Skip to content

"if not" vs "unless" #329

@giddie

Description

@giddie

Maybe a question more than an issue: I've gravitated toward using if not in certain circumstances, and I've figured out why:

In situations where I expect the condition to be false, and the code will normally run, I use unless:

unless name.empty?
  puts "Hello #{name}"
end

However, whenever I expect the condition to be true, and the code will not normally run, I use if not:

if not name == 'Paul'
  STDERR.puts "Unauthorized!"
end

Most people would probably use unless across the board, but in the latter example I'd find that difficult to understand at a glance, simply because the words "if" and "unless" have this semantic meaning associated with them. I'm interested in thoughts on this. This isn't something I particularly planned, so it's really not an attempt to over-complicate: I fell into this pattern of use because of my brain finding it easier to grok the keywords this way, and I eventually realised what I was doing. Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions