Tuesday, November 13, 2007 11:15 PM
What's up with me? I'm writing about coding instead of sports or my distaste for when my friends are drunk. How weird is that? Ok, one tidbit, MU won tonight in their second basketball game of the season and will be playing at the Sprint Center here in Kansas City. Pretty cool!
I guess this is another pet peeve inspired post. I want to know how many of you out there try and make your code read like sentences? I do it nearly everywhere. I even try and do it in my naming conventions in SQL. For instances I will make all my table names plural, and row identifiers singular, and in my column names I try not to prefix with the table name. That is something I see a lot. For instance, I see tables all the time named something like Accounts, and then I see columns named Account_Name or Account_Address or Account_Balance when Name, Address and Balance would work just fine. Oh, that just reminded me of another pet peeve. I hate columns that store calculated values, unless there is a good reason for it. Too many things can go wrong with storing a calculated value. Buggy code seems to always lead to values not adding up properly. You have an invoice total of $500.00, but if you sum up the different prices that make that total you get $500.02, but there was some rounding issue and data got messed up and no one ever knew so the company might have lost two cents. Doesn't seem like much, but it can be a pretty big deal. That's my two cents about that! Haha!
Back to my original thoughts. I like to code in a pattern which is easy to read. I typically code in C#, so I'll use it for my examples. It is really easy to write code quickly, and start naming variables a, b, c, etc. I am just as guilty as the next guy on that one. In fact, I think at times in my career I have written some of the hardest to follow code ever. I have also made some pretty nice code. I like to take advantage of collections and arrays using foreach loops. I like to make the code flow as if it were a sentence. Here is an example of what I am talking about:
int sumOfNumbers = 0;
foreach (int number in numbers)
{
sumOfNumbers += number;
}
At face value, that is a pretty simple piece of code. You might be thinking, "What is so readable about that?" You are right, that was pretty basic, but I did some good things. I named my variables in a way that is easy to understand what they are. I used a foreach loop, which makes it a bit easier to follow without casting or conversions making clutter. I used a good operator for adding up my values. It is very clean and concise. It basically reads like a sentence. For each number in my numbers collection, add it to my sum of numbers variable. Let's see what happens when good code goes bad. That was a bad pun. I was thinking about using "Variables Gone Wild", but I thought that was pretty cheesy too!
Here is code that does the exact same thing, but much uglier:
int a = 0;
int i = 0;
while (true)
{
if (i > numbers.Count - 1)
{
break;
}
object b = numbers[i];
a = a + Convert.ToInt32(b);
i = i + 1;
}
I bet you've seen something like that before. I like to think of code blocks like that as a party. The first group shows up and they are looking all clean and dapper. Then the next group shows up and someone spills a drink. Then the next group shows up and steps over the spill and then goes right for the bag of chips and leaves a crumb trail. Before long, the place is a mess and you don't want to touch a thing in fear that you might do more damage than good.
Here is my final attempt to ugly it up by trying to cram it all into one line:
int c=0;for (int j = 0;j<numbers.Count;c+=Convert.ToInt32(numbers[j]),j++){}
I am sure there is a way to write it even uglier than that. I think I am making my point however. Readable code helps us all out in the long run. It makes it much easier to maintain and easier to improve.
As I write these types of blogs I'll probably get a little more complex at times and little basic at others. Today was just a basic example. Feel free to share your nightmare code blocks with me. I was inspired to write this today as I was digging through some old ASP code at work and just kept laughing to myself about how dishelved it actually was. The code has had many people running through it trying not to stomp on someone elses code in fear of breaking something and it has become quite the mess.
Happy blogging! Talk to you soon!