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

Re: Formatting question

Expand Messages
  • g2223060
    Douglas- I m not trying to convince you of anything ;) As background, I work as a user interface architect for a large financial services company that uses
    Message 1 of 16 , Aug 4, 2011
    • 0 Attachment
      Douglas- I'm not trying to convince you of anything ;)

      As background, I work as a user interface architect for a large financial services company that uses offshore developers. In our group, there are about 12-15 JavaScript programmers, working on 4 or 5 projects. My concern is the quality of the code they deliver. All of our JavaScript programmers are required to watch your videos as well as some internal videos that were inspired by you about our internal frameworks. We also have a policy that all JavaScript must pass JSLint before committing. I typically receive several requests for assistance each day. Some of them are due to inexperience with JavaScript and some of them are errors returned by JSLint that are not obvious. Most of the JSLint problems are truly problems- I can use it as a teaching opportunity, they correct their code, it passes JSLint and we move on. Others issues, such as this one, require me to do some research about the meaning of the error.

      So, as to this issue, if a variable is being initialized on the 'var' statement, that line may well exceed 50-60 characters, for example:

      var elementInWebPage = YAHOO.util.Dom.get('someElementID'), i, j;

      That first variable declaration is 59 characters. If I enforce {vars: false} then they are required to continue with one var statement, even though their editor/formatter may wrap the line.

      Long story a little longer (thanks for reading): Now that I know the real reason JSLint complains that 'j' should be in column 5, not column 8 I can inform our offshore staff to declare the uninitialized variables first. If they REALLY want to know why this is valid:

      var i, j,
      elementInWebPage = YAHOO.util.Dom.get('someElementID');

      and this is not:

      var elementInWebPage = YAHOO.util.Dom.get('someElementID'),
      i, j;

      I can tell them. But to say that the first way is "better" from a code quality or readability perspective, I just don't see that.

      -Steve

      --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
      >
      > --- In jslint_com@yahoogroups.com, "g2223060" <g2223060@> wrote:
      >
      > > Wow. So if I have a variable that is initialized on the var statement and it is 50-60 characters long, I should declare it LAST on the var statement, otherwise I will have to have a bunch of lines with one variable on each line, like:
      > > i,
      > > j,
      > > k,
      > > ...etc...
      > >
      > > For the life of me, I can't see how this JSLint rule helps readability or code quality. But that's just me... Thanks for your explanation!
      >
      >
      > Are you trying to convince me that you are stupid enough to have a 60 character variable name and one character variable names in the same function? I'm not buying it. I don't think you are *that* stupid.
      >
    • Robert Ferney
      Just as a personal impression. I do find var i, j, elementInWebPage = YAHOO.util.Dom.get( someElementID ); more readable than var elementInWebPage =
      Message 2 of 16 , Aug 4, 2011
      • 0 Attachment
        Just as a personal impression.
        I do find

        var i, j,
        elementInWebPage = YAHOO.util.Dom.get('someElementID');

        more readable than

        var elementInWebPage = YAHOO.util.Dom.get('someElementID'),
        i, j;

        having the un-initialize variables first, makes if obvious that they are
        being declared as variables. while in the second form, the i, j; look a bit
        orphaned.

        - Robert Ferney


        [Non-text portions of this message have been removed]
      • Doc Emmett Splendid
        I ve struggled with how to organize unintialised variables in one full var statement as well (largely because I only recently became convinced through
        Message 3 of 16 , Aug 4, 2011
        • 0 Attachment
          I've struggled with how to organize unintialised variables in one full var statement as well (largely because I only recently became convinced through experience that the "one var per function" is The Right Thing To Do). I haven't followed this format in the past, but I will do so henceforth for the very reason you say.

          Emmett


          ________________________________
          From: Robert Ferney <capnregex@...>
          To: jslint_com@yahoogroups.com
          Sent: Thursday, 4 August 2011, 13:33
          Subject: Re: [jslint] Re: Formatting question


           
          Just as a personal impression.
          I do find

          var i, j,
          elementInWebPage = YAHOO.util.Dom.get('someElementID');

          more readable than

          var elementInWebPage = YAHOO.util.Dom.get('someElementID'),
          i, j;

          having the un-initialize variables first, makes if obvious that they are
          being declared as variables. while in the second form, the i, j; look a bit
          orphaned.

          - Robert Ferney

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




          [Non-text portions of this message have been removed]
        • Mark Volkmann
          Keep in mind this option: var elementInWebPage, i, j; elementInWebPage = YAHOO.util.Dom.get( someElementID ); I like to declare all the local variables at the
          Message 4 of 16 , Aug 4, 2011
          • 0 Attachment
            Keep in mind this option:

            var elementInWebPage, i, j;

            elementInWebPage = YAHOO.util.Dom.get('someElementID');

            I like to declare all the local variables at the beginning of each function
            in alphabetical order and then initialize them on separate lines.
            The only time I initialized them in the var statement is when they have
            short values like 1, [], or {}.

            --
            R. Mark Volkmann
            Object Computing, Inc.


            [Non-text portions of this message have been removed]
          • g2223060
            I agree with you, Mark- I have been using that style in my own code. Probably why I haven t seen this error until the developer asked for my help with it :)
            Message 5 of 16 , Aug 4, 2011
            • 0 Attachment
              I agree with you, Mark- I have been using that style in my own code. Probably why I haven't seen this error until the developer asked for my help with it :) Maybe we should just have a policy that you don't initialize in the declaration...

              Some experimentation with JSLint shows that this is only enforced when there is ONE variable on the first line. For example, this passes:

              var a, b,
              c, d, e, f, g, h, i;

              But this does not:

              var x,
              y, z;

              -Steve

              --- In jslint_com@yahoogroups.com, Mark Volkmann <r.mark.volkmann@...> wrote:
              >
              > Keep in mind this option:
              >
              > var elementInWebPage, i, j;
              >
              > elementInWebPage = YAHOO.util.Dom.get('someElementID');
              >
              > I like to declare all the local variables at the beginning of each function
              > in alphabetical order and then initialize them on separate lines.
              > The only time I initialized them in the var statement is when they have
              > short values like 1, [], or {}.
              >
              > --
              > R. Mark Volkmann
              > Object Computing, Inc.
              >
              >
              > [Non-text portions of this message have been removed]
              >
            • abyssoft@ymail.com
              I use a formatting that is indeed supported by JSLint and is highly readable to me var ....varname1, ....varname2 = suchandsuch , ....varname3 = 1234; that
              Message 6 of 16 , Aug 4, 2011
              • 0 Attachment
                I use a formatting that is indeed supported by JSLint and is highly readable to me

                var
                ....varname1,
                ....varname2 = "suchandsuch",
                ....varname3 = 1234;

                that way if I see a var I know that until I see the ; I am still with in my declaration block.


                --- In jslint_com@yahoogroups.com, Doc Emmett Splendid <emmett.thesane@...> wrote:
                >
                > I've struggled with how to organize unintialised variables in one full var statement as well (largely because I only recently became convinced through experience that the "one var per function" is The Right Thing To Do). I haven't followed this format in the past, but I will do so henceforth for the very reason you say.
                >
                > Emmett
                >
                >
                > ________________________________
                > From: Robert Ferney <capnregex@...>
                > To: jslint_com@yahoogroups.com
                > Sent: Thursday, 4 August 2011, 13:33
                > Subject: Re: [jslint] Re: Formatting question
                >
                >
                >  
                > Just as a personal impression.
                > I do find
                >
                > var i, j,
                > elementInWebPage = YAHOO.util.Dom.get('someElementID');
                >
                > more readable than
                >
                > var elementInWebPage = YAHOO.util.Dom.get('someElementID'),
                > i, j;
                >
                > having the un-initialize variables first, makes if obvious that they are
                > being declared as variables. while in the second form, the i, j; look a bit
                > orphaned.
                >
                > - Robert Ferney
                >
                > [Non-text portions of this message have been removed]
                >
                >
                >
                >
                > [Non-text portions of this message have been removed]
                >
              • abyssoft@ymail.com
                That looks like jslint is right when you review it. by setting a single variable on the line of the var statement you are setting the expected pattern of 1
                Message 7 of 16 , Aug 4, 2011
                • 0 Attachment
                  That looks like jslint is right when you review it.

                  by setting a single variable on the line of the var statement you are setting the expected pattern of 1 variable per line

                  by setting either 2+ variables or no variables on the line of the var statement you are setting the expected pattern of one or more per line following the var statement.

                  I mention in another reply in this thread that I use the

                  var
                  ....varname1,
                  ....varname2 = "string",
                  ....varname3 = 0;

                  pattern with one variable per line and var statement on it's own line, as it is both supported by JSLint and I find it very easy to read.


                  --- In jslint_com@yahoogroups.com, "g2223060" <g2223060@...> wrote:
                  >
                  > I agree with you, Mark- I have been using that style in my own code. Probably why I haven't seen this error until the developer asked for my help with it :) Maybe we should just have a policy that you don't initialize in the declaration...
                  >
                  > Some experimentation with JSLint shows that this is only enforced when there is ONE variable on the first line. For example, this passes:
                  >
                  > var a, b,
                  > c, d, e, f, g, h, i;
                  >
                  > But this does not:
                  >
                  > var x,
                  > y, z;
                  >
                  > -Steve
                  >
                  > --- In jslint_com@yahoogroups.com, Mark Volkmann <r.mark.volkmann@> wrote:
                  > >
                  > > Keep in mind this option:
                  > >
                  > > var elementInWebPage, i, j;
                  > >
                  > > elementInWebPage = YAHOO.util.Dom.get('someElementID');
                  > >
                  > > I like to declare all the local variables at the beginning of each function
                  > > in alphabetical order and then initialize them on separate lines.
                  > > The only time I initialized them in the var statement is when they have
                  > > short values like 1, [], or {}.
                  > >
                  > > --
                  > > R. Mark Volkmann
                  > > Object Computing, Inc.
                  > >
                  > >
                  > > [Non-text portions of this message have been removed]
                  > >
                  >
                • Tom Worster
                  ... If you take that step, I hope you do so because you believe it is a good policy, not because of a tool like JSLint. Personally, fwiw, I think initializing
                  Message 8 of 16 , Aug 5, 2011
                  • 0 Attachment
                    On 8/4/11 5:40 PM, "g2223060" <g2223060@...> wrote:

                    >I agree with you, Mark- I have been using that style in my own code.
                    >Probably why I haven't seen this error until the developer asked for my
                    >help with it :) Maybe we should just have a policy that you don't
                    >initialize in the declaration...

                    If you take that step, I hope you do so because you believe it is a good
                    policy, not because of a tool like JSLint.

                    Personally, fwiw, I think initializing to a literal in the declaration is
                    just fine, and separating var status = 'ready'; into two statements
                    doesn't help, but calling functions and doing calculations etc. is for
                    subsequent lines of code.

                    tom
                  • R. Mark Volkmann
                    One reason I like that style, declaring all local variables on one line and initializing them later, is that it reminds me of Smalltalk. ... R. Mark Volkmann
                    Message 9 of 16 , Aug 5, 2011
                    • 0 Attachment
                      One reason I like that style, declaring all local variables on one line and initializing them later, is that it reminds me of Smalltalk.

                      ---
                      R. Mark Volkmann
                      Object Computing, Inc.

                      On Aug 5, 2011, at 7:24 AM, Tom Worster <fsb@...> wrote:

                      > On 8/4/11 5:40 PM, "g2223060" <g2223060@...> wrote:
                      >
                      > >I agree with you, Mark- I have been using that style in my own code.
                      > >Probably why I haven't seen this error until the developer asked for my
                      > >help with it :) Maybe we should just have a policy that you don't
                      > >initialize in the declaration...
                      >
                      > If you take that step, I hope you do so because you believe it is a good
                      > policy, not because of a tool like JSLint.
                      >
                      > Personally, fwiw, I think initializing to a literal in the declaration is
                      > just fine, and separating var status = 'ready'; into two statements
                      > doesn't help, but calling functions and doing calculations etc. is for
                      > subsequent lines of code.
                      >
                      > tom
                      >
                      >


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