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. Zero-choice alternative

Zero-choice alternative

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharp
24 Posts 14 Posters 1 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.
  • P PIEBALDconsult

    More like a second chance. Yesterday I somehow wound up writing if ( x != x )..., I enjoyed the warning I got: warning CS1718: Comparison made to same variable; did you mean to compare something else?

    T Offline
    T Offline
    Tony Pottier
    wrote on last edited by
    #4

    You could actually write something like that to show the dangers of unsafe multithreading :p

    C 1 Reply Last reply
    0
    • L Lutoslaw

      Found in my own code:

      if (action == InvalidContentActions.Remove || action == InvalidContentActions.Remove) {

      ...

      Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #5

      There should be more code like your example. I'm all for double checking things instead of just single checking. ;P

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

      T 1 Reply Last reply
      0
      • C Chris Meech

        There should be more code like your example. I'm all for double checking things instead of just single checking. ;P

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

        T Offline
        T Offline
        TommyTomToms
        wrote on last edited by
        #6

        I agree. In this day and age you can never be sure of anything...

                DoesJessicaAlbaLoveMe uncertain = new DoesJessicaAlbaLoveMe();
                string yahhhoooooo = "";
                
                if (DoesJessicaAlbaLoveMe != null && DoesJessicaAlbaLoveMe!= null)
                {
                    yahhhoooooo = uncertain.ToString().ToString().ToString();
                }
                else
                {
                    yahoo = "She's just teasin :P";
                }
        

        A dogged, arrogant belief in self and the childlike idealism that comes with not knowing my limits. This is my greatest blessing, my priceless attribute.

        L 1 Reply Last reply
        0
        • T TommyTomToms

          I agree. In this day and age you can never be sure of anything...

                  DoesJessicaAlbaLoveMe uncertain = new DoesJessicaAlbaLoveMe();
                  string yahhhoooooo = "";
                  
                  if (DoesJessicaAlbaLoveMe != null && DoesJessicaAlbaLoveMe!= null)
                  {
                      yahhhoooooo = uncertain.ToString().ToString().ToString();
                  }
                  else
                  {
                      yahoo = "She's just teasin :P";
                  }
          

          A dogged, arrogant belief in self and the childlike idealism that comes with not knowing my limits. This is my greatest blessing, my priceless attribute.

          L Offline
          L Offline
          Lutoslaw
          wrote on last edited by
          #7

          TommyTomToms wrote:

          DoesJessicaAlbaLoveMe != null && DoesJessicaAlbaLoveMe!= null

          It is quite dangerous: if uncertain is volatile, it may change in the meantime. Indeed, if it's not on paper, such behavior might even be expected.

          Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

          T 1 Reply Last reply
          0
          • T Tony Pottier

            You could actually write something like that to show the dangers of unsafe multithreading :p

            C Offline
            C Offline
            ClementsDan
            wrote on last edited by
            #8

            I've seriously used code like that to check for NaN.

            S B 2 Replies Last reply
            0
            • L Lutoslaw

              Found in my own code:

              if (action == InvalidContentActions.Remove || action == InvalidContentActions.Remove) {

              ...

              Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

              C Offline
              C Offline
              ClementsDan
              wrote on last edited by
              #9

              Some code I encountered yesterday included:

              if (string.IsNullOrEmpty(client) || string.IsNullOrEmpty(client))

              I think it's just a typo.

              L 1 Reply Last reply
              0
              • C ClementsDan

                Some code I encountered yesterday included:

                if (string.IsNullOrEmpty(client) || string.IsNullOrEmpty(client))

                I think it's just a typo.

                L Offline
                L Offline
                Lutoslaw
                wrote on last edited by
                #10

                ClementsDan wrote:

                I think it's just a typo.

                In my case the code should be:

                if (action == InvalidContentActions.Remove || action == InvalidContentActions.CommentOut)

                Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

                1 Reply Last reply
                0
                • C ClementsDan

                  I've seriously used code like that to check for NaN.

                  S Offline
                  S Offline
                  supercat9
                  wrote on last edited by
                  #11

                  I've seriously used code like that to check for NaN. If A and B are both floating-point NaN's related to the same condition, what are the values of the six relational operators? If they all report false, then one could test if two numbers were both equal or both NaN with "if (!(a != b))"; if NaN's report not equal to each other but neither greater- nor less-than each other, then maybe "if (!(a > b) && !(a < b))". Of course, unless such statements are commented people would likely find them confusing.

                  1 Reply Last reply
                  0
                  • L Lutoslaw

                    TommyTomToms wrote:

                    DoesJessicaAlbaLoveMe != null && DoesJessicaAlbaLoveMe!= null

                    It is quite dangerous: if uncertain is volatile, it may change in the meantime. Indeed, if it's not on paper, such behavior might even be expected.

                    Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

                    T Offline
                    T Offline
                    Thomas Weller 0
                    wrote on last edited by
                    #12

                    Not totally sure about that without checking, but: Isn't an if statement atomic, at least such a simple one? I.E.: Is uncertain allowed to change at all between the two boolean checks, may it be volatile or not?

                    www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                    Programmer - an organism that turns coffee into software.

                    L S C 3 Replies Last reply
                    0
                    • S supercat9

                      If action is volatile, it would be possible for the second test to succeed even if the first fails. Indeed, if it's a memory-mapped mapped register, such behavior might even be expected.

                      T Offline
                      T Offline
                      Thomas Weller 0
                      wrote on last edited by
                      #13

                      That's true. But in that case it has to be made explicit in the code, either by using comments or a critical section or whatever may be appropriate. Otherwise it would indeed be a very severe coding horror (since it could cause an error that would be very hard to find...) X| .

                      www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                      Programmer - an organism that turns coffee into software.

                      1 Reply Last reply
                      0
                      • T Thomas Weller 0

                        Not totally sure about that without checking, but: Isn't an if statement atomic, at least such a simple one? I.E.: Is uncertain allowed to change at all between the two boolean checks, may it be volatile or not?

                        www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                        Programmer - an organism that turns coffee into software.

                        L Offline
                        L Offline
                        Lutoslaw
                        wrote on last edited by
                        #14

                        uncertain = uncertian

                        I'm affraid that Jessica could change a volatile value of the uncertian even during the '=' symbol.

                        Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

                        T 1 Reply Last reply
                        0
                        • T Thomas Weller 0

                          Not totally sure about that without checking, but: Isn't an if statement atomic, at least such a simple one? I.E.: Is uncertain allowed to change at all between the two boolean checks, may it be volatile or not?

                          www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                          Programmer - an organism that turns coffee into software.

                          S Offline
                          S Offline
                          supercat9
                          wrote on last edited by
                          #15

                          If "foo" is volatile, the statement "if (foo == foo) bar();" must read the value of foo exactly twice and compare the two values read. If "foo" happens to be an address in normal RAM, the two reads would be most likely read the same data unless an interrupt or task switch happened to occur between them (not terribly likely). Not everything in a processor-based system is RAM, however. When a typical flash memory chip is performing a write cycle, consecutive read operations are guaranteed to return different data. If the generated code for the above 'if' statement didn't read 'foo' twice, the 'if' test would succeed even while the flash was still being written.

                          T L 2 Replies Last reply
                          0
                          • L Lutoslaw

                            uncertain = uncertian

                            I'm affraid that Jessica could change a volatile value of the uncertian even during the '=' symbol.

                            Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

                            T Offline
                            T Offline
                            Thomas Weller 0
                            wrote on last edited by
                            #16

                            So there's absolutely nothing you can do - this code IS dangerous and stays dangerous :). -- Or is it Jessica who is dangerous... ;)

                            www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                            Programmer - an organism that turns coffee into software.

                            1 Reply Last reply
                            0
                            • S supercat9

                              If "foo" is volatile, the statement "if (foo == foo) bar();" must read the value of foo exactly twice and compare the two values read. If "foo" happens to be an address in normal RAM, the two reads would be most likely read the same data unless an interrupt or task switch happened to occur between them (not terribly likely). Not everything in a processor-based system is RAM, however. When a typical flash memory chip is performing a write cycle, consecutive read operations are guaranteed to return different data. If the generated code for the above 'if' statement didn't read 'foo' twice, the 'if' test would succeed even while the flash was still being written.

                              T Offline
                              T Offline
                              Thomas Weller 0
                              wrote on last edited by
                              #17

                              Thanks for that - wasn't totally sure about it from the top of my head...

                              www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                              Programmer - an organism that turns coffee into software.

                              1 Reply Last reply
                              0
                              • T Thomas Weller 0

                                Not totally sure about that without checking, but: Isn't an if statement atomic, at least such a simple one? I.E.: Is uncertain allowed to change at all between the two boolean checks, may it be volatile or not?

                                www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                                Programmer - an organism that turns coffee into software.

                                C Offline
                                C Offline
                                cpkilekofp
                                wrote on last edited by
                                #18

                                No, an if statement is not guaranteed to be atomic as it can be decomposed into multiple binary instructions, and a task switch can occur between any of those instructions unless the compiler designates the entire sequence as a critical section.

                                1 Reply Last reply
                                0
                                • C ClementsDan

                                  I've seriously used code like that to check for NaN.

                                  B Offline
                                  B Offline
                                  Bram Fokke
                                  wrote on last edited by
                                  #19

                                  Wouldn't float.IsNaN(x) be a more readable way to achieve that?

                                  M 1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    More like a second chance. Yesterday I somehow wound up writing if ( x != x )..., I enjoyed the warning I got: warning CS1718: Comparison made to same variable; did you mean to compare something else?

                                    K Offline
                                    K Offline
                                    KarstenK
                                    wrote on last edited by
                                    #20

                                    That is a good test for a custom "!=" operator X|

                                    Greetings from Germany

                                    1 Reply Last reply
                                    0
                                    • S supercat9

                                      If action is volatile, it would be possible for the second test to succeed even if the first fails. Indeed, if it's a memory-mapped mapped register, such behavior might even be expected.

                                      P Offline
                                      P Offline
                                      peterchen
                                      wrote on last edited by
                                      #21

                                      Reminds me of that little DOS / Win console classic to wait for a keystroke: if (!getch()) getch(); :cool: yes, this is the right way to do it

                                      Burning Chrome ^ | Linkify!| FoldWithUs! | sighist

                                      1 Reply Last reply
                                      0
                                      • B Bram Fokke

                                        Wouldn't float.IsNaN(x) be a more readable way to achieve that?

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

                                        Only decimal numbers have NaN as static class member (at least ni C#), natural numbers like int or short don't have it. Though if something is NaN, you would get an error the moment it's assigned to a natural number variable.

                                        1 Reply Last reply
                                        0
                                        • S supercat9

                                          If "foo" is volatile, the statement "if (foo == foo) bar();" must read the value of foo exactly twice and compare the two values read. If "foo" happens to be an address in normal RAM, the two reads would be most likely read the same data unless an interrupt or task switch happened to occur between them (not terribly likely). Not everything in a processor-based system is RAM, however. When a typical flash memory chip is performing a write cycle, consecutive read operations are guaranteed to return different data. If the generated code for the above 'if' statement didn't read 'foo' twice, the 'if' test would succeed even while the flash was still being written.

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

                                          hey, if would b gr8 if u could explain wat u mean by variable being volatile

                                          Ravie Busie Coding is my birth-right and bugs are part of feature my code has!

                                          S 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