Dec 242009
 

This is a special Christmas Eve “Code You Should Not Be Writing” post that I think is special enough to flip over your chair.

This is a common mistake C# developers make, which is forgiveable. You should use string.IsNullOrEmpty() method instead, which was introduced only in .NET Framework 2.0. It is understandable that most developers haven’t gotten up to speed yet.

[sourcecode language="csharp"]
string duration;
// … some code assignment for duration
if (duration == string.Empty || duration == null)
{
// … some error handling code
}
[/sourcecode]

But this just blew my mind. Can anyone guess why this is so very wrong?

[sourcecode language="csharp"]
string finalTitle;
// … some code assignment for finalTitle
if (finalTitle == string.Empty == null)
{
// … some error handling code
}
[/sourcecode]

Yes. You should never write code like this. Heaven’s me, this is valid code! The condition is just WRONG.

Merry Christmas everyone, and I hope you like this edition of “Code You Should Not Be Writing”.

Related posts:

  1. Code You Should Not Be Writing – Part 2
  2. Code You Should Not Be Writing – Part 3
  3. Code You Should Not Be Writing – Part 1
  4. Managed Code within LINQ Pitfall
  5. ManWrap Library – Managed Code in Unmanaged C++!?!
  • http://blog.dk.sg DK

    Which school is he from?
    .-= DK´s last blog ..Christmas Don’t Be Late =-.

  • http://icelava.net Aaron Seet

    No school, perhaps?