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. The Lounge
  3. PQOTD

PQOTD

Scheduled Pinned Locked Moved The Lounge
csharpquestion
59 Posts 31 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.
  • M Marc Clifton

    Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

    		bool more = ProcessFlyouts();
    		more = more || ProcessCarrierAnimations();
    

    How would you change it to "do the right thing?" Marc

    P Offline
    P Offline
    phil o
    wrote on last edited by
    #2

    Can you define 'right'?

    [Flags]
    public enum Bool {
    True, False, ForSure, Maybe, ProbablyNot, Depends, NotDecidedYet, Undefined
    }

    private interface IStealth { }

    M 1 Reply Last reply
    0
    • P phil o

      Can you define 'right'?

      [Flags]
      public enum Bool {
      True, False, ForSure, Maybe, ProbablyNot, Depends, NotDecidedYet, Undefined
      }

      private interface IStealth { }

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #3

      You should be able to glean what the programmer intends to happen from the code. :) Marc

      P B S P 4 Replies Last reply
      0
      • M Marc Clifton

        Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

        		bool more = ProcessFlyouts();
        		more = more || ProcessCarrierAnimations();
        

        How would you change it to "do the right thing?" Marc

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #4

        |

        Regards, Nish


        Blog: voidnish.wordpress.com Latest article: C++ 11 features in Visual C++ 2013 Preview

        M R G 3 Replies Last reply
        0
        • M Marc Clifton

          Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

          		bool more = ProcessFlyouts();
          		more = more || ProcessCarrierAnimations();
          

          How would you change it to "do the right thing?" Marc

          D Offline
          D Offline
          Deflinek
          wrote on last edited by
          #5

          I'm not quite sure what was unexpected here. If you mean the ProcessCarrierAnimations() never gets called if (more == true) then it really was expected :) However changing || to | will make it called regardless of more's value.

          -- "My software never has bugs. It just develops random features."

          M 1 Reply Last reply
          0
          • M Marc Clifton

            You should be able to glean what the programmer intends to happen from the code. :) Marc

            P Offline
            P Offline
            phil o
            wrote on last edited by
            #6

            Maybe, if I could see the content of both methods that return a bool :) Otherwise, quite hard to detect a logic issue when the logic itself is not told.

            [Flags]
            public enum Bool {
            True, False, ForSure, Maybe, ProbablyNot, Depends, NotDecidedYet, Undefined
            }

            private interface IStealth { }

            1 Reply Last reply
            0
            • M Marc Clifton

              Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

              		bool more = ProcessFlyouts();
              		more = more || ProcessCarrierAnimations();
              

              How would you change it to "do the right thing?" Marc

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #7

              It will run either ProcessFlyouts, or ProcessCarrierAnimations, or neither, and the variable will tell you if it successfully ran either. I would imagine you either want && or |.

              M J 2 Replies Last reply
              0
              • M Marc Clifton

                Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                		bool more = ProcessFlyouts();
                		more = more || ProcessCarrierAnimations();
                

                How would you change it to "do the right thing?" Marc

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #8

                I assume you meant it to do this:

                bool more = ProcessFlyouts();
                more = ProcessCarrierAnimations() || more;

                :laugh: Would do the same in C and C++ as well.

                Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                M 1 Reply Last reply
                0
                • N Nish Nishant

                  |

                  Regards, Nish


                  Blog: voidnish.wordpress.com Latest article: C++ 11 features in Visual C++ 2013 Preview

                  M Offline
                  M Offline
                  Marc Clifton
                  wrote on last edited by
                  #9

                  Exactly. :)

                  1 Reply Last reply
                  0
                  • M Marc Clifton

                    Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                    		bool more = ProcessFlyouts();
                    		more = more || ProcessCarrierAnimations();
                    

                    How would you change it to "do the right thing?" Marc

                    Kornfeld Eliyahu PeterK Offline
                    Kornfeld Eliyahu PeterK Offline
                    Kornfeld Eliyahu Peter
                    wrote on last edited by
                    #10

                    I assume that you want to run the second method if first returns true, if that the case you have to switch the order

                    bool more = ProcessFlyouts();
                    more = ProcessCarrierAnimations() || more;

                    I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                    "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                    M 1 Reply Last reply
                    0
                    • D Deflinek

                      I'm not quite sure what was unexpected here. If you mean the ProcessCarrierAnimations() never gets called if (more == true) then it really was expected :) However changing || to | will make it called regardless of more's value.

                      -- "My software never has bugs. It just develops random features."

                      M Offline
                      M Offline
                      Marc Clifton
                      wrote on last edited by
                      #11

                      deflinek wrote:

                      However changing || to | will make it called regardless of more's value.

                      Yup! Marc

                      1 Reply Last reply
                      0
                      • B BobJanova

                        It will run either ProcessFlyouts, or ProcessCarrierAnimations, or neither, and the variable will tell you if it successfully ran either. I would imagine you either want && or |.

                        M Offline
                        M Offline
                        Marc Clifton
                        wrote on last edited by
                        #12

                        Yup. | Marc

                        1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          I assume you meant it to do this:

                          bool more = ProcessFlyouts();
                          more = ProcessCarrierAnimations() || more;

                          :laugh: Would do the same in C and C++ as well.

                          Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                          M Offline
                          M Offline
                          Marc Clifton
                          wrote on last edited by
                          #13

                          OriginalGriff wrote:

                          more = ProcessCarrierAnimations() || more;

                          Quite so. The bitwise | operator solves the problem too. Marc

                          OriginalGriffO 1 Reply Last reply
                          0
                          • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                            I assume that you want to run the second method if first returns true, if that the case you have to switch the order

                            bool more = ProcessFlyouts();
                            more = ProcessCarrierAnimations() || more;

                            I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                            M Offline
                            M Offline
                            Marc Clifton
                            wrote on last edited by
                            #14

                            Kornfeld Eliyahu Peter wrote:

                            you have to switch the order

                            Yes indeed. Or use the bitwise operator. Marc

                            Kornfeld Eliyahu PeterK D 2 Replies Last reply
                            0
                            • M Marc Clifton

                              Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                              		bool more = ProcessFlyouts();
                              		more = more || ProcessCarrierAnimations();
                              

                              How would you change it to "do the right thing?" Marc

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

                              MISRA[^] Rule 33 & 34:

                              The right hand operand of a || shall not contain side effects
                              The operands of a logical || shall be primary expressions

                              The example you gave is the very reason for the above two rules.

                              ~RaGE();

                              I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Entropy isn't what it used to.

                              B P 2 Replies Last reply
                              0
                              • M Marc Clifton

                                OriginalGriff wrote:

                                more = ProcessCarrierAnimations() || more;

                                Quite so. The bitwise | operator solves the problem too. Marc

                                OriginalGriffO Offline
                                OriginalGriffO Offline
                                OriginalGriff
                                wrote on last edited by
                                #16

                                Yes - but I don't like bitwise operators with bools...

                                Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                1 Reply Last reply
                                0
                                • N Nish Nishant

                                  |

                                  Regards, Nish


                                  Blog: voidnish.wordpress.com Latest article: C++ 11 features in Visual C++ 2013 Preview

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

                                  Yep, but this is a workaround, not a best practice though.

                                  ~RaGE();

                                  I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Entropy isn't what it used to.

                                  1 Reply Last reply
                                  0
                                  • M Marc Clifton

                                    Kornfeld Eliyahu Peter wrote:

                                    you have to switch the order

                                    Yes indeed. Or use the bitwise operator. Marc

                                    Kornfeld Eliyahu PeterK Offline
                                    Kornfeld Eliyahu PeterK Offline
                                    Kornfeld Eliyahu Peter
                                    wrote on last edited by
                                    #18

                                    I do not like that change from C/C++...I mean the | and || changes... X|

                                    I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                                    "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                                    F J 2 Replies Last reply
                                    0
                                    • R Rage

                                      MISRA[^] Rule 33 & 34:

                                      The right hand operand of a || shall not contain side effects
                                      The operands of a logical || shall be primary expressions

                                      The example you gave is the very reason for the above two rules.

                                      ~RaGE();

                                      I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Entropy isn't what it used to.

                                      B Offline
                                      B Offline
                                      BobJanova
                                      wrote on last edited by
                                      #19

                                      There's actually some nice concise uses of

                                      var result = DoSomething() && DoSomethingThatDependsOnIt();
                                      var result = DoSomething() || HandleFailureCase();

                                      It's even better in languages where you can use non-boolean types with the boolean operators.

                                      R F 2 Replies Last reply
                                      0
                                      • M Marc Clifton

                                        Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                                        		bool more = ProcessFlyouts();
                                        		more = more || ProcessCarrierAnimations();
                                        

                                        How would you change it to "do the right thing?" Marc

                                        K Offline
                                        K Offline
                                        Keith Barrow
                                        wrote on last edited by
                                        #20

                                        Though Nish's and OG's examples will work, I think it is symantically better to do this:

                                        bool more = ProcessFlyouts();
                                        bool foo = ProcessCarrierAnimations();
                                        more = more || foo;

                                        Less terse code (normally bad) - but explicit in calling ProcessCarrierAnimations.

                                        PB 369,783 wrote:

                                        I just find him very unlikeable, and I think the way he looks like a prettier version of his Mum is very disturbing.[^]

                                        P 1 Reply Last reply
                                        0
                                        • M Marc Clifton

                                          Programming Quiz of the day. I just wrote this code (C#), which did something very unexpected. What was it that it unexpectedly did?

                                          		bool more = ProcessFlyouts();
                                          		more = more || ProcessCarrierAnimations();
                                          

                                          How would you change it to "do the right thing?" Marc

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

                                          more |= ProcessCarrierAnimations(); ?

                                          You'll never get very far if all you do is follow instructions.

                                          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