Dec 172009
 

Well, this is part 3 already. I hope you’ve enjoyed and appreciated the last 2 part and understand my pain. Well, here’s something that doesn’t seem all too convoluted, but could have been achieved with just 1 line of code. By the way, just for those who don’t know, there isn’t really anything syntactically wrong with any of these code.

[sourcecode language="csharp"]
for (int i = 0; i < 24; i++)
{
string strIndex;
if (i < 10)
{
strIndex = "0" + i.ToString();
}
else
{
strIndex = i.ToString();
}
// … do more stuff with strIndex
}
[/sourcecode]

It’s slightly tougher to understand why this is bad code for those inexperienced. For those you understand why you should not write this code, good for you! You’ve got some developer sense in you. Till the next part, I hope you enjoy this code. Learn not to write like that, please.

Related posts:

  1. Code You Should Not Be Writing – Part 1
  2. Code You Should Not Be Writing – Part 2
  3. Toronto Code Camp Interview
  4. Managed Code within LINQ Pitfall
  5. Complex Properties with Inner Property Persistence persisting a Collection property in a User Custom Control using Code Beside Method
  • http://icelava.net Aaron Seet

    guess the Pad methods are still quite obscure.

  • http://blog.dk.sg DK

    How you shorten to 1 line? I can only shorten to 3.

  • http://www.justinlee.sg Justin Lee

    strIndex = string.Format(“{0:00}”, i);

    Or any other languages with a format string method.