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 Offline
    G Offline
    geoffs
    wrote on last edited by
    #1

    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 M E N R 20 Replies 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
      GDavy
      wrote on last edited by
      #2

      Uhm I think your code would have a bug.... You better write it this way:

      m_boolVar = intVar != 0;

      G 1 Reply Last reply
      0
      • G GDavy

        Uhm I think your code would have a bug.... You better write it this way:

        m_boolVar = intVar != 0;

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

        Yeah, thanks, I typed too fast. Sometimes (as you get older) what you are thinking doesn't quite translate into what you are typing! :sigh:

        G Q 2 Replies 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

          M Offline
          M Offline
          Megidolaon
          wrote on last edited by
          #4

          Your code is shorter, but the other one is easier to read. Your code is still pretty easy to read but I think readability is more important than conciseness. When you look at someone's or even your own code after a while (to find a bug or whatever), it's best if you can easily read and immediately grasp the function of the code. If everything is as concise as possibly, you often have hard to read code, which takes much longer to comprehend. So, writing easy to read code will save you time later.

          G D D C 4 Replies 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

            E Offline
            E Offline
            Eslam Afifi
            wrote on last edited by
            #5

            geoffs wrote:

            m_boolVar == !!intVar;

            And this one

            m_boolVar = !intVar;

            Although ! is shorter, I prefer to use the != way because it's clearer.

            Eslam Afifi

            G 1 Reply Last reply
            0
            • M Megidolaon

              Your code is shorter, but the other one is easier to read. Your code is still pretty easy to read but I think readability is more important than conciseness. When you look at someone's or even your own code after a while (to find a bug or whatever), it's best if you can easily read and immediately grasp the function of the code. If everything is as concise as possibly, you often have hard to read code, which takes much longer to comprehend. So, writing easy to read code will save you time later.

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

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

              L K M 3 Replies Last reply
              0
              • E Eslam Afifi

                geoffs wrote:

                m_boolVar == !!intVar;

                And this one

                m_boolVar = !intVar;

                Although ! is shorter, I prefer to use the != way because it's clearer.

                Eslam Afifi

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

                The second example was only if I was feeling in a perverse mood. :-D However, like GDavy, you caught me in another instance of fingers not doing what my mind was thinking. I meant to type '=' and not '=='. I obviously posted this way too late in the evening (almost midnight my time). I corrected this in the original. Also, you cannot use just one logical negation because we want the receiving bool variable to receive true if the int variable is non-zero. One negation reverses the true/false sense which, for this example, means that m_boolVar becomes true if intVar is 0 and false otherwise. That is not what was desired. Time to go to sleep... :zzz:

                E 1 Reply Last reply
                0
                • G geoffs

                  The second example was only if I was feeling in a perverse mood. :-D However, like GDavy, you caught me in another instance of fingers not doing what my mind was thinking. I meant to type '=' and not '=='. I obviously posted this way too late in the evening (almost midnight my time). I corrected this in the original. Also, you cannot use just one logical negation because we want the receiving bool variable to receive true if the int variable is non-zero. One negation reverses the true/false sense which, for this example, means that m_boolVar becomes true if intVar is 0 and false otherwise. That is not what was desired. Time to go to sleep... :zzz:

                  E Offline
                  E Offline
                  Eslam Afifi
                  wrote on last edited by
                  #8

                  Oops :doh:, you're right. Sorry. It's 9:12 am here and I'm still awake. I think I should sleep :zzz:.

                  Eslam Afifi

                  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

                    N Offline
                    N Offline
                    Nagy Vilmos
                    wrote on last edited by
                    #9

                    For me

                    geoffs wrote:

                    m_boolVar = intVar != 0;

                    is the bestest style. Generally != is a better option then ==.


                    Panic, Chaos, Destruction. My work here is done.

                    G 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
                      RugbyLeague
                      wrote on last edited by
                      #10

                      It's just different styles of programming rather than a coding horror.

                      G C 2 Replies 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

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #11

                        Not a horror, in my opinon. :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        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
                          darkelv
                          wrote on last edited by
                          #12

                          If you had typed to fast and made a mistake, the reader might have read too fast and made a mistake too. Yours is more "horror" than the first example, IMO.

                          G 1 Reply Last reply
                          0
                          • M Megidolaon

                            Your code is shorter, but the other one is easier to read. Your code is still pretty easy to read but I think readability is more important than conciseness. When you look at someone's or even your own code after a while (to find a bug or whatever), it's best if you can easily read and immediately grasp the function of the code. If everything is as concise as possibly, you often have hard to read code, which takes much longer to comprehend. So, writing easy to read code will save you time later.

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

                            Megidolaon wrote:

                            Your code is shorter, but the other one is easier to read.

                            I disagree, The simple != is more immediately clear than the trinary expression. The latter takes longer to read, and the unnecessary verbosity triggers a doublecheck to make sure I didn't misread something making it even worse.

                            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

                            G 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
                              dojohansen
                              wrote on last edited by
                              #14

                              Totally agree - it *is* a coding horror. Anyone who uses a ternary of type boolean is a twit.

                              G 1 Reply Last reply
                              0
                              • M Megidolaon

                                Your code is shorter, but the other one is easier to read. Your code is still pretty easy to read but I think readability is more important than conciseness. When you look at someone's or even your own code after a while (to find a bug or whatever), it's best if you can easily read and immediately grasp the function of the code. If everything is as concise as possibly, you often have hard to read code, which takes much longer to comprehend. So, writing easy to read code will save you time later.

                                D Offline
                                D Offline
                                dojohansen
                                wrote on last edited by
                                #15

                                Megidolaon wrote:

                                Your code is shorter, but the other one is easier to read.

                                How on earth can it possibly be easier to read??? His code is a subset of the code you claim is "easier to read". In order to understand the ternary you need to understand the first expression (the condition), as well as the ternary itself, as well as the assignment operator. His code makes for less to read, and is clearer (one less step in deducing what value the variable ends up having under various conditions). Anyone who uses a ternary of type boolean is a twit!

                                G M 2 Replies Last reply
                                0
                                • D Dan Neely

                                  Megidolaon wrote:

                                  Your code is shorter, but the other one is easier to read.

                                  I disagree, The simple != is more immediately clear than the trinary expression. The latter takes longer to read, and the unnecessary verbosity triggers a doublecheck to make sure I didn't misread something making it even worse.

                                  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

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

                                  Exactly. If a programmer is conversant in c/c++, and where I work they wouldn't have gotten hired if they weren't, the != version is both intuitive and easier to read (IMO).

                                  1 Reply Last reply
                                  0
                                  • D dojohansen

                                    Megidolaon wrote:

                                    Your code is shorter, but the other one is easier to read.

                                    How on earth can it possibly be easier to read??? His code is a subset of the code you claim is "easier to read". In order to understand the ternary you need to understand the first expression (the condition), as well as the ternary itself, as well as the assignment operator. His code makes for less to read, and is clearer (one less step in deducing what value the variable ends up having under various conditions). Anyone who uses a ternary of type boolean is a twit!

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

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

                                    G 1 Reply Last reply
                                    0
                                    • N Nagy Vilmos

                                      For me

                                      geoffs wrote:

                                      m_boolVar = intVar != 0;

                                      is the bestest style. Generally != is a better option then ==.


                                      Panic, Chaos, Destruction. My work here is done.

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

                                      Thanks for the vote of confidence! On another point, I was with you until that last statement. Wouldn't the choice of '==' versus '!=' be completely dependent on what comparison you were trying to make? Generally isn't a term I would apply here.

                                      D 1 Reply Last reply
                                      0
                                      • R RugbyLeague

                                        It's just different styles of programming rather than a coding horror.

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

                                        I agree that it leans more towards the stylistic category rather than coding horror. That's why after I'd posted I wondered if I shouldn't have more appropriately posted this in the Soap Box. Oh well...

                                        1 Reply Last reply
                                        0
                                        • G geoffs

                                          Yeah, thanks, I typed too fast. Sometimes (as you get older) what you are thinking doesn't quite translate into what you are typing! :sigh:

                                          G Offline
                                          G Offline
                                          GDavy
                                          wrote on last edited by
                                          #20

                                          geoffs wrote:

                                          as you get older

                                          Don't worry about it, I can have that too, and I'm not that old yet (little over 30). And luckily not everything I think is being translated into what I'm typing ;) As to your original post, I completely agree with you. I have the unlucky position to have to tackle code that has multiple gems like these. Is the logical expression that difficult to read for some that they have to literally assign a 'true' or 'false'? Or perhaps the coder fears that the logical expression returns a different kind of bool than true or false? I myself find it bad code-practice.

                                          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