Coding Antipattern - Part 1

I was looking in our codebase and found some "interesting" code.Now, this isn't something that's unique... Every company has some crevice that contains some crap code.The first one is a quickie from the department of backflip double-negatives:

txTotal.Visible = !Settings.IsShowTotalsByGroup ? false : true;

This same pattern was used all over the place. How someone ever came up with such a construct is something that I might never figure out.The second one is a bit more involved... this time from the a string for everything and everything a string department:

List aaa = new List();foreach (DataRow dr in drs){if (_daysWithActivityUniqueValues.ContainsKey(dr)){string t_vID = "" + dr["VehicleId"];if (!allVehicleIds.Contains(t_vID)){foreach (string sss in _daysWithActivityUniqueValues[dr]){//if (!aaa.Contains(sss))//{aaa.Add(sss);//}}allVehicleIds.Add(t_vID);}}}val = "" + aaa.Count;txTotal.Text = Math.Round(double.Parse(val), 2).ToString();if (allVehicleIds.Count == 0){txAvg.Text = "---";}else{txAvg.Text = Math.Round(double.Parse(val)/allVehicleIds.Count, 1).ToString();}

Now I'm not even going to get into the naming scheme with aaa, sss and val. Those are pretty self explanatory for the most part... no, I'm looking at the use of arrays.First off the array is built and stuffed full of strings. I'm not quite sure why since the contents of the array is never used (though some commented out code was using it, again, I'm really not sure how). Here's where it gets to the good part. The size of the array is silently cast to a string by appending it to an empty string, then on the very next line it's parsed to a double. Then, just for good measure it's rounded -- as if it weren't an integer to begin with.What I've been told is that this was written by a couple of consultants. This goes a long way to describing the problem: code needs to have ownership. Someone who is leaving might not have their pride on the line.In any case, I figured you might like a good laugh.  :-D

Previous
Previous

Bike Parts

Next
Next

Teardown Tuesday: Typewriter