2 Replies Latest reply on Apr 10, 2013 7:26 AM by JADarnell

    General Javascript question (format style)

    JADarnell Level 1

      Though this is a general Javascript question, whenever I see a question like this asked of a forum, regardless of how specialized it is, it generates lots of traffic and rarely does the submitter get scolded.  It seems that everyone has their own ideas about style to use when writing code, which is fine.

       

      But in every Javascript sample I have seen, curly brace format loks like this:

       

      for(a = 0; a < n; a++) {

         Some code...

      }

       

      Personally I prefer this style:

       

      for(a = 0;a < n; a++)

      {

         Some code...

      }

       

      I use a for loop as an example, but this could be true for function definitions, while loops, if statements, the whole gamut of JS code which uses code blocks.

       

      Is this just a Javascript convention or is there some reason behind it?

        • 1. Re: General Javascript question (format style)
          [Jongware] Most Valuable Participant

          JADarnell wrote:

           

          .. in every Javascript sample I have seen, curly brace format looks like this ..

          Not in mine you don't I prefer the second style as well.

           

          These are not specifically "Javascript" conventions, the same applies to Java, C, C++, C#, Objective-C, and some other languages with a similar syntax. The debate is actually about 'readability' -- people argue that the first brace is part of the "for" loop line, and the indentation in the second line is enough to immediately see "this is inside a loop".

           

          I beg to differ. A function, for example, is always written like this:

           

          function (x)

          {

            .. statements ..
          }

           

          -- I have never seen this written as in your first example. For me, there is no difference between a function body and the functional 'repeating' block in a for .. loop. if .. else .. get a similar treatment:

           

          if (statement fulfills requirement)

          {

            .. do something useful ..
          } else

          {

            .. do something else ..
          }

           

          I must admit I'm not *entirely* consequent in this system. While I write a top-while loop thusly:

           

          while (x)

          {

            .. statements ..
          }

           

          .. a do-while loop gets a slightly different treatment:

           

          do

          {

            .. statements ..

          } while (x);

           

          In the end I don't think it really matters what style you adopt personally. It's best not to *mix* these, because that can cause serious mis-alignment of your braced code.

          • 2. Re: General Javascript question (format style)
            JADarnell Level 1

            Thanks for the reply.  This discussion is about style; everyone has an opinion and though I know I am right (grin) I do not begrudge someone a different style if that's what makes him/her comfy.  I admit I can be anal at times, however.  For example I wrote some JS code just yesterday that looked like this:

             

            myobject.eventListener = function()

                                                      {

                                                           do some stuff;

                                                       };

             

            I really like an abundance of whitespace in my code.  This style works for me but has driven a number of my colleagues in this office up a tree.

             

            Please note that in the forum editor the curly braces fall directly under the "f" in the "function" token.  The display program moves them.

             

            R,

            John