Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 542 Bytes

becareful-of-chains-of-ternary-operators.md

File metadata and controls

32 lines (25 loc) · 542 Bytes

Be careful of chaining ternary operators

A chain of ternary operators make it hard to judge what is happening.

Do not do this:

$size = $button === 'submit' ? 'big : 'small';
$text = $button === 'submit'
  ? $l10n->getBigText()
  : $l10n->getSmallText();
$action = $button === 'submit'
  ? '/submit'
  : '/reset'

Do this:

if ($button === 'submit') {
  $size = 'big';
  $text = $l10n->getBigText();
  $action = '/submit';
} else {
  $size = 'small';
  $text = $l10n->getSmallText();
  $action = '/reset';
}