Geeks With Blogs
Dave Chestnutt: SparklingCode and CodeGaffes Writing better code; for fun and profit

This CodeGaffe covers two similar problems.  The first one involves Booleans, while the second covers any variable type. 


Here's some code that contains 2 bugs in one line.   This is a practice to avoid.  Can you spot the 2 bugs? 


if (m_condition = true) { // *** DON'T DO THIS!
  // do something


If you had any trouble spotting the problem, that's because you're normal.  And if you didn't have any trouble, that's because you were looking for a problem.  If you didn't know there was anything wrong with that code, you might just have easily read the code as the author undoubtedly intended:


if (m_condition == true) { // legal but error prone
  // do something


And while you wouldn't accidentally leave out one of the equal signs, it's a bear to find this problem when someone else does.  And this is a case where the compiler doesn't help you.  It's quite happy to compile this code.  Which is why we get two bugs: 1) it always sets m_condition to true and, 2) always enters the block of code.


Instead, let a Boolean be a Boolean and test it naturally:


if (m_condition) {    // natural test for bool
  // do something


This is a problem for all three languages, C#, Java, and C++.  But, test Booleans naturally and you'll avoid the problem.


Now, for the second half of the CodeGaffe.  What about other types besides Boolean, like integers?  Don't you have to test them this way?  Well it turns out, that because this was a source of bugs, the designers of Java and C# made the language prevent you from putting anything other than a Boolean expression inside an if statement.  So, dropping an equal sign on the floor will result in a compiler warning.  If you write this it won't compile.  The compiler will save you:


if (m_flag = 3) {    // won't compile in Java and C#
  // do something


But what about C++?  That code will compile, and suffer the same bugs as the first example with Booleans.  So, to be safe, get into the habit of doing this (I know, it looks weird at first):


if (3 == m_flag) {    // put constant first
  // do something


Now the compiler is your friend.  If you inadvertently forget the second equal sign, you'll get a compiler error instead of a run-time bug.  And that's much more productive!  I'll take a compiler error over a runtime bug any day of the week.


Technorati tags: ,

Posted on Monday, February 6, 2006 5:35 PM CodeGaffes | Back to top

Comments on this post: CodeGaffe: Surprising Assignments

# re: CodeGaffe: Surprising Assignments
Requesting Gravatar...
Good post!

Before working with in my current project I coded with C#. So something like

if isGeek = true then
end if

was not possible. And this is good!

Using the equal sign for comparison and for setting values is dangerous!

Use the natural test where possible.
Left by Louis Haussknecht on Feb 07, 2006 3:34 AM

Your comment:
 (will show your gravatar)

Copyright © Dave Chestnutt | Powered by: