Skip to content

Unless is complex to readΒ #190

@janpieper

Description

@janpieper

Versions

Elixir

$ elixir --version
Erlang/OTP 27 [erts-15.0.1] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Elixir 1.17.2 (compiled with Erlang/OTP 27)

Styler

$ mix deps | grep locked | grep styler
locked at 1.0.0 (styler) 4ba5bc40

Example Input

if env != :test do
  # ...
end

Current Behaviour

unless env == :test do
  # ...
end

Problem

We're about to upgrade styler to 1.x and I was shocked after checking what have changed after running the formatter 😲

I know that Elixir has if and its negated version unless, but I really don't get why unless env == :test should be more readable than if env != :test πŸ€” Just because it is there doesn't mean we need to use it.

When reading the code I first see env == :test and to know whether this is true or not I also need to look whether it is an if or unless clause. So, I always need to think about two things instead of just one when simply using a if-clause.

Maybe that's just because of how my brain works, especially because I never used unless before, but I would really like to know whether using unless is really better than having an if-clause with a negation.

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