clear code

Planted Jun 2021 Tended 3 years ago

Contents

Sometimes called “clean code,” though I prefer “clear” for its emphasis on code’s communicative properties.

  • Avoid negations, which require the reader to do extra mental work to reverse what follows. For example, prefer string.present? over !string.blank?
  • Extending the above, if you have an if/else where the condition is a negation (e.g. if foo != 1…) you can remove the negation from the condition if you flip the if and else clauses.

Further Learning