no-negated-condition.md

January 23, 2019 ยท View on GitHub

no-negated-condition

It can be hard to reason about negated conditions:

  • if (not condition)
  • unless (not condition)

Negated conditions can often be avoided or simplified by:

  • Flipping if (not condition) ... else ... to if .... else ...
  • Replacing if (not condition) with unless
  • Replacing unless (not condition) with if

This rule forbids the following:

{{#if (not condition)}}
  ...
{{/if}}
{{#if (not condition)}}
  ...
{{else}}
  ...
{{/if}}
{{#unless (not condition)}}
  ...
{{/unless}}

And similar examples with non-block forms like:

<img class={{if (not condition) "some-class" "other-class"}}>
{{input class=(if (not condition) "some-class" "other-class")}}

This rule allows the following:

{{#if condition}}
  ...
{{/if}}
{{#if condition}}
  ...
{{else}}
  ...
{{/if}}
{{#unless condition}}
  ...
{{/unless}}

And similar examples with non-block forms like:

<img class={{if condition "some-class" "other-class"}}>
{{input class=(if condition "some-class" "other-class")}}

References