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

    D Offline
    D Offline
    dan sh
    wrote on last edited by
    #41

    No it did not do anything unexpected. || operator uses short-circuit evaluation. I would assume the intent was to call ProcessCarrierAnimations only if more was false.

    My CP workspace: Incredibly trivial and probably useless code samples[^]

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

      Yeah! It compiles. After then you can rub your head to find where the bug is!

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

      J Offline
      J Offline
      JimmyRopes
      wrote on last edited by
      #42

      Kornfeld Eliyahu Peter wrote:

      It compiles. After then you someone else will can rub your their head to find where the bug is!

      FTFY :-D

      The report of my death was an exaggeration - Mark Twain
      Simply Elegant Designs JimmyRopes Designs
      I'm on-line therefore I am. JimmyRopes

      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

        C Offline
        C Offline
        Chris Quinn
        wrote on last edited by
        #43

        It compiled?

        ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

        1 Reply Last reply
        0
        • P PIEBALDconsult

          I never follow other peoples' rules. :cool:

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

          S Offline
          S Offline
          SortaCore
          wrote on last edited by
          #44

          It's my rule that people have to breathe (or die trying).

          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

            A Offline
            A Offline
            Adam Tibi
            wrote on last edited by
            #45

            This is JavaScript style, the developer meant, if "more" is "undefined" then try the other option ProcessCarrierAnimations(). But obviously, this doesn't work in C#.

            Make it simple, as simple as possible, but not simpler.

            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

              G Offline
              G Offline
              Gary Wheeler
              wrote on last edited by
              #46

              Just to be contrary:

              bool more = false;
              if (ProcessFlyouts()) more = true;
              if (ProcessCarrierAnimations()) more = true;

              Software Zen: delete this;

              1 Reply Last reply
              0
              • N Nish Nishant

                |

                Regards, Nish


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

                G Offline
                G Offline
                Gary Wheeler
                wrote on last edited by
                #47

                Yuck. One of my least favorite things when reading someone else's C/C++ code is when they mix logical and bit-wise operators in an expression. In this case, using a bit-wise operator on bool values just seems wrong.

                Software Zen: delete this;

                N 1 Reply Last reply
                0
                • G Gary Wheeler

                  Yuck. One of my least favorite things when reading someone else's C/C++ code is when they mix logical and bit-wise operators in an expression. In this case, using a bit-wise operator on bool values just seems wrong.

                  Software Zen: delete this;

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

                  I agree, it's not something I'd use myself. I was only answering Marc's academic question :-)

                  Regards, Nish


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

                  G 1 Reply Last reply
                  0
                  • N Nish Nishant

                    I agree, it's not something I'd use myself. I was only answering Marc's academic question :-)

                    Regards, Nish


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

                    G Offline
                    G Offline
                    Gary Wheeler
                    wrote on last edited by
                    #49

                    I thought that was the case - your response just seemed rather unNish-like :laugh:.

                    Software Zen: delete this;

                    N 1 Reply Last reply
                    0
                    • G Gary Wheeler

                      I thought that was the case - your response just seemed rather unNish-like :laugh:.

                      Software Zen: delete this;

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

                      Heh :-)

                      Regards, Nish


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

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

                        J Offline
                        J Offline
                        James Curran
                        wrote on last edited by
                        #51

                        >> It will run either ProcessFlyouts, or ProcessCarrierAnimations, or neither, Um... Not even close. It will, unquestionably, run ProcessFlyouts. It may also run ProcessCarrierAnimations.

                        Truth, James

                        B 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

                          J Offline
                          J Offline
                          James Curran
                          wrote on last edited by
                          #52

                          The "early out" semantics of the logical operators (|| and &&) are the same for C#, C++, Java, and C, and have been well-specified since the First Edition of K&R C. How could the result possibly be unexpected?

                          Truth, James

                          1 Reply Last reply
                          0
                          • J James Curran

                            >> It will run either ProcessFlyouts, or ProcessCarrierAnimations, or neither, Um... Not even close. It will, unquestionably, run ProcessFlyouts. It may also run ProcessCarrierAnimations.

                            Truth, James

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

                            That is pretty close to what I said, although that was admittedly technically wrong. (I understand how || works but apparently failed in the challenge of writing English!) "Not even close" would be "it returns banana or grapefruit depending on the wolf".

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

                              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)

                              J Offline
                              J Offline
                              James Curran
                              wrote on last edited by
                              #54

                              >> I do not like that change from C/C++...I mean the | and || changes... Exactly, what changes are those? I've used C# for 10 ten years, and C/C++ for the ten years before that, and have noticed no differences in the handling of logical operators.

                              Truth, James

                              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

                                R Offline
                                R Offline
                                RichardInToronto
                                wrote on last edited by
                                #55

                                My bet is that ProcessCarrierAnimations isn't executed if ProcessFlyouts returns true. If ProcessCarrierAnimations should still be called, you could remove one of the "vertical bars", changing the "short-circuit" OR into a regular logical-OR. Richard

                                F 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
                                  patbob
                                  wrote on last edited by
                                  #56

                                  Without further information, it is obvious that the programmer intended ProcessCarrierAnimations() to only be called if ProcessFlyouts() returns false. I'm with OriginalGriff on this..

                                  ProcessCarrierAnimations() || more;

                                  This is clearly the better way to express intent that using a bitwise OR. Unless there are tests like this all over the code done with bitwise ORs, using one here requires the next programmer to notice you did something atypical here in a fairly subtle way, which means its a maintenance hazard, which in my book is part of the definition of poor coding.

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

                                  1 Reply Last reply
                                  0
                                  • R RichardInToronto

                                    My bet is that ProcessCarrierAnimations isn't executed if ProcessFlyouts returns true. If ProcessCarrierAnimations should still be called, you could remove one of the "vertical bars", changing the "short-circuit" OR into a regular logical-OR. Richard

                                    F Offline
                                    F Offline
                                    Fernando Takeshi Sato
                                    wrote on last edited by
                                    #57

                                    hhhmmm...I never thought of bitwise operators as being the 'regular' ones; quite the contrary, actually.

                                    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

                                      M Offline
                                      M Offline
                                      Member_5893260
                                      wrote on last edited by
                                      #58

                                      It failed to run ProcessCarrierAnimations because more was already true, and thus the evaluator short-circuited. You want something like this:

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

                                      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

                                        U Offline
                                        U Offline
                                        User 10244914
                                        wrote on last edited by
                                        #59

                                        if(!ProcessFlyouts()) { ProcessCarrierAnimations() }

                                        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