What's the stupidest mistake you ever made?

Coders forum. Discuss techniques, swap code snippets, ask for help etc. relating to all forms of software development.
User avatar
Muttley
Sheepie Whore
Sheepie Whore
Posts: 13169
Joined: Thu Apr 25, 2002 12:30 pm 0
XBL Gamertag: Mu77ley
Location: In a maze of twisty passages, all alike.
Contact:

Post by Muttley »

nyarla wrote:
hairy_pete wrote:

Code: Select all

if (x = 50)
    some_code();
else
    some_other_code();
Hence the coding rule that such equation is to be written with the constant on the left side,

Code: Select all

if (50==x)
   blah();
else
   blurb();
so that it doesn't compile when you write

Code: Select all

if (50=x)


this may be a dumb question...but what's this one all about? i only vaguely know C (i've been trying to mess around with it again recently...). could someone explain this to me? like..what's the difference between "if (x=50)" and "if (50==x)" (or "if (50=x)") ..or am i missing the point entirely? :)
In C, x=50 would set the variable x to 50.
To compare values in C you need to use ==

So 50==x would compile as it's a valid comparison, but 50=x would fail as you're trying to set a variable called "50" to the value x. :)

Muttley
Muttley's Blog: Rarely updated, occasionally interesting… maybe
RetroRemakes Framework: Open Source BlitzMax Game Framework
MuttMod: My Open Source BlitzMax Modules
User avatar
nyarla
Baaaaaa!
Baaaaaa!
Posts: 2820
Joined: Tue Jan 14, 2003 1:53 pm 0
Location: i said kiss me you're beautiful these are truly the last days
Contact:

Post by nyarla »

ahhhhhhh cool. thanks muttley. :)
i remember it took me ages to figure that out when i started playing with C a few years ago (after using Pascal for ages)... i'd forgotten.
/me thinks about sticking to blitzbasic
User avatar
piku
Sheepie Whore
Sheepie Whore
Posts: 20960
Joined: Wed Apr 24, 2002 10:44 am 0
Website: Http://Www.ncot.uk
Location: Wakefield
Contact:

Post by piku »

the Pascal ':=' operator is equivalent to C's '=' operator
the Pascal '=' operator is equivalent to C's '==' operator

So...

:= == =
= == ==

;)
return (0);
User avatar
nyarla
Baaaaaa!
Baaaaaa!
Posts: 2820
Joined: Tue Jan 14, 2003 1:53 pm 0
Location: i said kiss me you're beautiful these are truly the last days
Contact:

Post by nyarla »

:lol:
User avatar
Firefox
Sheepie Whore
Sheepie Whore
Posts: 11412
Joined: Fri Apr 26, 2002 6:15 pm 0
Location: Sheffield

Post by Firefox »

nyarla wrote:could someone explain this to me? like..what's the difference between "if (x=50)" and "if (50==x)" (or "if (50=x)") ..or am i missing the point entirely? :)
It's a way to avoid introducing subtle bugs caused by a common typo. In C the "==" operator tests if the righthand side equals the lefthand side, the "=" operator assigns the value of the righthand side to whatever's on the left. It's easy to type "=" when you really meant "==" in an "if" statement.

So, when you try to do "if (x = 50)" by mistake then it will actually succeed: It will always set x to be 50 instead of testing if x equals 50 like you probably meant, and the condition will always be true because the expression had a nonzero result.

If you try to do "if (50 = x)" the compiler will barf and grind to a halt because you're trying to asign the contents of a variable to an integer constant.

(edit) Doh! I didn't notice there was another page in the thread after the question... :oops:
User avatar
failrate
Baaaaaa!
Baaaaaa!
Posts: 2354
Joined: Sun May 23, 2004 6:54 am 0
Location: Sandy Eggo
Contact:

Post by failrate »

Python barfs if you try to tell it if x=50:
It immediately throws an exception and tells you to fix it, dammit.
User avatar
hairy_pete
Baaaaaaa!
Baaaaaaa!
Posts: 7217
Joined: Mon May 27, 2002 8:10 pm 0
Location: A Tiny Village in Bavaria

Post by hairy_pete »

It isn't like the if (x=<something>) construct isn't useful, you can assign and compare in one go. As in

Code: Select all

if (y=function(x))
foo(y);
else
bar(y);
It's merely dangerous whilst coding, one = short or too many, and the thing doesn't quite do what you intended anymore, the typo being very hard to spot.
User avatar
piku
Sheepie Whore
Sheepie Whore
Posts: 20960
Joined: Wed Apr 24, 2002 10:44 am 0
Website: Http://Www.ncot.uk
Location: Wakefield
Contact:

Post by piku »

C is great.

Doing 'x == 50' does indeed test if x is equal to 50. It then returns either '0' if it doens't match, or '1' if it does.

Doing 'x = 50' sets x to be 50. It also returns 'true' to say the operation worked. I can't remember what the 'true' value is, is it '1', '50' or some random number the compiler decided would be nice?

