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.
  • J JeremyBob

    condition
    ? DoThis()
    : DoThat();

    And now? All in all none of this really matters. As long as everyone on the project sticks to a predetermined coding standard, then the format doesn't matter as much, as long as its consistent. Consistency in code can resolve a huge amount of misunderstanding.

    D Offline
    D Offline
    DJ van Wyk
    wrote on last edited by
    #59

    Standard? STANDARD? What's that??? I found that we do keep to standards whenever all the other developers code exactly the same way as I do. ;P

    My plan is to live forever ... so far so good

    1 Reply Last reply
    0
    • I Ian Shlasko

      That's not good enough...

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

      It's not properly formatted until there's only one token per line. :)

      Proud to have finally moved to the A-Ark. Which one are you in?
      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

      M Offline
      M Offline
      Matthys Terblanche
      wrote on last edited by
      #60

      Totally agree...but for a very spesific reason. I'm using a Braille display with a maximum of 40 chars per line, so it have a influence on my format preferences... ;P Seriously though, if (condition) { Action1(); } else { Action2(); } is my real preference, and there's a plus to that, whenever the situation change to have more than one statement for the if test, the person who has to change it doesn't have to remember to go and fill in the {}.:cool:

      1 Reply Last reply
      0
      • P Paulo Zemek

        I understand... and I can't say I disagree... that arrives to the point of preference, not to the point of usefulness. But one thing I never do is 2 (or more) real calls in the same line: SomeObject.DoCallOne().DoCall2(otherObject.AnotherCallWithAResult(), evenAnotherObject.WithEvenAnExtraCall(), theFinal.ObjectWithTheFinalCall());

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

        Just your contrived example hurts my eyes. I'd hate to see that in real life. :~

        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

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

          X Offline
          X Offline
          xiecsuk
          wrote on last edited by
          #62

          Of course it is, or at least, it was. That's what we used to do when programming in APL. There were execution costs for each new line you used.

          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
            Jorgen Andersson
            wrote on last edited by
            #63

            I don't care very much as long as it's consistent. But since you can't make multiple statements without braces...

            Wrong is evil and must be defeated. - Jeff Ello (√-shit)2

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

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

              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 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
                Johnny J
                wrote on last edited by
                #65

                Either one of those suits me fine. The ones I can't stand are these:

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

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

                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
                • R R Giskard Reventlov

                  Mladen Janković wrote:

                  it can make you look obnoxious.

                  But I am obnoxious

                  M Offline
                  M Offline
                  Mladen Jankovic
                  wrote on last edited by
                  #66

                  Well I can't argue with that :)

                  Commodore 64 emulator for Windows Phone

                  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
                    Jacquers
                    wrote on last edited by
                    #67

                    And then there is the choice between: if (condition) vs if (condition == true) if (!condition) vs if (condition == false) I sometimes put in the == false part because the ! is easy to miss when reading.

                    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:

                      F Offline
                      F Offline
                      Fabio Franco
                      wrote on last edited by
                      #68

                      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 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

                        D Offline
                        D Offline
                        Daniel Vaughan
                        wrote on last edited by
                        #69

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