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

2743Re: [jslint] Re: Ternary Indentation Question

Expand Messages
  • Satyam
    Jan 18, 2012
    • 0 Attachment
      On 01/18/2012 01:57 PM, douglascrockford wrote:
      >
      > --- In jslint_com@yahoogroups.com
      > <mailto:jslint_com%40yahoogroups.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.
      >
      If both the 'if' part and the 'else' part had nested expressions, having
      all in the same indentation level would be confusing. This example has
      all the nesting on the 'if' part, it is not the most generic case.
      >
      >


      [Non-text portions of this message have been removed]
    • Show all 8 messages in this topic