Either way, if you do

Code: Select all

if (x = 50)
not only are you setting x to be 50, but you're also running whatever is inside your if() loop, since setting a variable to a value is always going to work.

My favourite C constructs though have to be:

Code: Select all

switch foo {
  case 1:
    code();
    code();
  break;
  case 2:
  case 3:
    code();
  break;
}
and

Code: Select all

(x < 50) ? (x) : (50)
Switch statements and complex conditionals rule :)
return (0);
User avatar
bloid
Baaaaaa!
Baaaaaa!
Posts: 4082
Joined: Wed Dec 17, 2003 3:12 pm 0
Location: Mancuniana

Post by bloid »

piku wrote:C is great.

Doing 'x == 50' does indeed test if x is equal to 50. It then returns either '0' if it doens't match, or '1' if it does.

Doing 'x = 50' sets x to be 50. It also returns 'true' to say the operation worked. I can't remember what the 'true' value is, is it '1', '50' or some random number the compiler decided would be nice?
With C, false is 0 and true is any other number...

So the call:

x = 50 ;

evaluates to 50, so:

if( x = 50 )

is true (as 50 is not 0)
"What's there to lose?" said the general, selling our dead meat by the pound...
User avatar
piku
Sheepie Whore
Sheepie Whore
Posts: 20960
Joined: Wed Apr 24, 2002 10:44 am 0
Website: Http://Www.ncot.uk
Location: Wakefield
Contact:

Post by piku »

bloid wrote:So the call:

x = 50 ;

evaluates to 50, so:
Duh :roll: of course it evaluates to 50. That's why you can do

Code: Select all

int x,y,z;
x = y = z = 0;
return (0);
User avatar
donky
Baaa!
Baaa!
Posts: 193
Joined: Fri Mar 28, 2003 7:41 pm 0

Post by donky »

Not backing up my code. My laptop just died and almost everything I ever wrote was on there. Lost it all. Strangely enough, not that concerned about it.
User avatar
piku
Sheepie Whore
Sheepie Whore
Posts: 20960
Joined: Wed Apr 24, 2002 10:44 am 0
Website: Http://Www.ncot.uk
Location: Wakefield
Contact:

Post by piku »

donky wrote:Not backing up my code. My laptop just died and almost everything I ever wrote was on there. Lost it all. Strangely enough, not that concerned about it.
Alright... I can take a hint. This is the second "backup your data" thread I've read today...

My laptop is being sent across to my server as I write this :)
return (0);
User avatar
Firefox
Sheepie Whore
Sheepie Whore
Posts: 11412
Joined: Fri Apr 26, 2002 6:15 pm 0
Location: Sheffield

Post by Firefox »

piku wrote:
donky wrote:Not backing up my code. My laptop just died and almost everything I ever wrote was on there. Lost it all. Strangely enough, not that concerned about it.
Alright... I can take a hint. This is the second "backup your data" thread I've read today...

My laptop is being sent across to my server as I write this :)
Hehe, are the Backup Police out to get you Piku? :lol:
User avatar
tufty
Sheepie Whore
Sheepie Whore
Posts: 12525
Joined: Fri Jan 07, 2005 4:52 pm 0
Location: French Alps

Post by tufty »

nyarla wrote:this may be a dumb question...but what's this one all about? i only vaguely know C (i've been trying to mess around with it again recently...). could someone explain this to me? like..what's the difference between "if (x=50)" and "if (50==x)" (or "if (50=x)") ..or am i missing the point entirely? :)
in C, = is the assignment operator, and == is the equality operator.

Thus, if(x=5) will assign 5 to the variable 'x' and then make a decision based on the validity or otherwise of the result (x, in this case, is not 0, so will always branch, although throwing a warning under gcc at least). if(5=x) will cause a compile-time error which will pick up the (probable) typo.

In some cases, such a syntactical issue is valid, particularly in objective-c initiisiers where teh following:

Code: Select all

if (self = [super init]) {
 ...
}
is common. To avoid a warning, one uses

Code: Select all

if ((self = [super init])) {
}
note the double brackets.

Simon
User avatar
Flabio
Baaaaaaa!
Baaaaaaa!
Posts: 8717
Joined: Wed Mar 03, 2004 12:37 pm 0
Location: Guildford

Post by Flabio »

Friend of mine in e-commerce was writing a system to bill credit cards. He had no stock database to work with, and no test credit card. He ended up billing his own card £200 for a cow. It appeared on his statement as 'a cow'. Took him months to get the cash back :)

Worst thing I've ever done is related to writing a LAN play mode in a previous job. I had two machines set up, and I thoughtfully compiled the exe locally on each machine. The two machines would always drift slowly out of synch with each other. Never occured to me that the second machine would have a different service pack on devstudio, so it was compiling the float code slightly differently. Copied the exe from one to the other, and it all worked fine. Time lost: 2 weeks.
Post Reply