Probably an inane question, but why do we use the current JS coding patterns?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RMWChaos
    New Member
    • Oct 2007
    • 137

    Probably an inane question, but why do we use the current JS coding patterns?

    Not to go against the grain or anything (or disagree with Douglas Crawford), but why is this considered the de-facto JS coding pattern:

    [code=javascript]
    // The "Crawford" pattern
    function myFunc() {
    var a = "Hello";
    var b = "world";
    for (var i = 0; i < 3; i++) {
    alert(a + " " + b);
    };
    };
    [/code]

    When (to me) this is much clearer to read:

    [code=javascript]
    // The RMWChaos pattern ;-)
    function myFunc()
    {
    var a = "Hello";
    var b = "world";
    for (var i = 0; i < 3; i++)
    {
    alert(a + " " + b);
    };
    };
    [/code]

    In both cases, the code will be the same length when minimized, and they are syntacticly identical to one another. However, the second version just seems infinitely easier to read (to see where blocks begin, identify conditionals, etc.) and, therefore, easier to modify, bug-check, etc. (IMHO).

    Am I alone here? Maybe this is just my bias from working with BASIC and COBOL for so long, and after a while I will see the first version as easier to read ... perhaps.

    Oh, and what's the big deal about using spaces to indent (4, 8, 16, etc.) vs. Tab? So what if Tab lengths are not standardized between programs...as long as they are indented?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    See this thread in the Software Development forum. It's more generic.

    btw, I prefer the first method and I don't see why the second one has to be easier to read. In fact, the last bracket looks like it closes the for loop, but that's probably because I'm so used to the first method.

    Comment

    • RMWChaos
      New Member
      • Oct 2007
      • 137

      #3
      Originally posted by acoder
      See this thread in the Software Development forum. It's more generic.

      btw, I prefer the first method and I don't see why the second one has to be easier to read. In fact, the last bracket looks like it closes the for loop, but that's probably because I'm so used to the first method.
      I stated coding with Notepad and now with Notepad+. N+ highlights the code blocks with red lines when they are directly inline with one another, but only highlights the brackets themselves when they are indented differently.

      [code=javascript]
      // With Notepad+, brackets inline

      { // All these lines are
      | (...) // highlighted when
      } // brackets are inline.

      // With Notepad+, brackets not inline

      { // only this line...
      (...)
      }; // and this line are highlighted
      [/code]

      But even with just plain text, I still think it's easier to read. Just preference, I guess. I will read the link you provided, and may change my mind over time.

      Thanks!

      Comment

      • RMWChaos
        New Member
        • Oct 2007
        • 137

        #4
        Originally posted by acoder
        I actually use the first method. I've never had bracket problems because I use indenting too and all brackets line up with the command.

        I don't know how I started using this method as opposed to the second method, but it's stuck and I actually find the second method unsightly. If I'm copying code from somewhere or altering code, I'll change it to the first style (if it's not a lot of code). To save even more space, I'll also get rid of brackets altogether if it's a single line of code in if, for, while, etc.

        Just personal habits.
        Well, at least you're consistent. =D Funny, when I get code that looks the way you write it, with opening bracket on the same line, closing bracket inline with the function statement, I change it to look the way I prefer.

        But unsightly!? C'mon, even a blind man can see my way looks waaaay prettier. =D

        It seems that I am not, after all, alone on this point.

        Comment

        • Logician
          New Member
          • Feb 2007
          • 210

          #5
          Originally posted by RMWChaos
          Not to go against the grain or anything (or disagree with Douglas Crawford), but why is this considered the de-facto JS coding pattern:

          [code=javascript]
          // The "Crawford" pattern
          function myFunc() {
          var a = "Hello";
          var b = "world";
          for (var i = 0; i < 3; i++) {
          alert(a + " " + b);
          };
          };
          [/code]

          When (to me) this is much clearer to read:

          [code=javascript]
          // The RMWChaos pattern ;-)
          function myFunc()
          {
          var a = "Hello";
          var b = "world";
          for (var i = 0; i < 3; i++)
          {
          alert(a + " " + b);
          };
          };
          [/code]
          In both cases, the code will be the same length when minimized, and they are syntacticly identical to one another. However, the second version just seems infinitely easier to read
          The first layout is often known to C programmers as K&R notation (Kernighan and Ritchie) Its sole purpose/advantage is to conserve space in published listings; it does nothing to help readability or debugging and makes checks for correct brace pairing a nightmare. Unfortunately it gets copied globally by beginners who don't know any better. A more useful way to save space is not to put single statements within braces. I would like to show the way I would write it, but this forum's broken software makes a complete mess of my indentation.
          Oh, and what's the big deal about using spaces to indent (4, 8, 16, etc.) vs. Tab? So what if Tab lengths are not standardized between programs...as long as they are indented?
          Tab characters used to indent listings are just unnecessary and annoying. Any decent editor can configure the tab key to insert a fixed number of spaces.

          Comment

          • RMWChaos
            New Member
            • Oct 2007
            • 137

            #6
            Originally posted by Logician
            I would like to show the way I would write it, but this forum's broken software makes a complete mess of my indentation.
            Feel free to email me an example, if you don't mind! Like you, I don't like K&R for the same reasons you supplied.

            Currently, I am partial to either of the two methods below, but would be interested to see yours as well.

            [code=javascript]
            function myFunc()
            {
            var a = "Hello";
            var b = "world";
            for (var i = 0; i < 3; i++)
            {
            alert(a + " " + b);
            };
            };

            // ...or... //

            function myFunc()
            {
            var a = "Hello";
            var b = "world";
            for (var i = 0; i < 3; i++)
            {
            alert(a + " " + b);
            };
            };
            [/code]

            The only difference being the lack of additional indentation on the main function and for loop code blocks in the 2nd example. I can see where the 2nd example would be easier to read by those used to seeing the close bracket inline with the initiating statement. Otherwise, I like the first way, which is the way I have done it since starting to code JS.

            At this point, I figure if other people don't like my method, they don't have to use it, and if they want to borrow from my code, they can modify it the way they prefer it. I will do the same. =D

            Minify is going to take care of all these extra line breaks, indents, spaces, and dev notation anyway. The rest is just personal readability I suppose.

            Thanks!

            Comment

            • Logician
              New Member
              • Feb 2007
              • 210

              #7
              Originally posted by RMWChaos
              Feel free to email me an example, if you don't mind! Like you, I don't like K&R for the same reasons you supplied.
              Sadly only an image is reliable here.

              Comment

              • RMWChaos
                New Member
                • Oct 2007
                • 137

                #8
                So you align your brackets with the initial statement, but you are not using brackets with the for loop? Interesting. Is that reliable?

                [code=javascript]
                function myFunc()
                {
                var a = "Hello", b = "world";

                for (var i = 0; i < 3; i++)
                alert(a + " " + b);
                };
                [/code]

                Is that about right? What if there is additional code after the alert? I don't see how that would not confuse the end of the for loop.

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5390

                  #9
                  if you leave the brackets then only one following statement is executed in that loop ...

                  i would never do that for my own ... i always use brackets even in the if-statements ... but its allowed and a lot of people do it that way

                  kind regards

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by RMWChaos
                    Well, at least you're consistent. =D Funny, when I get code that looks the way you write it, with opening bracket on the same line, closing bracket inline with the function statement, I change it to look the way I prefer.

                    But unsightly!? C'mon, even a blind man can see my way looks waaaay prettier. =D
                    Hey, you weren't supposed to quote that! :D When did I write that?

                    I accept the points about brackets being easier to pair, but I don't have that problem because I'm consistent.

                    If I had to choose between yours and the 'other' method with one less indent, I'd choose that one any day.

                    But, hey, can't get too worked up about it. I can read all of these. The worst are something like this:
                    [code=javascript]function something()
                    {
                    if (blah)
                    {
                    for (...)
                    {
                    ...
                    }
                    }
                    }[/code]Argh! No indentation - makes it a nightmare!

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5390

                      #11
                      Originally posted by Logician
                      The first layout is often known to C programmers as K&R notation (Kernighan and Ritchie) Its sole purpose/advantage is to conserve space in published listings; it does nothing to help readability or debugging and makes checks for correct brace pairing a nightmare. Unfortunately it gets copied globally by beginners who don't know any better.
                      i strongly disagree with that and in case you call douglas crockford a beginner who didn't know any better then i think you should tell us what javascript-guru you are :) i think i should bow to you ... ;)

                      kind regards

                      Comment

                      • RMWChaos
                        New Member
                        • Oct 2007
                        • 137

                        #12
                        Originally posted by gits
                        i strongly disagree with that and in case you call douglas crockford a beginner who didn't know any better then i think you should tell us what javascript-guru you are :) i think i should bow to you ... ;)

                        kind regards
                        LOL! There are so many levels of amateur, professional, and guru between me and Douglas...I just bow to everyone to be safe. =D

                        Originally posted by acoder
                        If I had to choose between yours and the 'other' method with one less indent, I'd choose that one any day.
                        Now you're just being contrary. ;-)

                        Originally posted by acoder
                        Hey, you weren't supposed to quote that! :D When did I write that?
                        Um...

                        Originally posted by acoder
                        See this thread in the Software Development forum. It's more generic.
                        Maybe this is why you're stuck using the same ugly old style...short memory. =D

                        Comment

                        • Logician
                          New Member
                          • Feb 2007
                          • 210

                          #13
                          Originally posted by gits
                          i strongly disagree with that and in case you call douglas crockford a beginner who didn't know any better then i think you should tell us what javascript-guru you are :) i think i should bow to you ... ;)

                          kind regards
                          Anyone in the business of publishing listings, finds themselves under the same pressure to save space, which is why they'll use the 'opening brace at end of line' format regardless of their opinion of it.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Originally posted by RMWChaos
                            Now you're just being contrary. ;-)
                            Um...
                            Maybe this is why you're stuck using the same ugly old style...short memory. =D
                            I was only joking about you quoting me from another thread.

                            I'm not being contrary about choosing a particular method. I meant if I was forced to choose between your method and the method I find 'unsightly', I'd choose that one because yours seems a bit confusing being so used to using the command to line up with the closing bracket.

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Originally posted by Logician
                              Anyone in the business of publishing listings, finds themselves under the same pressure to save space, which is why they'll use the 'opening brace at end of line' format regardless of their opinion of it.
                              I'm not so sure of that. He wouldn't then include it as a coding convention, would he?

                              Comment

                              Working...