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. Other Discussions
  3. The Weird and The Wonderful
  4. To me this is a coding horror, and to you? [modified]

To me this is a coding horror, and to you? [modified]

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
84 Posts 26 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.
  • G geoffs

    First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:

    m\_boolVar = (intVar == 0 ? false : true) ;
    

    Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:

    m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
    

    ...or if I was feeling in a bit more perverse mood:

    m\_boolVar = !!intVar;
    

    There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.

    modified on Tuesday, September 16, 2008 3:02 AM

    N Offline
    N Offline
    Nemanja Trifunovic
    wrote on last edited by
    #31

    geoffs wrote:

    but it is a coding horror for my programming style and I am curious as to what the others here think about it.

    As you said - it is a horror for your programming style. Different people use different styles, and as long as the code is correct and reasonably readable there is no point in discussing it.

    Programming Blog utf8-cpp

    C 1 Reply Last reply
    0
    • G geoffs

      First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:

      m\_boolVar = (intVar == 0 ? false : true) ;
      

      Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:

      m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
      

      ...or if I was feeling in a bit more perverse mood:

      m\_boolVar = !!intVar;
      

      There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.

      modified on Tuesday, September 16, 2008 3:02 AM

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #32

      It's questionable. Depends on how much readability is necessary.

      "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

      1 Reply Last reply
      0
      • G geoffs

        First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:

        m\_boolVar = (intVar == 0 ? false : true) ;
        

        Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:

        m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
        

        ...or if I was feeling in a bit more perverse mood:

        m\_boolVar = !!intVar;
        

        There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.

        modified on Tuesday, September 16, 2008 3:02 AM

        R Offline
        R Offline
        realJSOP
        wrote on last edited by
        #33

        I would have scoped it:

        m_boolVar = (intVar != 0);

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        C 1 Reply Last reply
        0
        • G geoffs

          First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:

          m\_boolVar = (intVar == 0 ? false : true) ;
          

          Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:

          m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
          

          ...or if I was feeling in a bit more perverse mood:

          m\_boolVar = !!intVar;
          

          There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.

          modified on Tuesday, September 16, 2008 3:02 AM

          D Offline
          D Offline
          dighn
          wrote on last edited by
          #34

          I think it's too trivial to fuss over.

          C 1 Reply Last reply
          0
          • R Rage

            For embedded people, this is the misra conform code :

            if (0!=intVar)
            m_boolVar=true;
            else
            m_boolVar=false;

            Your first example breaks at least three rules.

            G Offline
            G Offline
            geoffs
            wrote on last edited by
            #35

            Which example are you pointing at? The original line of code that I didn't like, or my preferred coding style? Also, which MISRA guidelines are you utilizing... MISCRA-C 1998, MISRA-C 2004, MISRA-C++?

            R 1 Reply Last reply
            0
            • G geoffs

              First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:

              m\_boolVar = (intVar == 0 ? false : true) ;
              

              Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:

              m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
              

              ...or if I was feeling in a bit more perverse mood:

              m\_boolVar = !!intVar;
              

              There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.

              modified on Tuesday, September 16, 2008 3:02 AM

              S Offline
              S Offline
              Swinefeaster
              wrote on last edited by
              #36

              i would go with:

              m_boolVar = intVar ? true : false;

              K C 2 Replies Last reply
              0
              • G geoffs

                I would have no problems with how you coded that up.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #37

                Thanks :) In any case, a simple method like

                public static bool IsError(int ResultValue)
                {
                // 0 = success
                // 1 = some error
                // 2 = serious error
                // 3 = ....
                return ResultValue != 0;
                }

                will do the trick nicely. Such methods may appear a bit ridiculous as well, but actually the compiler should inline them when optimizing the code. And in cases when the return values represent not only success or failure, but also warnings, correctable errors, critical errors, fatal errors and who knows what else, a method to classify the result is much better than dealing with this redundantly in the code every time it's needed.

                A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                1 Reply Last reply
                0
                • G geoffs

                  First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:

                  m\_boolVar = (intVar == 0 ? false : true) ;
                  

                  Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:

                  m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
                  

                  ...or if I was feeling in a bit more perverse mood:

                  m\_boolVar = !!intVar;
                  

                  There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.

                  modified on Tuesday, September 16, 2008 3:02 AM

                  K Offline
                  K Offline
                  Kevin McFarlane
                  wrote on last edited by
                  #38

                  I use the style of your second example though usually with brackets around the expression. m_boolVar = (intVar != 0); However, recently I've been moving towards dispensing with the brackets too, partly prompted by my refactoring tool.

                  Kevin

                  1 Reply Last reply
                  0
                  • G geoffs

                    That's what people are always saying and perhaps rightfully so. However, coming from a background of programming stemming from the mid-70's when I had 4KB of memory to program with and compilers that weren't as optimizing as today's, conciseness was a virtue. After so many years it has become a habit but hopefully not to the point where I am so concise that I generate obfuscated code. For me, verbose code is actually painful to read so maybe it works both ways. And maybe there is no absolute wrong or right in this case either...

                    K Offline
                    K Offline
                    Kevin McFarlane
                    wrote on last edited by
                    #39

                    geoffs wrote:

                    For me, verbose code is actually painful to read

                    It's a question of striking the right balance. I often find that too many parentheses makes code harder to read in cases where removing those parentheses would cause no human ambiguity. e.g., I prefer if (a == b || c == d) to if ((a == b) || (c == d)) Similarly I prefer return a == b; to return (a == b);

                    Kevin

                    1 Reply Last reply
                    0
                    • S Swinefeaster

                      i would go with:

                      m_boolVar = intVar ? true : false;

                      K Offline
                      K Offline
                      Kevin McFarlane
                      wrote on last edited by
                      #40

                      That would be invalid in C# or Java. Even in C/C++ I would always query against a Boolean expression for conceptual clarity.

                      Kevin

                      1 Reply Last reply
                      0
                      • G geoffs

                        First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:

                        m\_boolVar = (intVar == 0 ? false : true) ;
                        

                        Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:

                        m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
                        

                        ...or if I was feeling in a bit more perverse mood:

                        m\_boolVar = !!intVar;
                        

                        There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.

                        modified on Tuesday, September 16, 2008 3:02 AM

                        A Offline
                        A Offline
                        Andrew Torrance
                        wrote on last edited by
                        #41

                        I prefer your coworkers style . In terms of efficiency i doubt if there is any significant difference unless this is getting called repeatedly , and it is much much easier to read and understand than m_boolVar = intVar != 0; as this requires you to think about precedence. Just becasue you understand it does not mean that the maintenance programmer will . But at the end of the day its a style question , and nothing divides programmers more than that .

                        C 1 Reply Last reply
                        0
                        • G geoffs

                          First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:

                          m\_boolVar = (intVar == 0 ? false : true) ;
                          

                          Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:

                          m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
                          

                          ...or if I was feeling in a bit more perverse mood:

                          m\_boolVar = !!intVar;
                          

                          There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.

                          modified on Tuesday, September 16, 2008 3:02 AM

                          G Offline
                          G Offline
                          geoffs
                          wrote on last edited by
                          #42

                          Now that I've gotten this off of my chest, I can admit to myself that it really was more of a style issue rather than a coding horror as modern compilers would probably generate similar, if not same, code for any of the alternatives. I probably should have posted it to Soap Box or Lounge, but it is what it is at this point. Thanks for all of your inputs. Some people agreed, some did not, and yet others were bored by the whole thing (in which case why did they even bother to reply?). Meanwhile, my style is best... :doh: ;)

                          C 1 Reply Last reply
                          0
                          • G geoffs

                            First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:

                            m\_boolVar = (intVar == 0 ? false : true) ;
                            

                            Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:

                            m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
                            

                            ...or if I was feeling in a bit more perverse mood:

                            m\_boolVar = !!intVar;
                            

                            There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.

                            modified on Tuesday, September 16, 2008 3:02 AM

                            L Offline
                            L Offline
                            leonej_dt
                            wrote on last edited by
                            #43

                            geoffs wrote:

                            m_boolVar = !!intVar;

                            What is so perverse with that?

                            To those who understand, I extend my hand. To the doubtful I demand: Take me as I am. Not under your command, I know where I stand. I won't change to fit yout plan. Take me as I am.

                            G C 2 Replies Last reply
                            0
                            • G geoffs

                              Which example are you pointing at? The original line of code that I didn't like, or my preferred coding style? Also, which MISRA guidelines are you utilizing... MISCRA-C 1998, MISRA-C 2004, MISRA-C++?

                              R Offline
                              R Offline
                              Rage
                              wrote on last edited by
                              #44

                              The first line. Your favorite style is worse, since you are using implicit casting and poor maintainable conventions. I refer to MISRA-C 2004.

                              G 1 Reply Last reply
                              0
                              • L leonej_dt

                                geoffs wrote:

                                m_boolVar = !!intVar;

                                What is so perverse with that?

                                To those who understand, I extend my hand. To the doubtful I demand: Take me as I am. Not under your command, I know where I stand. I won't change to fit yout plan. Take me as I am.

                                G Offline
                                G Offline
                                geoffs
                                wrote on last edited by
                                #45

                                :laugh: Ah, a man after my own heart! It is not perverse to me. In fact, I am quite at home with that syntax and like it. It is perverse because so many of my fellow programmers hate it and my use of it makes them froth at the mouth or sit there with a dumbfounded look because they don't understand what it is doing.

                                L 1 Reply Last reply
                                0
                                • R Rage

                                  The first line. Your favorite style is worse, since you are using implicit casting and poor maintainable conventions. I refer to MISRA-C 2004.

                                  G Offline
                                  G Offline
                                  geoffs
                                  wrote on last edited by
                                  #46

                                  If you are referring to the line:

                                  boolVar = intVar != 0;

                                  ...then the folks at MISRA specifically allow for such usage within their MISRA-C 2004 guidelines under TC1. For further clarification, please refer to: Rule 12.6, effectively Boolean expressions in assignment?[^]

                                  1 Reply Last reply
                                  0
                                  • G geoffs

                                    Thank you... for explaining in better terms what I was feeling when I made the original post.

                                    G Offline
                                    G Offline
                                    geoffs
                                    wrote on last edited by
                                    #47

                                    Amazing - CP is apparently not much different from Usenet. No matter how inoffensive a statement might be, you find someone who will take offense. How in heck can what I have stated in the previous message have caused someone to vote it down? :confused:

                                    D 1 Reply Last reply
                                    0
                                    • G geoffs

                                      :laugh: Ah, a man after my own heart! It is not perverse to me. In fact, I am quite at home with that syntax and like it. It is perverse because so many of my fellow programmers hate it and my use of it makes them froth at the mouth or sit there with a dumbfounded look because they don't understand what it is doing.

                                      L Offline
                                      L Offline
                                      leonej_dt
                                      wrote on last edited by
                                      #48

                                      geoffs wrote:

                                      because they don't understand what it is doing

                                      I hate people who use C to program in Java.

                                      To those who understand, I extend my hand. To the doubtful I demand: Take me as I am. Not under your command, I know where I stand. I won't change to fit yout plan. Take me as I am.

                                      1 Reply Last reply
                                      0
                                      • G geoffs

                                        Amazing - CP is apparently not much different from Usenet. No matter how inoffensive a statement might be, you find someone who will take offense. How in heck can what I have stated in the previous message have caused someone to vote it down? :confused:

                                        D Offline
                                        D Offline
                                        Dan Neely
                                        wrote on last edited by
                                        #49

                                        Let me get this right, you're asking about how stupidity can exist on the internet. The only type of question more likely to get a yes answer than 'can there be anyone on the web that stupid/ignorant/obnoxious' is 'can there be anything on the web that gross/perverse/obscene'.

                                        Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                                        D 1 Reply Last reply
                                        0
                                        • G geoffs

                                          First, let me say that the code excerpt below is not an egregious violation (and maybe not a violation at all), but it is a coding horror for my programming style and I am curious as to what the others here think about it. So, in reviewing a coworker's code I come across the following line:

                                          m\_boolVar = (intVar == 0 ? false : true) ;
                                          

                                          Yes, parenthesization and spacing exactly as shown above. Were it my code, it would have been written as:

                                          m\_boolVar = intVar != 0; // (corrected from == 0 by GDavy -- I typed too fast!
                                          

                                          ...or if I was feeling in a bit more perverse mood:

                                          m\_boolVar = !!intVar;
                                          

                                          There were much bigger fish to fry in this code, but there are times when I just can't let things like this go by. These things are like misspelled words that shout out at me from among the surrounding text.

                                          modified on Tuesday, September 16, 2008 3:02 AM

                                          D Offline
                                          D Offline
                                          David St Hilaire
                                          wrote on last edited by
                                          #50

                                          I see a lot of:

                                          if (b1)
                                          b2 = true;
                                          else
                                          b2 = false;

                                          and even some:

                                          switch (b1)
                                          {
                                          case true:
                                          b2 = true;
                                          break;
                                          case false:
                                          b2 = false;
                                          break;
                                          }

                                          C 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