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.
  • 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
          • D darkelv

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

            I am in complete agreement with you that the verbosity of the line of code that didn't sit well with me would surely tend to slow any reader down. When I am being asked to review hundreds or thousands of lines of code I would rather be slowed down for consideration of the correctness of the solution for the problem rather than issues of coding style like this. BTW, the issue of being tired and thus not typing what the mind was thinking is an issue that can result in mistypes regardless of how the line was coded. It is often best to not program when exhausted (and obviously to not post to Code Project when same).

            1 Reply Last reply
            0
            • D dojohansen

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

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

              Hey, I am really getting to like you... :-D

              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:

                Q Offline
                Q Offline
                QuiJohn
                wrote on last edited by
                #23

                geoffs wrote:

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

                Which can also happen when coding, and the original version would be less prone to this kind of error. Although I wouldn't have done it that way either. I find your last solution to be way more of a horror than the original though. I also like putting parentheses around something like "Foo = (Bar != 0)" as it makes it visually more obvious what is going on.


                He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

                G 1 Reply Last reply
                0
                • Q QuiJohn

                  geoffs wrote:

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

                  Which can also happen when coding, and the original version would be less prone to this kind of error. Although I wouldn't have done it that way either. I find your last solution to be way more of a horror than the original though. I also like putting parentheses around something like "Foo = (Bar != 0)" as it makes it visually more obvious what is going on.


                  He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

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

                  David Kentley wrote:

                  I find your last solution to be way more of a horror than the original though.

                  I did use the word perverse with regards to the last solution. But sometimes I feel that way. I must disagree with you with regard to the first version as I feel that it is just as prone, if not more so, to errors from mistyping.

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

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

                    Oh, yes, I remember those days. Comments were out of the question since your source / the interpreter /the compiler had to fit into those 4 kb. And the same went for longer variable names, if the language supported them at all. But since then we literally got a million times as much memory at our disposal. Priorities have changed and now I also prefer clarity.

                    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.

                    G 1 Reply Last reply
                    0
                    • L Lost User

                      Oh, yes, I remember those days. Comments were out of the question since your source / the interpreter /the compiler had to fit into those 4 kb. And the same went for longer variable names, if the language supported them at all. But since then we literally got a million times as much memory at our disposal. Priorities have changed and now I also prefer clarity.

                      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.

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

                      Certainly true. I think the real issue is whether or not the original line has any more clarity than the one I prefer coded up. IMO it does not. What part of "logical expression yielding a boolean result" is tough for any programmer to read and understand? At some point, verbosity for clarity's sake can become ridiculous.

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

                      B L 2 Replies Last reply
                      0
                      • G geoffs

                        Certainly true. I think the real issue is whether or not the original line has any more clarity than the one I prefer coded up. IMO it does not. What part of "logical expression yielding a boolean result" is tough for any programmer to read and understand? At some point, verbosity for clarity's sake can become ridiculous.

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

                        B Offline
                        B Offline
                        BillW33
                        wrote on last edited by
                        #27

                        I have to agree. m_boolVar = intVar != 0; is much clearer and easier to read, IMO. Bill W

                        1 Reply Last reply
                        0
                        • G geoffs

                          Certainly true. I think the real issue is whether or not the original line has any more clarity than the one I prefer coded up. IMO it does not. What part of "logical expression yielding a boolean result" is tough for any programmer to read and understand? At some point, verbosity for clarity's sake can become ridiculous.

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

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

                          In fact I just had the very same situation today. A call to a SAP-Websewrvice yielded an integer as result. The function in which the web method was called was required by an interface to return a boolean value for success or failure. In my case it came down to something like this:

                          ...

                          ReturnValue = SAPWebserviceProxy.Z_XXXXXX(...);
                          ErrorFlag = IsError(ReturnValue);
                          if(ErrorFlag)
                          {
                          LogSAPError(ReturnValue);
                          }
                          else
                          {
                          LogSuccess(.....);
                          }

                          return ErrorFlag;

                          This is by no means great code, but at least everybody should be able to see what's going on. It's alwys a little awkward, no matter what you do.

                          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.

                          G 1 Reply Last reply
                          0
                          • L Lost User

                            In fact I just had the very same situation today. A call to a SAP-Websewrvice yielded an integer as result. The function in which the web method was called was required by an interface to return a boolean value for success or failure. In my case it came down to something like this:

                            ...

                            ReturnValue = SAPWebserviceProxy.Z_XXXXXX(...);
                            ErrorFlag = IsError(ReturnValue);
                            if(ErrorFlag)
                            {
                            LogSAPError(ReturnValue);
                            }
                            else
                            {
                            LogSuccess(.....);
                            }

                            return ErrorFlag;

                            This is by no means great code, but at least everybody should be able to see what's going on. It's alwys a little awkward, no matter what you do.

                            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.

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

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

                            L 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
                              Rage
                              wrote on last edited by
                              #30

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