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

Re: [jslint] shorthand if statements

Expand Messages
  • Dan McNeil
    nice refactor ________________________________ From: Mark Volkmann To: jslint_com@yahoogroups.com Sent: Wed, May 19, 2010 10:53:58
    Message 1 of 3 , May 27, 2010
    • 0 Attachment
      nice refactor



      ________________________________
      From: Mark Volkmann <r.mark.volkmann@...>
      To: jslint_com@yahoogroups.com
      Sent: Wed, May 19, 2010 10:53:58 AM
      Subject: Re: [jslint] shorthand if statements


      On Wed, May 19, 2010 at 9:43 AM, Simon Kenyon Shepard <
      simon.shepard@...> wrote:

      >
      >
      > Apologies if this has already been covered at an earlier date, I recently
      > came across this as an example of good coding practice:
      >
      > // Don't
      > if (this.getBehavior("Modal")) {
      >
      > node.lastChild.style[property] = this._position[property] +
      >
      > (type(this._position[property]) === "number" ? "px" : "");
      > } else {
      >
      > node.style[property] = this._position[property] +
      >
      > (type(this._position[property]) === "number" ? "px" : "");
      > }
      > // Do
      > (this.getBehavior("Modal") ? node.lastChild : node).style[property] =
      > this._position[property] +
      >
      > (type(this._position[property]) === "number" ? "px" : "");
      >
      > the second example advocates using the shorthand if statement repeatedly to
      > reduce the verbosity of the primary example.
      > I find the shorthand example terse, cryptic and hence difficult to read,
      > but
      > I don't know if this is just a personal preference and if I'm missing
      > something that makes it better.
      > If I were writing this code I would probably introduce another variable and
      > write it half and half like this:
      >
      > var nodeStyle = node.style[property];
      > if(this.getBehavior("Modal")) {
      > nodeStyle = node.lastChild.style[property];
      > }
      > nodeStyle = this._position[property] + (type(this._position[property]) ===
      > "number" ? "px" : "");
      >
      > I was wondering what people's reactions/critiques/views were, is there one
      > dominant way or is it just preference?
      > If there is one way that is superior should it be a JSLint option?
      >
      I would do it like this:

      units = type(this._position[property]) === "number" ? "px" : "";

      nodeStyle = this.getBehavior("Modal") ?
      node.lastChild.style[property] :
      this._position[property] + units;

      --
      R. Mark Volkmann
      Object Computing, Inc.

      [Non-text portions of this message have been removed]







      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.