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.
  • D Daniel Vaughan

    It's slower to read an if statement with code on the same line because your eyes have to track to the right. It's the same reason why you shouldn't place form titles and fields on the same line. Here's some articles on it: http://www.uxmatters.com/mt/archives/2006/07/label-placement-in-forms.php[^] http://uxmag.com/articles/eye-tracking-and-web-usability-a-good-fit[^]

    Daniel Vaughan Twitter | Blog | Microsoft MVP | Projects: Calcium SDK, Clog | LinkedIn

    B Offline
    B Offline
    Brady Kelly
    wrote on last edited by
    #70

    Cool, thanks. I've bookmarked those.

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

    1 Reply Last reply
    0
    • Sander RosselS Sander Rossel

      Sure it's easy, until another developer adds a second line and forgets to add brackets. I've been working in some source code that didn't use brackets for single statements. I introduced a few bugs by not adding them when I had to and I've been wondering more than once if the original developer REALLY meant not to add brackets....

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

      is really weird to look at and at the very least makes you wonder if it was intended... Especially if DoAnotherThing(); isn't properly in/outdented!

      My blog[^]

      public class SanderRossel : Lazy<Person>
      {
      public void DoWork()
      {
      throw new NotSupportedException();
      }
      }

      C Offline
      C Offline
      Cloud William
      wrote on last edited by
      #71

      I am currently working on a system where most of the software was written in C in the early 1980s. It is formatted so badly that stuff like

       if (condition)
      DoThis();
      

      else DoThat();
      DoAnotherThing();

      is not uncommon at all. Add to that, there is no consistent use of tabs vs. spaces, no consistent tab stops, no naming conventions. Honestly, use the braces or don't, just be consistent, dammit. Proper indentation is far more valuable than brace usage; don't use TAB for indentation, and do stuff the same way, every damn time.

      "The only thing a free man can be forced to do is die." "The right to defend oneself and ones neighbors is the beginning of freedom." "Freedom? That is a worship word. . ." -- Cloud William "It is our worship word, too." -- James T. Kirk

      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:

        V Offline
        V Offline
        Vark111
        wrote on last edited by
        #72

        I only remove braces on single-line method preconditions at the beginning of a method:

        if (arg==null) throw new ArgumentNullException("arg", arg);

        Everywhere else I put braces.

        J 1 Reply Last reply
        0
        • Sander RosselS Sander Rossel

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

          You probably meant

          Ian Shlasko wrote:

          It's not properly formatted until there's only one token per line in the entire application.

          I guess it's theoretically possible... :~

          My blog[^]

          public class SanderRossel : Lazy<Person>
          {
          public void DoWork()
          {
          throw new NotSupportedException();
          }
          }

          J Offline
          J Offline
          Jim from Indy
          wrote on last edited by
          #73

          You guys need to investigate APL, a language described by one of my fellow students as "the ultimate in elegance"

          Jim

          1 Reply Last reply
          0
          • V Vark111

            I only remove braces on single-line method preconditions at the beginning of a method:

            if (arg==null) throw new ArgumentNullException("arg", arg);

            Everywhere else I put braces.

            J Offline
            J Offline
            Johnny J
            wrote on last edited by
            #74

            Vark111 wrote:

            if (arg==null) throw new ArgumentNullException("arg", arg);

            Let me guess - You're used to dealing with Pirated software??? ;)

            Anything that is unrelated to elephants is irrelephant
            Anonymous
            -----
            The problem with quotes on the internet is that you can never tell if they're genuine
            Winston Churchill, 1944
            -----
            I'd just like a chance to prove that money can't make me happy.
            Me, all the time

            1 Reply Last reply
            0
            • Sander RosselS Sander Rossel

              And why couldn't you write

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

              Now this code is totally unambiguous :) Although I know some people who really hate this...

              My blog[^]

              public class SanderRossel : Lazy<Person>
              {
              public void DoWork()
              {
              throw new NotSupportedException();
              }
              }

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

              Too much whitespace/not enough whitespace. The "Great Debate" goes on. Some programmers get paid by LOC, so they're induced to spread things out. Some programmers like to show how smart they are, so they try to condense everything to one line of cryptic functions. Other programmers heard "this" is the right way to do it. For me, the key is "consistency". I want my code and all my programmers' code to look identical. It should be totally transparent who wrote the code. And the code needs to be clear, quickly comprehensible and understandable, yet concise. I don't want to spend my time figuring out what John Doe was doing and maybe miss some key element when I'm under pressure to fix a problem or develop a new capability. I tell my programmers that "I was around long before you came and I will be around long after you leave. Do it my way because I will have to maintain it later." I just had to work on a one-liner script from another company that incorporated at least 12 function calls. Took me the better part of a week to figure out where the bug was. Yes it looked slick, but a week wasted is two weeks lost. That's my thoughts--clear, comprehensible, concise.

              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();

                T Offline
                T Offline
                tom1443
                wrote on last edited by
                #76

                I don't care what way you do it as long as it is not: if (something) { DoA() } Yuck! But seriously - if the biggest problem your project has is where people put or don't put the braces you are doing pretty good.

                1 Reply Last reply
                0
                • C Chris Losinger

                  but that's the start of the problem! someday, someone is going to have to come along and add some more logic in that if, and you've just forced them to do the work you should've done the first time! IMO

                  image processing toolkits | batch image processing

                  A Offline
                  A Offline
                  agolddog
                  wrote on last edited by
                  #77

                  Correct. If you have a 'rule' which says, "(don't) use braces, except in these special cases", then you have to remember the special cases. I'm in the braces-good camp for the reasons Chris supplied above. Missing them doesn't make your code noticeably more concise, and does make your code (and, more importantly, your intentions, when you've run away from this project and I'm trying to figure it out [or vice-versa]) noticeably more readable.

                  1 Reply Last reply
                  0
                  • A Andy Brummer

                    The rule that I have is that you omit the braces iff the statement is a simple single line. Also, if either branch needs braces then both get them, and loops always have braces.

                    Curvature of the Mind now with 3D

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

                    You beat me to it man. If it's hard to read then I use them. For simple things concise wins. +5

                    Jeremy Falcon

                    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

                      D Offline
                      D Offline
                      DenverEd
                      wrote on last edited by
                      #79

                      It's 2014, who cares how long the code file is. It's easier to read with the extra {}, it's easier to maintain with the extra {}. The only downside is typing extra {}. Don't you get paid per line anyway??

                      J 1 Reply Last reply
                      0
                      • D DenverEd

                        It's 2014, who cares how long the code file is. It's easier to read with the extra {}, it's easier to maintain with the extra {}. The only downside is typing extra {}. Don't you get paid per line anyway??

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

                        I care. Being able to see more at a glance is nice. It's also less convoluted when it's a simple statement. And I don't get paid per line, nobody does. I get paid to deliver a product that's useful. Nobody cares about line count except other nerds / devs.

                        Jeremy Falcon

                        1 Reply Last reply
                        0
                        • F Fabio Franco

                          Karel Čapek wrote:

                          Why are people so lazy?

                          Did it occur to you that it may be about style and not laziness? Some people can have even another style:

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

                          To alcohol! The cause of, and solution to, all of life's problems - Homer Simpson ---- Our heads are round so our thoughts can change direction - Francis Picabia

                          R Offline
                          R Offline
                          R Giskard Reventlov
                          wrote on last edited by
                          #81

                          Fabio Franco wrote:

                          Did it occur to you that it may be about style and not laziness?

                          No: people who do this tend to shortcut other things (sweeping generalization and/or observation over many years of being a coder) or crush the code into as small a space as possible making it hard to read. The point is really to have consistency of style and format in the code: braces make it easier to read. Consider this:

                          int a = 0;
                          string bean = "green";
                          float golden= 1.618;
                          if (condition)
                          {
                          x = 42;
                          bean = "eatme";
                          golden = a * pi;
                          }
                          else
                          a = 1;
                          bean = 'foo';
                          golden = a - x;

                          I find that style difficult to read and adding the braces would make it much easier to see the flow. Oh, and then there are tabs v spaces... :-)

                          1 Reply Last reply
                          0
                          • P Paulo Zemek

                            I don't do if() and the code in the same line. It is more a debug stuff. If I want to debug the if itself I put the breakpoint in that line. If I only want to debug the method call, after the if succeeded, I put the breakpoint in the call line.

                            R Offline
                            R Offline
                            RJ Hatch
                            wrote on last edited by
                            #82

                            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 1 Reply Last reply
                            0
                            • 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
                                          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