.NET Framework - Possible mistaken empty statement

Asked By Jim McGivney on 16-Mar-09 09:44 PM
Why is this a "possible mistaken empty statement" ?

Color Kolor = MyBMP.GetPixel(j, k);
Color TheColor = Color.FromArgb(255,255,0,128);

if (Kolor == TheColor);  //   This is the statement
flagged as possible mistaken empty statement

Thanks in advance for your help.

Peter Duniho replied on 16-Mar-09 09:56 PM
On Mon, 16 Mar 2009 18:44:47 -0700, Jim McGivney <mcgivney@winid.com>

A semi-colon by itself is an empty statement.  By placing a semi-colon
after the "if", you have placed an empty statement after the "if".  The
braces and indentation imply that you want to execute the "Kount++" only
if the condition in the "if" is true, but the way the code is now, it will
always execute.

For what it's worth, a good rule of thumb is that you should have stepped
through in the debugger every line of code you write, at least once for
each possible execution path (for a given conditional...trying to cover
all the possible permutations for all conditionals together is simply
impractical for any interesting program, and sometimes even for smaller
sections of code).

Had you done that, you would have noticed the "Kount++" statement
executing even when "Kolor == TheColor" is false.  :)

Code almost never is correct when an "if" is followed by an empty
statement, and I'd argue that even if someone intentionally did that, they
shouldn't have (i.e. because the only valid reason to do that is if some
other functionality was embedded in the "if" statement itself as
side-effects, which itself would be a terrible design
practice...fortunately, I've never seen anyone actually do that :) ).