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. Bug of the day

Bug of the day

Scheduled Pinned Locked Moved The Weird and The Wonderful
help
37 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.
  • D Delphi4ever

    if(SomeThing == SomeOtherThing); { DoSomeThing; } This one has been sitting in the codebase for a couple of years... :(( At least it did SomeThing...

    M Offline
    M Offline
    MainFrameMan_ALIVE_AND_WELL
    wrote on last edited by
    #23

    the semi-colon on the end of the conditional, the block will always execute. DoSomething is in its own little world and will always execute. C# will just raise a warning on the conditional but will not compile with the DoSomeThing becuse it is not a method or an assignment. But, what if it is not C# compiler, then there could be compilers that would allow this and it would be worthless code, i still use Crimson, NotePad, etc to write code. Either to many semi colons or not enough !!

    1 Reply Last reply
    0
    • G Gary Wheeler

      Yes, but I've always hated doing those, unless you write it on a single line:

      if condition then DoSomething() else DoOtherThing();

      That's the only way in my mind to avoid stupid mistakes like this:

      if condition then
      DoThing1();
      DoThing2();
      DoThing3();
      MainStuff();

      DoThing2() and DoThing3() look like they're part of the if, but they're not. I do the same thing in C-style languages. If an if-statement occupies more than one line, it gets braced.

      Software Zen: delete this;

      S Offline
      S Offline
      StatementTerminator
      wrote on last edited by
      #24

      Gary Wheeler wrote:

      if condition then DoThing1(); DoThing2(); DoThing3(); MainStuff();

      Yeah that's one of my all-time favorites. It usually starts out like:

      if(_condition_) 
          DoThing1();
      

      And then someone comes along later and adds DoThing2(), and the compiler silently chuckles to itself and lets the code do a lot of Thing2. Personally, I like to always use curly braces to block off code for that very reason, even if it's only one statement. If I see something like that with one statement, I'll add the braces so it's clear when someone comes along and changes it. Putting it all on one line works fine as well, but I like to always create a code block because it makes it easier to add statements later. And really, what's the point of keeping it on one line? But I know a lot of programmers have an irrational fear of vertical space :)

      1 Reply Last reply
      0
      • D Delphi4ever

        if(SomeThing == SomeOtherThing); { DoSomeThing; } This one has been sitting in the codebase for a couple of years... :(( At least it did SomeThing...

        R Offline
        R Offline
        RafagaX
        wrote on last edited by
        #25

        I bet that in DoSomeThing there is logic to handle the strange case when the if condition is not firing properly. :rolleyes:

        CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

        1 Reply Last reply
        0
        • P Pablo Aliskevicius

          I usually prefer:

          if (42 == ComputeSomeThing(x)) {
          //...
          }

          This one avoids the = against == pit. Another thing I do: my IDE is configured to show operators (like '(){};,+-=...') in color, so they are a bit harder to miss (like the original example). There are two other tools that can help with this: compiler warnings, and static code analysis. JM2B,

          Pablo. "Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, circa 1899).

          S Offline
          S Offline
          StatementTerminator
          wrote on last edited by
          #26

          Pablo Aliskevicius wrote:

          This one avoids the = against == pit.

          LOL, that one kills me, because I'm constantly switching back and forth between C# and VB :((

          1 Reply Last reply
          0
          • A anton_l

            It seems that problem is in semicolon after "if" statement. DoSomeThing will be fired any time the code executes.

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

            anton_l wrote:

            It seems that problem is in semicolon after "if" statement.

            It seems that's the reason he posted it! :laugh:

            1 Reply Last reply
            0
            • K Klaus Werner Konrad

              Wich compiler ?

              while (*dest++ = *source++);

              is completely correct, isn't it ?

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

              Klaus-Werner Konrad wrote:

              Wich compiler?

              FTFY: Witch compiler Actually, in this case the C# produces three useless wormings: both for the "while(...);" (an empty statment), "x=y" (an assigment instead of a comparison) and the "*" (an "unsafe" code), does it?

              Greetings - Jacek

              K B 2 Replies Last reply
              0
              • R Ravi Bhavnani

                Does that even compile? /ravi

                My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                T Offline
                T Offline
                Thomas Daniels
                wrote on last edited by
                #29

                No, it doesn't. But that's just the bug: it doesn't compile! [EDIT] I'm sorry, I didn't see the semicolon after the if statement. That's the bug! :doh:

                The quick red ProgramFOX jumps right over the Lazy<Dog>. My latest article: Understand how bitwise operators work (C# and VB.NET examples) My group: C# Programmers Group

                1 Reply Last reply
                0
                • L Lutoslaw

                  Klaus-Werner Konrad wrote:

                  Wich compiler?

                  FTFY: Witch compiler Actually, in this case the C# produces three useless wormings: both for the "while(...);" (an empty statment), "x=y" (an assigment instead of a comparison) and the "*" (an "unsafe" code), does it?

                  Greetings - Jacek

                  K Offline
                  K Offline
                  Klaus Werner Konrad
                  wrote on last edited by
                  #30

                  Thanks for the correction. My example was - as a reply to the mention of C, of course a C code snippet, and is the full working function body for strcpy(). Of course, it's unsafe - but lightning fast :-)

                  B 1 Reply Last reply
                  0
                  • L Lutoslaw

                    Klaus-Werner Konrad wrote:

                    Wich compiler?

                    FTFY: Witch compiler Actually, in this case the C# produces three useless wormings: both for the "while(...);" (an empty statment), "x=y" (an assigment instead of a comparison) and the "*" (an "unsafe" code), does it?

                    Greetings - Jacek

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

                    They're not useless warnings, they're warning you that you did something unintended. Actually this wouldn't compile at all in C#, even with unsafe mode turned on, because the result type isn't boolean. It's a classic and well known piece of C code, and I think you only got a warning for the empty loop body (and if you did if(a = 3) by accident you were just screwed, hence writing if(3 == a) instead which is an error if you screw it up).

                    L 1 Reply Last reply
                    0
                    • K Klaus Werner Konrad

                      Thanks for the correction. My example was - as a reply to the mention of C, of course a C code snippet, and is the full working function body for strcpy(). Of course, it's unsafe - but lightning fast :-)

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

                      Just in case you didn't get the joke there, he's making a funny about the compiler being witchcraft. The word you meant to use is 'which'.

                      1 Reply Last reply
                      0
                      • B BobJanova

                        They're not useless warnings, they're warning you that you did something unintended. Actually this wouldn't compile at all in C#, even with unsafe mode turned on, because the result type isn't boolean. It's a classic and well known piece of C code, and I think you only got a warning for the empty loop body (and if you did if(a = 3) by accident you were just screwed, hence writing if(3 == a) instead which is an error if you screw it up).

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

                        Right. :thumbsup:

                        Greetings - Jacek

                        1 Reply Last reply
                        0
                        • D Delphi4ever

                          if(SomeThing == SomeOtherThing); { DoSomeThing; } This one has been sitting in the codebase for a couple of years... :(( At least it did SomeThing...

                          A Offline
                          A Offline
                          Adam David Hill
                          wrote on last edited by
                          #34

                          Ooh, nasty! Couldn't see it at first.

                          Check out my latest article: Celerity: How it was all done. A complete how-to on our sensor-driven head-tracking virtual reality tunnel game in C#.

                          1 Reply Last reply
                          0
                          • D Delphi4ever

                            if(SomeThing == SomeOtherThing); { DoSomeThing; } This one has been sitting in the codebase for a couple of years... :(( At least it did SomeThing...

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

                            Delphi4ever wrote:

                            if(SomeThing == SomeOtherThing);

                            I remember years ago spending a few hours debugging why (in C++):

                            for (int i=0; i<10; i++);
                            DoSomething();

                            where DoSomething executed only once. I only had to learn that lesson once! :rolleyes: Marc

                            Testers Wanted!
                            Latest Article: User Authentication on Ruby on Rails - the definitive how to
                            My Blog

                            1 Reply Last reply
                            0
                            • G Gary Wheeler

                              I've worked with people who did this:

                              if condition
                              DoSomething();
                              else
                              {
                              DoOtherThing1();
                              DoOtherThing2();
                              }

                              or

                              if condition
                              {
                              DoSomething1();
                              DoSomething2();
                              }
                              else
                              DoOtherThing();

                              Both of which give me the creeping heebie-jeebies.

                              Software Zen: delete this;

                              C Offline
                              C Offline
                              cjb110
                              wrote on last edited by
                              #36

                              oh, urm, oops? I do that...

                              G 1 Reply Last reply
                              0
                              • C cjb110

                                oh, urm, oops? I do that...

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

                                It's valid syntax, and if you're confident that you'll never ever forget to add or remove braces appropriately, go for it.

                                Software Zen: delete this;

                                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