The mistake you'll probably make!

closed account (3pj6b7Xj)
Beginners are excited about a new language but C++ has one little feature that has always annoyed me, after years of programming, I make the same mistake, time and time again and sometimes, its difficult to find and when I do find it, I get so ticked off.

Look at the following code and tell me what is wrong...

 
if (number=3) sum=4;


This compiles perfect fine but the expression always evalutes to true and sets number to 3 making a bug almost impossible to find if this little if statement was inside a program with thirteen-thousand lines of code...yikes!

The correct way is if (number==3) sum=4; That is how its always done but yet, the potential to trip and fall is there, the = and == operator is a banana skin waiting for you some where on the floor, just when you least expect it, you slip and hit your head.

The text editor I wrote under DJGPP long ago, had a bug like this, when I found the bug after 2 weeks of search and testing, I was furious, it dind't make any sense.....it was at that time that I sat down and did the following...

#define is ==

I went arund every were replacing the == operator with is and my code ended up looking something like...

if (number is 3) sum = 4;

Altho this was probably a very bad idea it made for some rather interesting looking code, in fact I posted the code on a forum way back and people were wondering what wacked version of C++ I had, until I uncovered the #define for is which was converted to == during compile time.

I eventually dropped the habit but when I make the mistake it makes me wonder, why didn't they just use another keyword for the C++ language, drop == altogether and make it "is" which, could stomp the dangerous camoflouge bug that hides itself from view. I know it is a bad idea to add another keyword to the language but you must admit = and == ... people make that mistake on ocassion if they aren't careful and when they do, it could set back the development of a project for one little "typo".

I seriously believe, they need to do something about it, i'm interested in hearing what everybody else has to say about this. Have you had it happened to you?
I think everybody's made this mistake at least once, although IMO most of the confusion is caused by people coming from other languages where = has multiple meanings based on the context (?VB?).

I started with C++ and one of the very first things the book I was reading said was that = and == are very different and you shouldn't mix them up. Since it drew the possibility for mixup to my attention right away and stressed the importance of it, I was always mindful and never really had problems with it.

Although these days, some compilers will issue a warning if you put = in an if statement like that, so it might be moot anyway.

As for whether using = was a good design decision, I don't think it was. I think ?Delphi? had the right idea by using == for comparison and := for assignment. That way it's really hard to mix them up.
I also had this problem a few days back. I was working on something fairly large for my standards, and it becomes verryyyy tedious when things aren't working like you'd want them to, and your IDE isn't showing errors.
Topic archived. No new replies allowed.