The curley bracket!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    The curley bracket!

    Where do you put it?

    Like this
    Code:
    if (...){
    //blah blah
    }
    or like this
    Code:
    if(...)
    {
    //blah blah blah
    }
    Let the debate begin!!!
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    The second way of course. Never understood why anyone changed it. It's the only way (with indenting of course) that you can keep track of the opening and closing of brackets in nested statements. This was clearly a case of someone trying to change a standard that didn't need to be changed.

    Mary

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      The first way saves a line of whitespace. And the closing bracket lines up with the command.

      see?

      Code:
         if {
         }
      
         case {
         }
      
         while {
         }
      I prefer to do it the second way myself. (just playing the devils advocate).

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        If it were that easy there wouldn't be so many questions posted on this site that are solved by fixing the use of brackets.

        Why is it so important to save a line of whitespace?

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Because it saves on lines of code, makes it easier to print, and saves pain and agony on page-up and page-down buttons.

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            Originally posted by RedSon
            Because it saves on lines of code, makes it easier to print, and saves pain and agony on page-up and page-down buttons.
            This goes back to the IDE argument. Back in my day ... creak, creak ...
            we coded on unix or dos and printed out on dot matrix printers. We didn't have these concerns.

            Comment

            • AricC
              Recognized Expert Top Contributor
              • Oct 2006
              • 1885

              #7
              In school we called the indenting a great way of showing variable scope. If prefer to indent keeps things readable!

              Aric

              Comment

              • AricC
                Recognized Expert Top Contributor
                • Oct 2006
                • 1885

                #8
                Here is another debate what do you like:

                intIndex++

                Or

                intIndex += 1

                I prefer the +=

                Aric

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Doesn't this debate (or perhaps I should now say these debates) belong in a language-specific forum? My guess would be Java. As a (part-time, unofficial) VB developer, I don't have any great preference in the use of curly brackets.

                  Comment

                  • MMcCarthy
                    Recognized Expert MVP
                    • Aug 2006
                    • 14387

                    #10
                    Originally posted by Killer42
                    Doesn't this debate (or perhaps I should now say these debates) belong in a language-specific forum? My guess would be Java. As a (part-time, unofficial) VB developer, I don't have any great preference in the use of curly brackets.
                    These questions are cross language although they are C based as many of the languages are C based. It's not really a technical question in my opinion as it is actually a question of standardisation rather than syntax. The issue of standardisation is a developers discussion rather than a language specific one. I think this issue can extend to naming conventions, as in whether they are useful or not. Personally I find them very useful.

                    Mary

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Originally posted by RedSon
                      Where do you put it?

                      Like this
                      Code:
                      if (...){
                      //blah blah
                      }
                      or like this
                      Code:
                      if(...)
                      {
                      //blah blah blah
                      }
                      Let the debate begin!!!
                      I prefer the second layout but have worked with both. What you use however is not so important as making sure the coding standard for the project in question specifies a layout and everyone on the project sticking to it.

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Originally posted by AricC
                        Here is another debate what do you like:

                        intIndex++

                        Or

                        intIndex += 1

                        I prefer the +=

                        Aric
                        I don't think I care, but I use the ++ because it is 3 less characters to type.

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by AricC
                          Here is another debate what do you like:

                          intIndex++

                          Or

                          intIndex += 1

                          I prefer the +=

                          Aric
                          intIndex++ wherever I can use it. CFScript (Coldfusion scripting language) doesn't allow these so I'm forced to use intIndex = intIndex + 1 (so much typing!)

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Originally posted by RedSon
                            Where do you put it?

                            Like this
                            Code:
                            if (...){
                            //blah blah
                            }
                            or like this
                            Code:
                            if(...)
                            {
                            //blah blah blah
                            }
                            Let the debate begin!!!
                            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.

                            Comment

                            • Ganon11
                              Recognized Expert Specialist
                              • Oct 2006
                              • 3651

                              #15
                              My style:

                              Code:
                              if (<condition>) singleStatement;
                              if (<condition>) {
                                 state1;
                                 state2;
                                 state3;
                              } else if (<condition>) {
                                 state1;
                                 state2;
                              } else singleStatement;
                              
                              intIndex++;
                              I like saving the lines. Also, when scanning code, I can identify where each block of code begins/ends by the indenting involved. Finally,

                              Code:
                              }
                              else if (<condition>)
                              {
                              looks uglier and 'breaks up' the code more than

                              Code:
                              } else if (<condition>) {
                              ++ is just great.

                              Comment

                              Working...