Loading ...
Sorry, an error occurred while loading the content.

2741Re: Ternary Indentation Question

Expand Messages
  • douglascrockford
    Jan 18, 2012
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "spence.randall@..." <randall@...> wrote:
      >
      > Given the following example code:
      >
      > function test(x) {
      > ~~~~'use strict';
      > ~~~~return x === 1 ? 'one' :
      > ~~~~~~~~x === 2 ? 'two' :
      > ~~~~~~~~~~~~x === 3 ? 'three' :
      > ~~~~~~~~~~~~~~~~x === 4 ? 'four' : 'zero';
      > }

      I would right it this way:

      function test(x) {
      ~~~~'use strict';
      ~~~~return x === 1
      ~~~~~~~~? 'one'
      ~~~~~~~~: x === 2
      ~~~~~~~~? 'two'
      ~~~~~~~~: x === 3
      ~~~~~~~~? 'three'
      ~~~~~~~~: x === 4
      ~~~~~~~~? 'four'
      ~~~~~~~~: 'zero';
      }

      The placement of ? and : makes it easier to read them as then and else if.
    • Show all 8 messages in this topic