Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Pet Peeve

Pet Peeve

Scheduled Pinned Locked Moved The Lounge
tutorialquestion
102 Posts 51 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Oleg A Lukin

    That works fine until you get this in the code after all :)

    if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
    	goto fail;
    if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    	goto fail;
    	goto fail;
    if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
    	goto fail;
    

    When I first saw that bug I got even more convinced see that my approach to braces everywhere as a must works better in the end.

    Banking establishments are more dangerous than standing armies. T.Jefferson

    P Offline
    P Offline
    Paulo Zemek
    wrote on last edited by
    #83

    I, for example, would never write the code like that. I can write an if without the {}, but I always put an extra line break after the call. And seeing two lines after the if simply looks wrong.

    if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
    goto fail;

    if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    goto fail;
    goto fail; // why the hell there's a line of code here? It is not important what it does, it looks wrong.

    if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
    goto fail;

    So, this kind of error is as ugly to me as this (in fact, even uglier as the excessive {} somewhat hide the problem):

    if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
    {
    goto fail;
    }

    if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    {
    goto fail;
    }
    goto fail; // Doesn't this look wrong?

    if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
    {
    goto fail;
    }

    But developers can also commit these errors (and by simply taking a looks without reading the code, I don't spot anything wrong):

    if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
    {
    goto fail;
    }

    if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    {
    goto fail;
    } // Oh, it looks like an else is missing...
    {
    goto fail;
    }

    if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
    {
    goto fail;
    }

    // So the code gets corrected to...
    if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
    {
    goto fail;
    }

    if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    {
    goto fail;
    }
    else
    {
    goto fail;
    }

    if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
    {
    goto fail;
    }

    O 1 Reply Last reply
    0
    • R R Giskard Reventlov

      Why are people so lazy? For example, how hard is it to

      if (condition)
      {
      DoThis();
      }
      else
      {
      DoThat();
      }

      as opposed to:

      if (condition)
      DoThis();
      else
      DoThat();

      Pedants :sigh:

      R Offline
      R Offline
      robertschoenstein
      wrote on last edited by
      #84

      I personally prefer the former of the two, for readability. I tend to "speed read" classes and utilize the curly brackets as logical breaks in the code. Basically, for me, it differentiates between code indentations and logic (if, foreach, for, etc.)

      ICP-Fan (The Keyboard Wielding Maniac)

      1 Reply Last reply
      0
      • R RJ Hatch

        Here's a Visual Studio trick: To debug the if, make sure the text cursor is somewhere in the if condition and press F9. To debug the method call after the if succeeded, make sure the text cursor is somewhere in the method call and press F9. Happy Debugging :)

        P Offline
        P Offline
        Paulo Zemek
        wrote on last edited by
        #85

        Imagine the if and the call are in the same line. Yet, the condition is false 99% of the time. Also, the method being called is called from many other places. If things are in two lines, simply put the breakpoint inside the if, not on the if line. If it is in the same line, you can't do that. Putting the breakpoint inside the method call will not help either, as it is called from many other places, so the best you can do is to create a conditional breakpoint. I prefer to have things in two lines and put the breakpoint exactly where it is needed.

        1 Reply Last reply
        0
        • B Brady Kelly

          I prefer

          if (something) DoAB();

          for the latter. I never use it, but have recently come across it. It seems more readable to me.

          No object is so beautiful that, under certain conditions, it will not look ugly. - Oscar Wilde

          S Offline
          S Offline
          StatementTerminator
          wrote on last edited by
          #86

          Brady Kelly wrote:

          I prefer

          if (something) DoAB();

          for the latter. I never use it, but have recently come across it. It seems more readable to me.

          Me too, if you must omit the braces then it's best to put it on one line. That way you don't have to worry about someone coming along later, not paying attention, and doing this:

          if (something)
          DoA();
          DoB();

          1 Reply Last reply
          0
          • P Paulo Zemek

            I agree with you. And I am actually the kind of person that when has to modify something like:

            if (something)
            {
            DoA();
            DoB();
            }

            To only call a DoAB(), I will go there and kill the extra { and }. So, I have more work doing that, but I keep consistency. So, it becomes:

            if (something)
            DoAB();

            S Offline
            S Offline
            StatementTerminator
            wrote on last edited by
            #87

            Paulo Zemek wrote:

            I agree with you. And I am actually the kind of person that when has to modify something like:

            if (something)
            {
            DoA();
            DoB();
            }

            To only call a DoAB(), I will go there and kill the extra { and }. So, I have more work doing that, but I keep consistency.
             
            So, it becomes:

            if (something)
            DoAB();

            Are you serious? You re-factor and combine methods just so you can avoid braces and use a dangerous bad practice?

            P 1 Reply Last reply
            0
            • S StatementTerminator

              Paulo Zemek wrote:

              I agree with you. And I am actually the kind of person that when has to modify something like:

              if (something)
              {
              DoA();
              DoB();
              }

              To only call a DoAB(), I will go there and kill the extra { and }. So, I have more work doing that, but I keep consistency.
               
              So, it becomes:

              if (something)
              DoAB();

              Are you serious? You re-factor and combine methods just so you can avoid braces and use a dangerous bad practice?

              P Offline
              P Offline
              Paulo Zemek
              wrote on last edited by
              #88

              No, I don't refactor and combine methods... I was only giving an example. What's more probably is that I extract an entire block (with many calls) into a single method. But for the example I used two calls.

              1 Reply Last reply
              0
              • P PIEBALDconsult

                :thumbsup: Quite. The maturity of a programming language may (in part) be calculated by how many newlines are required. A proper language requires none.

                S Offline
                S Offline
                StatementTerminator
                wrote on last edited by
                #89

                PIEBALDconsult wrote:

                Quite. The maturity of a programming language may (in part) be calculated by how many newlines are required. A proper language requires none.

                Just for making that statement, you should be forced to a maintain decade-old Perl CGI system.

                P 1 Reply Last reply
                0
                • J Jeremy Falcon

                  Ok, you have a good point with that, but it's still not worth the trade off of having really extra long files / routines. Besides, it gives you something to spot the new guys with so you can give them a hard time.

                  Jeremy Falcon

                  S Offline
                  S Offline
                  StatementTerminator
                  wrote on last edited by
                  #90

                  Jeremy Falcon wrote:

                  Ok, you have a good point with that, but it's still not worth the trade off of having really extra long files / routines. Besides, it gives you something to spot the new guys with so you can give them a hard time.

                  Who cares how long the files are? They aren't being printed on paper (hopefully), and scrolling is not so hard. Setting a trap for junior programmers that introduces a bug is not responsible programming. If you do that, you are responsible for the bug, not the newbie, and you should be "given a hard time" for compromising the code base just to be a jerk.

                  J 1 Reply Last reply
                  0
                  • J Jeremy Falcon

                    I'm not lazy, but I'll still use the latter. There's no point in always having to use a bracket so I don't, unless it makes things easier to read. Which in your example it doesn't. Wasting space just makes a source code file longer anyway and harder to navigate. Concise wins, unless it's hard to read.

                    Jeremy Falcon

                    R Offline
                    R Offline
                    ronchristie
                    wrote on last edited by
                    #91

                    I both agree and disagree. My personal coding style is:

                    if (expr) {
                    doSomething();
                    }
                    else {
                    doAnotherThing();
                    }

                    I like using the braces so that adding an extra bit of logic to the execution block won't inadvertently change the program flow, and at the same time placing the opening brace on the same line as the if... and the else statements compacts the code a bit. To me the advantage with compact code is that you can see more of the program logic in a single screen which means less time scrolling up and down trying to follow the program logic. For that reason, I'm ruthless in eliminating blank line whitespace except to visually offset functions or methods.

                    Ron Christie

                    J R 2 Replies Last reply
                    0
                    • S StatementTerminator

                      Jeremy Falcon wrote:

                      Ok, you have a good point with that, but it's still not worth the trade off of having really extra long files / routines. Besides, it gives you something to spot the new guys with so you can give them a hard time.

                      Who cares how long the files are? They aren't being printed on paper (hopefully), and scrolling is not so hard. Setting a trap for junior programmers that introduces a bug is not responsible programming. If you do that, you are responsible for the bug, not the newbie, and you should be "given a hard time" for compromising the code base just to be a jerk.

                      J Offline
                      J Offline
                      Jeremy Falcon
                      wrote on last edited by
                      #92

                      The trap isn't that hard to spot though. Seriously man, we're not talking about something difficult. It's just a peeve. We can debate, but that's all it is. Just a peeve. And my preference is that I'd rather see more of the file, because it's not that hard to spot.

                      Jeremy Falcon

                      1 Reply Last reply
                      0
                      • R ronchristie

                        I both agree and disagree. My personal coding style is:

                        if (expr) {
                        doSomething();
                        }
                        else {
                        doAnotherThing();
                        }

                        I like using the braces so that adding an extra bit of logic to the execution block won't inadvertently change the program flow, and at the same time placing the opening brace on the same line as the if... and the else statements compacts the code a bit. To me the advantage with compact code is that you can see more of the program logic in a single screen which means less time scrolling up and down trying to follow the program logic. For that reason, I'm ruthless in eliminating blank line whitespace except to visually offset functions or methods.

                        Ron Christie

                        J Offline
                        J Offline
                        Jeremy Falcon
                        wrote on last edited by
                        #93

                        As much as I'm not a fan of that (even in JavaScript), that makes more sense than:

                        if (expr)
                        {
                        doSomethingForOneLine();
                        }
                        else
                        {
                        doAnotherThingForOneLine();
                        }

                        You're version is at least concise, and no matter what some may say on CP, concise is important. Less is more. Always will be.

                        Jeremy Falcon

                        1 Reply Last reply
                        0
                        • S StatementTerminator

                          PIEBALDconsult wrote:

                          Quite. The maturity of a programming language may (in part) be calculated by how many newlines are required. A proper language requires none.

                          Just for making that statement, you should be forced to a maintain decade-old Perl CGI system.

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #94

                          Perl is a scripting language; not a programming language.

                          S 1 Reply Last reply
                          0
                          • P Paulo Zemek

                            I, for example, would never write the code like that. I can write an if without the {}, but I always put an extra line break after the call. And seeing two lines after the if simply looks wrong.

                            if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
                            goto fail;

                            if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
                            goto fail;
                            goto fail; // why the hell there's a line of code here? It is not important what it does, it looks wrong.

                            if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
                            goto fail;

                            So, this kind of error is as ugly to me as this (in fact, even uglier as the excessive {} somewhat hide the problem):

                            if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
                            {
                            goto fail;
                            }

                            if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
                            {
                            goto fail;
                            }
                            goto fail; // Doesn't this look wrong?

                            if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
                            {
                            goto fail;
                            }

                            But developers can also commit these errors (and by simply taking a looks without reading the code, I don't spot anything wrong):

                            if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
                            {
                            goto fail;
                            }

                            if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
                            {
                            goto fail;
                            } // Oh, it looks like an else is missing...
                            {
                            goto fail;
                            }

                            if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
                            {
                            goto fail;
                            }

                            // So the code gets corrected to...
                            if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
                            {
                            goto fail;
                            }

                            if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
                            {
                            goto fail;
                            }
                            else
                            {
                            goto fail;
                            }

                            if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
                            {
                            goto fail;
                            }

                            O Offline
                            O Offline
                            Oleg A Lukin
                            wrote on last edited by
                            #95

                            Well, I won't write anything such as that either. But since this a real world example of infamous 'goto fail' bug obviously other people do. In general I'd say that none of your samples with braces would've passed my code review, but I admit your first sample should attract review attention as well, it'll probably will attract enough attention from any reviewer. Still I'd say that with braces style the code like that most probably would not leave the developer and will be fixed before review

                            Banking establishments are more dangerous than standing armies. T.Jefferson

                            1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              Perl is a scripting language; not a programming language.

                              S Offline
                              S Offline
                              StatementTerminator
                              wrote on last edited by
                              #96

                              Yeah true, you really need to be pedantic about a joke?

                              P 1 Reply Last reply
                              0
                              • L Lost User

                                Do you really require 8 lines to convey 4 lines worth of information? Perhaps "begin" and "end" would be even more clear? How about "then"? :)

                                if (condition) then
                                begin
                                DoThis();
                                end
                                else
                                begin
                                DoThat();
                                end;

                                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                                S Offline
                                S Offline
                                StatementTerminator
                                wrote on last edited by
                                #97

                                LOL, that looks exactly like T-SQL :)

                                1 Reply Last reply
                                0
                                • S StatementTerminator

                                  Yeah true, you really need to be pedantic about a joke?

                                  P Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #98

                                  No, but I really need to be demeaning of Perl. X|

                                  1 Reply Last reply
                                  0
                                  • R ronchristie

                                    I both agree and disagree. My personal coding style is:

                                    if (expr) {
                                    doSomething();
                                    }
                                    else {
                                    doAnotherThing();
                                    }

                                    I like using the braces so that adding an extra bit of logic to the execution block won't inadvertently change the program flow, and at the same time placing the opening brace on the same line as the if... and the else statements compacts the code a bit. To me the advantage with compact code is that you can see more of the program logic in a single screen which means less time scrolling up and down trying to follow the program logic. For that reason, I'm ruthless in eliminating blank line whitespace except to visually offset functions or methods.

                                    Ron Christie

                                    R Offline
                                    R Offline
                                    rjmoses
                                    wrote on last edited by
                                    #99

                                    I personally prefer this convention: if ((a cond b) conjunction (c cond d) conjunction // Additional conditions always on separate line (e cond f)) { // Always include opening brace do .... something } // Always closing brace Or } else { // Always closing brace..else..opening brace do .... something else } // Always closing brace The same conventions and principles apply to loops (for, while, etc.) and other conditionals. I strictly avoid NOT using braces under any circumstances. I want to be able to see a complete thought, i.e., a block of code, in one intact, obvious section. These are my thoughts based on years of chasing bugs, modifying other people's code, and having to debug somebody else's problem. Other people are welcome to use their own conventions, but I may not be able to help them efficiently. Just because I can do something doesn't mean I should do it.

                                    R 1 Reply Last reply
                                    0
                                    • R R Giskard Reventlov

                                      Why are people so lazy? For example, how hard is it to

                                      if (condition)
                                      {
                                      DoThis();
                                      }
                                      else
                                      {
                                      DoThat();
                                      }

                                      as opposed to:

                                      if (condition)
                                      DoThis();
                                      else
                                      DoThat();

                                      Pedants :sigh:

                                      J Offline
                                      J Offline
                                      jschell
                                      wrote on last edited by
                                      #100

                                      Karel Čapek wrote:

                                      For example, how hard is it to

                                      Both are probably less hard than complaining about it. But other than that since it is legitimate syntax then it is legitimate syntax and nothing but a preference after that. Naturally if that is the most important problem facing you day to day or even one that even rises above the noise level in terms of problems you must work at an exceeding stellar place. I once worked a a place where the VP (most senior person in the entire site) would regularly take down the database through incorrect usage which would shut down the call center (several hundred employees) completely. Now that is a problem.

                                      1 Reply Last reply
                                      0
                                      • R rjmoses

                                        I personally prefer this convention: if ((a cond b) conjunction (c cond d) conjunction // Additional conditions always on separate line (e cond f)) { // Always include opening brace do .... something } // Always closing brace Or } else { // Always closing brace..else..opening brace do .... something else } // Always closing brace The same conventions and principles apply to loops (for, while, etc.) and other conditionals. I strictly avoid NOT using braces under any circumstances. I want to be able to see a complete thought, i.e., a block of code, in one intact, obvious section. These are my thoughts based on years of chasing bugs, modifying other people's code, and having to debug somebody else's problem. Other people are welcome to use their own conventions, but I may not be able to help them efficiently. Just because I can do something doesn't mean I should do it.

                                        R Offline
                                        R Offline
                                        ronchristie
                                        wrote on last edited by
                                        #101

                                        It's interesting that the older the programmer the more concision becomes important. :)

                                        Ron Christie

                                        1 Reply Last reply
                                        0
                                        • R R Giskard Reventlov

                                          Why are people so lazy? For example, how hard is it to

                                          if (condition)
                                          {
                                          DoThis();
                                          }
                                          else
                                          {
                                          DoThat();
                                          }

                                          as opposed to:

                                          if (condition)
                                          DoThis();
                                          else
                                          DoThat();

                                          Pedants :sigh:

                                          A Offline
                                          A Offline
                                          Al Chak
                                          wrote on last edited by
                                          #102

                                          I have more

                                          (condition)?DoThis():DoThat();

                                          DoThese[(condition)]();

                                          #define CALL_FUNC(__F) Do##__F
                                          CALL_FUNC(condition);

                                          do
                                          {
                                          if(condition)
                                          {
                                          DoThis();
                                          break;
                                          }
                                          } while(DoThat()&&condition);

                                          <pre lang="cs">
                                          switch(condition)
                                          {
                                          case 1:
                                          DoThis();
                                          break;
                                          default:
                                          DoThat();
                                          }

                                          :laugh:

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups