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. Clever Code
  4. if(x=5)

if(x=5)

Scheduled Pinned Locked Moved Clever Code
helpc++comarchitecture
33 Posts 14 Posters 84 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.
  • A android_web

    The "best practice" in writing comparisons like this is to put the constant on the LHS, so, instead of "if (x = 5)", you write "if (5 = x)", the compiler will flag the latter as an error. I think I read this trick in one of the Microsoft Programmer's Guides, many many years ago.

    -- God creates; man programs.

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #3

    Yeah, like I said, that's what I did after that.

    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

    1 Reply Last reply
    0
    • C Christian Graus

      Ravi beat me to it, but this is the first thing that comes to mind. I'd been coding C++ for about 8 months, paid employment for about 2. I spent a day looking for this bug, every time I saw the line if (x=5) my brain just turned it in to if (x==5) which is obviously what I meant. I got into the habit of writing if (5 == x ) after that, and every now and again, I'd get the compiler error that told me I'd made the same typo again :-)

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      PJ ArendsP Offline
      PJ ArendsP Offline
      PJ Arends
      wrote on last edited by
      #4

      I always compile at warning level 4 so code like this will generate warning C4706: 'assignment within conditional expression'. So I view this more as a typo than a bug.


      You may be right
      I may be crazy
      -- Billy Joel --

      Within you lies the power for good, use it!!!

      Within you lies the power for good; Use it!

      L 1 Reply Last reply
      0
      • C Christian Graus

        Ravi beat me to it, but this is the first thing that comes to mind. I'd been coding C++ for about 8 months, paid employment for about 2. I spent a day looking for this bug, every time I saw the line if (x=5) my brain just turned it in to if (x==5) which is obviously what I meant. I got into the habit of writing if (5 == x ) after that, and every now and again, I'd get the compiler error that told me I'd made the same typo again :-)

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        K Offline
        K Offline
        Kevin McFarlane
        wrote on last edited by
        #5

        Christian Graus wrote:

        I got into the habit of writing if (5 == x )

        I don't like that but it's quite common and is one reason (amomg many) why, in the newer languages, we shouldn't still be using C-syntax three decades after it was first devised. Still, I don't make this particular mistake. Maybe it's just the way my mind works. (I make all sorts of other mistake of course.:))

        Kevin

        C 1 Reply Last reply
        0
        • C Christian Graus

          Ravi beat me to it, but this is the first thing that comes to mind. I'd been coding C++ for about 8 months, paid employment for about 2. I spent a day looking for this bug, every time I saw the line if (x=5) my brain just turned it in to if (x==5) which is obviously what I meant. I got into the habit of writing if (5 == x ) after that, and every now and again, I'd get the compiler error that told me I'd made the same typo again :-)

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #6

          VB ::-> , atleast VB can sit straight now in this thread. lol btw, if(5==x):omg: My eyes are getting swapped automatically to make it (x==5). I've been trapped by the "=" for few times , but I've become more cautious on this.


          --[:jig:]-- [My Current Status] Link2006 wrote:Let's take it outside of CP Jeremy : Please don't.I would love to see this.I'm making the popcorn already.

          1 Reply Last reply
          0
          • PJ ArendsP PJ Arends

            I always compile at warning level 4 so code like this will generate warning C4706: 'assignment within conditional expression'. So I view this more as a typo than a bug.


            You may be right
            I may be crazy
            -- Billy Joel --

            Within you lies the power for good, use it!!!

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

            In C# if statments can only evaluate booleans so if(x=5) would not give it a boolean and cause a compiler error. C# is the way of the future.

            static int Sqrt(int x) { if (x<0) throw new ArgumentOutOfRangeException(); int temp, y=0, b=0x8000, bshft=15, v=x; do { if (v>=(temp=(y<<1)+b<>=1)>0); return y; :omg:

            S Z W 3 Replies Last reply
            0
            • L Lost User

              In C# if statments can only evaluate booleans so if(x=5) would not give it a boolean and cause a compiler error. C# is the way of the future.

              static int Sqrt(int x) { if (x<0) throw new ArgumentOutOfRangeException(); int temp, y=0, b=0x8000, bshft=15, v=x; do { if (v>=(temp=(y<<1)+b<>=1)>0); return y; :omg:

              S Offline
              S Offline
              Shog9 0
              wrote on last edited by
              #8

              Henize wrote:

              C# is the way of the future.

              Yeah, sure. VB uses context to determine which meaning for "=" you want. Is VB the way of the future? I'm with PJ on this one - it should produce a warning. that's just... The C++ Way.

              ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

              L 1 Reply Last reply
              0
              • S Shog9 0

                Henize wrote:

                C# is the way of the future.

                Yeah, sure. VB uses context to determine which meaning for "=" you want. Is VB the way of the future? I'm with PJ on this one - it should produce a warning. that's just... The C++ Way.

                ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

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

                VB is crap. C# allows flexable syntax and more power with pointers. I guess I should say .NET is the way of the future.

                static int Sqrt(int x) { if (x<0) throw new ArgumentOutOfRangeException(); int temp, y=0, b=0x8000, bshft=15, v=x; do { if (v>=(temp=(y<<1)+b<>=1)>0); return y; :omg:

                1 Reply Last reply
                0
                • C Christian Graus

                  Ravi beat me to it, but this is the first thing that comes to mind. I'd been coding C++ for about 8 months, paid employment for about 2. I spent a day looking for this bug, every time I saw the line if (x=5) my brain just turned it in to if (x==5) which is obviously what I meant. I got into the habit of writing if (5 == x ) after that, and every now and again, I'd get the compiler error that told me I'd made the same typo again :-)

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                  C Offline
                  C Offline
                  Cees Meijer
                  wrote on last edited by
                  #10

                  Hmm. I know you all use Visual C. Though I seldom make this mistake, Borlands C++Builder compiler gives me a neat warning: 'Possibly incorrect statement.:cool:'

                  Cees Meijer Software / Hardware Engineer QMetrix BV Specialists in River and Sea flow measurements.

                  C 1 Reply Last reply
                  0
                  • K Kevin McFarlane

                    Christian Graus wrote:

                    I got into the habit of writing if (5 == x )

                    I don't like that but it's quite common and is one reason (amomg many) why, in the newer languages, we shouldn't still be using C-syntax three decades after it was first devised. Still, I don't make this particular mistake. Maybe it's just the way my mind works. (I make all sorts of other mistake of course.:))

                    Kevin

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #11

                    *grin* I don't believe I've ever made it except as a typo. Yeah, I have abandoned that syntax in C#, as it's no longer necessary.

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                    1 Reply Last reply
                    0
                    • C Cees Meijer

                      Hmm. I know you all use Visual C. Though I seldom make this mistake, Borlands C++Builder compiler gives me a neat warning: 'Possibly incorrect statement.:cool:'

                      Cees Meijer Software / Hardware Engineer QMetrix BV Specialists in River and Sea flow measurements.

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #12

                      So does VC on a level 4. I should mention, I took this job on a project that had been around for some time, and when I started, there were 600 warnings or something. This was the event that finally helped me convince my boss to let me clear all those warnings, and impliment a 'warnings are errors' policy.

                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                      1 Reply Last reply
                      0
                      • C Christian Graus

                        Ravi beat me to it, but this is the first thing that comes to mind. I'd been coding C++ for about 8 months, paid employment for about 2. I spent a day looking for this bug, every time I saw the line if (x=5) my brain just turned it in to if (x==5) which is obviously what I meant. I got into the habit of writing if (5 == x ) after that, and every now and again, I'd get the compiler error that told me I'd made the same typo again :-)

                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                        S Offline
                        S Offline
                        S Douglas
                        wrote on last edited by
                        #13

                        Christian Graus wrote:

                        if (x=5)

                        Hasn't everyone* made this same mistake at one point in time? :doh: Not really wanting to admit this has happened to me on at least two occasions.


                        I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:

                        1 Reply Last reply
                        0
                        • C Christian Graus

                          Ravi beat me to it, but this is the first thing that comes to mind. I'd been coding C++ for about 8 months, paid employment for about 2. I spent a day looking for this bug, every time I saw the line if (x=5) my brain just turned it in to if (x==5) which is obviously what I meant. I got into the habit of writing if (5 == x ) after that, and every now and again, I'd get the compiler error that told me I'd made the same typo again :-)

                          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                          Z Offline
                          Z Offline
                          Zac Howland
                          wrote on last edited by
                          #14

                          Christian Graus wrote:

                          if (5 == x )

                          I've done this for years (after running into that bug a couple times in college and spending days trying to track it down). Interestingly enough, I've run into opposition to it from supervisors when coding standards are discussed. I've never quite understood why they wouldn't want to avoid possible bugs, but sometimes they are almost religious about it.

                          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                          K 1 Reply Last reply
                          0
                          • L Lost User

                            In C# if statments can only evaluate booleans so if(x=5) would not give it a boolean and cause a compiler error. C# is the way of the future.

                            static int Sqrt(int x) { if (x<0) throw new ArgumentOutOfRangeException(); int temp, y=0, b=0x8000, bshft=15, v=x; do { if (v>=(temp=(y<<1)+b<>=1)>0); return y; :omg:

                            Z Offline
                            Z Offline
                            Zac Howland
                            wrote on last edited by
                            #15

                            Henize wrote:

                            In C# if statments can only evaluate booleans so if(x=5) would not give it a boolean and cause a compiler error.

                            Which it "borrowed" from Java ...

                            Henize wrote:

                            C# is the way of the future.

                            Just like you don't use a hammer when you need a screwdriver, you don't use C# (or Java or VB) when you need C++.

                            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                            L 1 Reply Last reply
                            0
                            • Z Zac Howland

                              Christian Graus wrote:

                              if (5 == x )

                              I've done this for years (after running into that bug a couple times in college and spending days trying to track it down). Interestingly enough, I've run into opposition to it from supervisors when coding standards are discussed. I've never quite understood why they wouldn't want to avoid possible bugs, but sometimes they are almost religious about it.

                              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                              K Offline
                              K Offline
                              Kevin McFarlane
                              wrote on last edited by
                              #16

                              Zac Howland wrote:

                              nterestingly enough, I've run into opposition to it from supervisors when coding standards are discussed. I've never quite understood why they wouldn't want to avoid possible bugs, but sometimes they are almost religious about it.

                              I don't especially like this style but it's not the kind of thing I would forbid in a coding standard. It should concentrate on more important things, e.g., in C/C++, uninitialised variables.

                              Kevin

                              Z J 2 Replies Last reply
                              0
                              • K Kevin McFarlane

                                Zac Howland wrote:

                                nterestingly enough, I've run into opposition to it from supervisors when coding standards are discussed. I've never quite understood why they wouldn't want to avoid possible bugs, but sometimes they are almost religious about it.

                                I don't especially like this style but it's not the kind of thing I would forbid in a coding standard. It should concentrate on more important things, e.g., in C/C++, uninitialised variables.

                                Kevin

                                Z Offline
                                Z Offline
                                Zac Howland
                                wrote on last edited by
                                #17

                                Kevin McFarlane wrote:

                                It should concentrate on more important things, e.g., in C/C++, uninitialised variables.

                                Agreed. However, it is interesting what topics actually get fought over most when discussing coding standard guidelines. I've been in meetings with 2 different development teams at 2 different companies and had similar discussions over what I consider to be rather trivial issues.

                                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                K 1 Reply Last reply
                                0
                                • C Christian Graus

                                  Ravi beat me to it, but this is the first thing that comes to mind. I'd been coding C++ for about 8 months, paid employment for about 2. I spent a day looking for this bug, every time I saw the line if (x=5) my brain just turned it in to if (x==5) which is obviously what I meant. I got into the habit of writing if (5 == x ) after that, and every now and again, I'd get the compiler error that told me I'd made the same typo again :-)

                                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                                  S Offline
                                  S Offline
                                  Sarath C
                                  wrote on last edited by
                                  #18

                                  Take care also writing != comparison use if(5 != x) instead if(x != 5) because there's a chance for missing "!"

                                  -Sarath

                                  1 Reply Last reply
                                  0
                                  • K Kevin McFarlane

                                    Zac Howland wrote:

                                    nterestingly enough, I've run into opposition to it from supervisors when coding standards are discussed. I've never quite understood why they wouldn't want to avoid possible bugs, but sometimes they are almost religious about it.

                                    I don't especially like this style but it's not the kind of thing I would forbid in a coding standard. It should concentrate on more important things, e.g., in C/C++, uninitialised variables.

                                    Kevin

                                    J Offline
                                    J Offline
                                    jhwurmbach
                                    wrote on last edited by
                                    #19

                                    [coding standards]

                                    Kevin McFarlane wrote:

                                    It should concentrate on more important things

                                    Like which ways brackets should be placed! :mad: Hating coding style guides and the people that are content in contriving them


                                    "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

                                    K Z 2 Replies Last reply
                                    0
                                    • C Christian Graus

                                      Ravi beat me to it, but this is the first thing that comes to mind. I'd been coding C++ for about 8 months, paid employment for about 2. I spent a day looking for this bug, every time I saw the line if (x=5) my brain just turned it in to if (x==5) which is obviously what I meant. I got into the habit of writing if (5 == x ) after that, and every now and again, I'd get the compiler error that told me I'd made the same typo again :-)

                                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                                      J Offline
                                      J Offline
                                      Jun Du
                                      wrote on last edited by
                                      #20

                                      Christian Graus wrote:

                                      if (5 == x )

                                      Actually, there is quite some resistence to this. Their justification is that you don't avoid bugs by tricks or writing unreadable code. You happen to have such a trick for this case, but many other bugs cannot be avoided by tricks. Sticking to a good discipline is the key. The good thing is when you think of that trick, you are avoiding the bug already, regardless of which way you actually code. :)

                                      Best, Jun

                                      1 Reply Last reply
                                      0
                                      • Z Zac Howland

                                        Kevin McFarlane wrote:

                                        It should concentrate on more important things, e.g., in C/C++, uninitialised variables.

                                        Agreed. However, it is interesting what topics actually get fought over most when discussing coding standard guidelines. I've been in meetings with 2 different development teams at 2 different companies and had similar discussions over what I consider to be rather trivial issues.

                                        If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                        K Offline
                                        K Offline
                                        Kevin McFarlane
                                        wrote on last edited by
                                        #21

                                        Zac Howland wrote:

                                        I've been in meetings with 2 different development teams at 2 different companies and had similar discussions over what I consider to be rather trivial issues.

                                        Guidelines I've come across usually split into recommended and mandatory. Then the things which people disagree over are put in recommendations. But really guidelines, e.g., for C++, should concentrate on the kind of stuff in Scott Meyers's Effective C++ books.

                                        Kevin

                                        Z 1 Reply Last reply
                                        0
                                        • J jhwurmbach

                                          [coding standards]

                                          Kevin McFarlane wrote:

                                          It should concentrate on more important things

                                          Like which ways brackets should be placed! :mad: Hating coding style guides and the people that are content in contriving them


                                          "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

                                          K Offline
                                          K Offline
                                          Kevin McFarlane
                                          wrote on last edited by
                                          #22

                                          jhwurmbach wrote:

                                          Hating coding style guides and the people that are content in contriving them

                                          Well, they should be about Standards rather than style. That's the key. I have my preferences over curly bracket placement but they wouldn't be enforced in a standards document I would write.

                                          Kevin

                                          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