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. Straight from the horse's mouth

Straight from the horse's mouth

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorialquestioncareer
35 Posts 23 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.
  • R Rob Grainger

    I begin to understand where redundancy in code comes from. The following example is from the Windows Phone Development material on MSDN:

    popToSelectedButton.IsEnabled = (historyListBox.SelectedItems.Count > 0) ? true : false;

    OK, its harmless enough, but why on earth do people feel it necessary to explicitly state the result of a logical expression. If a programmer doesn't get logic, it's probably time to consider another career choice.

    J Offline
    J Offline
    JackDingler
    wrote on last edited by
    #24

    I do this when converting bool to BOOL or back, for two reasons. 1. Code clarity. 2. Eliminate warnings. And as someone else pointed out, conditional expressions didn't always return a bool type. They used to return integers. So this could just be habit.

    R 1 Reply Last reply
    0
    • L Lost User

      In this case it's probably not worth getting angry over. Look what the compiler made out of this little test with a false condition:

              test = (1 == 0) ? true : false;
      

      00000037 xor edx,edx
      00000039 mov dword ptr [ebp-40h],edx

      It even pulled the old XOR trick out of the hat to set EDX to 0 :) It effectively realized that the condition will always be false and simply copied 0 into the variable. Seriously, coding practices (good or bad, old or new) may become problematic when they are used because somebody said so or because they have always been used. Good practices and rules are not a good substitute for knowing (or being able to verify) what the compiler will make of your code. I had the dubious pleasure of working in a 'best practices' team recently and do not think very much about their religious belief in their rules and that nothing can go wrong as long as they firmly hold onto them. The gray mass in their heads is not just there to keep their ears apart :)

      I'm invincible, I can't be vinced

      R Offline
      R Offline
      Rob Grainger
      wrote on last edited by
      #25

      I won't dispute that this is harmless for the generated code, its just redundancy in source code, and in high-level languages (such as C#), that should often be a higher priority.

      1 Reply Last reply
      0
      • J JackDingler

        I do this when converting bool to BOOL or back, for two reasons. 1. Code clarity. 2. Eliminate warnings. And as someone else pointed out, conditional expressions didn't always return a bool type. They used to return integers. So this could just be habit.

        R Offline
        R Offline
        Rob Grainger
        wrote on last edited by
        #26

        Not in C#, I hope! (This is from MSDN's WinPhone SDK, so obviously not C/C++)

        J 1 Reply Last reply
        0
        • R Rob Grainger

          Not in C#, I hope! (This is from MSDN's WinPhone SDK, so obviously not C/C++)

          J Offline
          J Offline
          JackDingler
          wrote on last edited by
          #27

          Obvious to folks developing in that arena I'm sure.

          1 Reply Last reply
          0
          • R Rob Grainger

            I begin to understand where redundancy in code comes from. The following example is from the Windows Phone Development material on MSDN:

            popToSelectedButton.IsEnabled = (historyListBox.SelectedItems.Count > 0) ? true : false;

            OK, its harmless enough, but why on earth do people feel it necessary to explicitly state the result of a logical expression. If a programmer doesn't get logic, it's probably time to consider another career choice.

            P Offline
            P Offline
            patbob
            wrote on last edited by
            #28

            Rob Grainger wrote:

            OK, its harmless enough..

            True, but its obfuscating redundancy, and its begging to be abused or typoed into something like:

            popToSelectedButton.IsEnabled = (historyListBox.SelectedItems.Count > 0) ? false : true;

            and that would be harmful.

            We can program with only 1's, but if all you've got are zeros, you've got nothing.

            1 Reply Last reply
            0
            • F Forogar

              At least it wasn't:

              popToSelectedButton.IsEnabled = !(historyListBox.SelectedItems.Count > 0) ? false : true;

              That kind of thing doesn't not happen all the time!

              - Life in the fast lane is only fun if you live in a country with no speed limits. - Of all the things I have lost, it is my mind that I miss the most. - I vaguely remember having a good memory...

              K Offline
              K Offline
              KP Lee
              wrote on last edited by
              #29

              Forogar wrote:

              That kind of thing doesn't not happen all the time!

              So true! I don't often see the use of double negatives used "properly" in English, so I automatically corrected it in my head to what you "really" meant. That didn't make sense. Ah, you intentionally mangled the English to get an expression as valid as your code example. Your mangled code is superior to the one I came up with. ...== 0) ? false : true;

              1 Reply Last reply
              0
              • R Rob Grainger

                I begin to understand where redundancy in code comes from. The following example is from the Windows Phone Development material on MSDN:

                popToSelectedButton.IsEnabled = (historyListBox.SelectedItems.Count > 0) ? true : false;

                OK, its harmless enough, but why on earth do people feel it necessary to explicitly state the result of a logical expression. If a programmer doesn't get logic, it's probably time to consider another career choice.

                K Offline
                K Offline
                KP Lee
                wrote on last edited by
                #30

                It seems to be a characteristic of Microsoft programmers, that they want to prove they are clever by producing less readable code. At times, the attempts at "clever" also makes the code a bug. It took me a week to convince the manager this was a bug:

                if ((CurrentThreadCount - StepThreadCount) <= MaxThreadCount) getMoreThreads();

                And that was clear-cut code, not even close to "clever". Three to five seconds after I first read it, I saw the bug correction needed:

                if ((CurrentThreadCount + StepThreadCount) <= MaxThreadCount) getMoreThreads();

                (It did take me about 40 seconds to verify in my mind my first thought for a correction was correct and about 15 minutes to go through the code and verify the names and method did what was suggested.) What I did like about it was that the variable and method names were so clearly doing what the names suggested was being done.

                1 Reply Last reply
                0
                • R Rob Grainger

                  I begin to understand where redundancy in code comes from. The following example is from the Windows Phone Development material on MSDN:

                  popToSelectedButton.IsEnabled = (historyListBox.SelectedItems.Count > 0) ? true : false;

                  OK, its harmless enough, but why on earth do people feel it necessary to explicitly state the result of a logical expression. If a programmer doesn't get logic, it's probably time to consider another career choice.

                  K Offline
                  K Offline
                  K Quinn
                  wrote on last edited by
                  #31

                  Filed under [Clock Cycle Fetish]

                  1 Reply Last reply
                  0
                  • R Rob Grainger

                    I begin to understand where redundancy in code comes from. The following example is from the Windows Phone Development material on MSDN:

                    popToSelectedButton.IsEnabled = (historyListBox.SelectedItems.Count > 0) ? true : false;

                    OK, its harmless enough, but why on earth do people feel it necessary to explicitly state the result of a logical expression. If a programmer doesn't get logic, it's probably time to consider another career choice.

                    M Offline
                    M Offline
                    mbb01
                    wrote on last edited by
                    #32

                    It might not be so bad depending on the context. If this code was ported into C then it might avoid a problem in interpreting what true and false actually are. Then again might have been C code originally and simply copied into place ...

                    R 1 Reply Last reply
                    0
                    • M mbb01

                      It might not be so bad depending on the context. If this code was ported into C then it might avoid a problem in interpreting what true and false actually are. Then again might have been C code originally and simply copied into place ...

                      R Offline
                      R Offline
                      Rob Grainger
                      wrote on last edited by
                      #33

                      If you're writing code in which the meaning of true and false are in question, this would be the least of your problems at a guess. I've never experienced such issues, and I code widely in C, C#, C++, JavaScript, occassionally D and Java. I'm assured no such issues are common moving to PHP either by my learned friend.

                      M 1 Reply Last reply
                      0
                      • R Rob Grainger

                        If you're writing code in which the meaning of true and false are in question, this would be the least of your problems at a guess. I've never experienced such issues, and I code widely in C, C#, C++, JavaScript, occassionally D and Java. I'm assured no such issues are common moving to PHP either by my learned friend.

                        M Offline
                        M Offline
                        mbb01
                        wrote on last edited by
                        #34

                        I was speaking in the context of C and where some systems have macros defined for TRUE and FALSE. Sometimes I've seen those macros setup as (1==0) and (1==1) or simply 0 and 1 or 0 and -1. On several systems I've worked on GOOD and BAD have been used for an error flag and many programmers might assume GOOD and BAD are true and false and they are not. As ever with C, if in doubt be as explicit as you can be to avoid the compiler making choices for you. Most of the times you get away with those choices. Sometimes, for example when porting to another platform and compiler, you don't. Another common problem with porting C code is the difference between short, int, long and long64 (or whatever). What an int could actually be can cause all sorts of problems in porting scenarios.

                        1 Reply Last reply
                        0
                        • F Forogar

                          At least it wasn't:

                          popToSelectedButton.IsEnabled = !(historyListBox.SelectedItems.Count > 0) ? false : true;

                          That kind of thing doesn't not happen all the time!

                          - Life in the fast lane is only fun if you live in a country with no speed limits. - Of all the things I have lost, it is my mind that I miss the most. - I vaguely remember having a good memory...

                          P Offline
                          P Offline
                          Patrick Harris
                          wrote on last edited by
                          #35

                          5 stars... that made me laugh.

                          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