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

    #16
    Originally posted by acoder
    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.
    Yeah, I know, it's just fun to pick on you. ;-)

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #17
      Here's what I don't like about omitting braces for single-line blocks:
      [code=javascript]
      var element = document.getEle mentById('theEl ement');
      for( var i = 0; i < 10; ++i )
      doSomething();
      element.doSomet hingElse();
      andSomeMoreStuf f('table');
      [/code]

      It's borderline-legible if indented properly; it would be a nightmare otherwise:
      [code=javascript]
      var element = document.getEle mentById('theEl ement');
      for( var i = 0; i < 10; ++i )
      doSomething();
      element.doSomet hingElse();
      andSomeMoreStuf f('table');
      [/code]

      Or occasionally I see this (*shudder*):
      [code=javascript]
      var element = document.getEle mentById('theEl ement');
      for( var i = 0; i < 10; ++i )
      doSomething();
      element.doSomet hingElse();
      andSomeMoreStuf f('table');
      [/code]

      And just to throw my hat in the stack, I absolutely *hate* it when programmers put the opening brace on the same line as the declaration.

      Comment

      • helimeef
        New Member
        • Sep 2007
        • 77

        #18
        I prefer the first method, dunno why. Just used to it.
        Last edited by pbmods; Dec 26 '07, 04:03 AM. Reason: Don't tell people not to flame you; all it will do is make them want to flame you.

        Comment

        • RMWChaos
          New Member
          • Oct 2007
          • 137

          #19
          There is only one situation I have come across where the opening brace absolutely had to be on the same line as the opening statement...

          [code=javascript]
          var EventCache = function()
          {
          var listEvents = [];
          return { // This brace must be on the same line or it errors
          listEvents : listEvents,
          ( ... )
          [/code]

          I don't know why this is the case. All I do know is that if I move the brace one line lower, she throws an error.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #20
            Originally posted by pbmods
            Here's what I don't like about omitting braces for single-line blocks:...
            There's no excuse for non-indentation whatever the style.
            Originally posted by pbmods
            And just to throw my hat in the stack, I absolutely *hate* it when programmers put the opening brace on the same line as the declaration.
            Why do those who like the brace-on-new-line style always seem to hate the brace-on-same-line style? Just wondering...

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #21
              Originally posted by acoder
              Why do those who like the brace-on-new-line style always seem to hate the brace-on-same-line style? Just wondering...
              :) i'm wondering too ...

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #22
                Originally posted by acoder
                Why do those who like the brace-on-new-line style always seem to hate the brace-on-same-line style? Just wondering...
                For me, it's because I'm used to following the curvy thing. It's just easier for me to line up matching braces rather than have to match it against indented text.

                It also helps spread the code out a bit and makes it look a lot cleaner to me.

                Of course, I usually take it a step further and mess with parentheses, too. You'll often see a lot of this in my code:

                [code=php]
                $__stmt =
                $db->prepareStateme nt
                (
                "EXEC [Some].[Stored].[Procedure] ?, ?, ?",
                array
                (
                (int) $customerID,
                (int) $offerID,
                $somethingElseI Dunno
                )
                );
                [/code]

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #23
                  Originally posted by pbmods
                  For me, it's because I'm used to following the curvy thing. It's just easier for me to line up matching braces rather than have to match it against indented text.

                  It also helps spread the code out a bit and makes it look a lot cleaner to me.
                  Oh, I know the reasons, understand them and accept them to an extent, but it still doesn't explain the absolute hatred for the on-same-line method as opposed to a simple dislike or just a preference of one over the other.

                  Comment

                  • RMWChaos
                    New Member
                    • Oct 2007
                    • 137

                    #24
                    Originally posted by acoder
                    Oh, I know the reasons, understand them and accept them to an extent, but it still doesn't explain the absolute hatred for the on-same-line method as opposed to a simple dislike or just a preference of one over the other.
                    Oh, I don't hate it (I try never to hate anything; causes too much grief in this world). In fact, I am considering moving to it, but as a relatively new coder, I like to have my braces and brackets line up so that I do not forget to close them. It makes for easy bug fixing IMHO. Helps when you forget to open one too, or am I the only person that ever does that? =D

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #25
                      Originally posted by RMWChaos
                      Oh, I don't hate it (I try never to hate anything; causes too much grief in this world). In fact, I am considering moving to it, but as a relatively new coder, I like to have my braces and brackets line up so that I do not forget to close them. It makes for easy bug fixing IMHO. Helps when you forget to open one too, or am I the only person that ever does that? =D
                      That's OK then. At least we've got one. I'm not claiming the brackets/braces-on-the-same-line method is superior. I just feel that its supposed disadvantages seem a bit exaggerated.

                      Comment

                      • Logician
                        New Member
                        • Feb 2007
                        • 210

                        #26
                        Originally posted by acoder
                        There's no excuse for non-indentation whatever the style.

                        Why do those who like the brace-on-new-line style always seem to hate the brace-on-same-line style? Just wondering...
                        Simply because it requires more (unnecessary) effort to scrutinise the code for missing braces, when their presence should be made clear.

                        Comment

                        • Logician
                          New Member
                          • Feb 2007
                          • 210

                          #27
                          Originally posted by acoder
                          I'm not so sure of that. He wouldn't then include it as a coding convention, would he?
                          The fact that he's reiterating a convention doesn't mean that he agrees with it, just that he has to be seen to be consistent.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #28
                            Originally posted by Logician
                            Simply because it requires more (unnecessary) effort to scrutinise the code for missing braces, when their presence should be made clear.
                            Each to their own, I guess. I don't find it difficult as long as the code is properly indented. However, I'm not going to argue about which one is better. I might even admit that having braces on a new line is probably better from a logical point of view. If we started all over again, we probably would've had that as the de facto standard, so to speak.

                            Comment

                            • pbmods
                              Recognized Expert Expert
                              • Apr 2007
                              • 5821

                              #29
                              Originally posted by acoder
                              However, I'm not going to argue about which one is better.
                              I will.

                              0>:} (<-- Horns holding up cheap tinfoil halo.)

                              Comment

                              • gits
                                Recognized Expert Moderator Expert
                                • May 2007
                                • 5390

                                #30
                                remember this is not a religious forum :)) ... no, just kidding ... but to me it seems that there are two groups of developers ... the ones that includes the brackets in their (even personal) 'code-view' and the others that don't ... i have no special preference for one of them ... but we use the 'crockford'-way (bracket-on-the-same-line) at work and so i use to always use it ... like acoder said ... i find the pointed problems with that exaggerated ... the much more important point is about the indentation ... and one more point to mention :) ... long lines ... i really hate that, just to say that i hate something too ... i hate long lines > 80 chars :)

                                kind regards

                                Comment

                                Working...