Please don’t confuse efficiency with effectiveness – this is what people do when they think less code is better code. Recently I was asked by a new C# developer about code to trim all characters after the fourth character after a period. He said he could not use rounding because of the business he was in. I sent him this code:

string Laptime = "9.2345";
 int LocationOfDecimal = Laptime.IndexOf(".");
 int NumberOfCharsToKeep = 4;
string LapTimeCleanedUp = Laptime.Remove(LocationOfDecimal + NumberOfCharsToKeep);

Note - This assumes that there is always a decimal.

IndexOf might return a negative one, in which case you might want to handle that condition.

Someone else sent him this code:

try

{

    int index = Laptime.IndexOf('.');

    Laptime = Laptime.RemoveAt(index+1,1);

}

catch {}

In the end, this is what the new developer said, “I like Joe’s solution...it is very descriptive and helps me understand the process; however XXX’s is the one I'm implementing (quick and elegant).”

Oh woe is me.