Sunday, June 24, 2007

Some Best Practices to follow when coding .NET languages - Part 3

"The Fundamental Theorem of Formatting is that good visual layout shows the logical structure of a program." - Steve McConnell

To continue the series, we start tackling proper code formatting. This topic is very broad so I'll try to compress it as much as possible and hopefully deliver the message properly.

To start, I'll reiterate the term self-documenting code, which generally means that when someone read and maintains your code, it should be as fluid as possible without having to guess what this procedure does or what these variables mean. It should be like reading a book where everything is straightforword (and enjoyable?).

Refactoring Code

Great algorithms are usually churned out on the fly and are generally very cluttered pieces of work. If you were to maintain the code about 6 months later, I really doubt you would understand the flow or the logic you used before. Sloppy code is still Sloppy code. Everytime you come up with procedures, always try to apply best practices like refactoring commonly used and grouping them into classes or at the very least into methods/procedures. An Example:

using System.Windows.Forms;
using System;

static void main(string[] args)
{
int FirstNumber = 0;
int SecondNumber = 0;
int ThirdNumber = 0;
int ArithmeticResult = 0;

ArithmeticResult = FirstNumber + Second Number;
ArithmeticResult = ArithmeticResult - ThirdNumber;
MessageBox.Show(ArithmeticResult.ToString);


ArithmeticResult = FirstNumber + Second Number;

ArithmeticResult = ArithmeticResult - ThirdNumber;

MessageBox.Show(ArithmeticResult.ToString);

ArithmeticResult = FirstNumber + Second Number;
ArithmeticResult = ArithmeticResult - ThirdNumber;

MessageBox.Show(ArithmeticResult.ToString);

ArithmeticResult = FirstNumber + Second Number;
ArithmeticResult = ArithmeticResult - ThirdNumber;
MessageBox.Show(ArithmeticResult.ToString);
}


I know this may look like a very simple example, but it work to show that this code example could use some refactoring. The first step is to determine the procedures that repeat within a method and replace it with it's own method. We can see that the following lines are repeated:

ArithmeticResult = FirstNumber + Second Number;
ArithmeticResult = ArithmeticResult - ThirdNumber;

MessageBox.Show(ArithmeticResult.ToString);


Refactoring this we have the following method:

public void RefactoredArithmeticMethod(int FirstNumber, int SecondNumber, int ThirdNumber)
{
int
ArithmeticResult = 0;

ArithmeticResult = FirstNumber + Second Number;
ArithmeticResult = ArithmeticResult - ThirdNumber;
MessageBox.Show(ArithmeticResult.ToString);
}


We put the two commonly used operations wintin a new method to reduce the number of lines needed in the main method. Also note that the variable ArithmeticResults' only purpose is to display the arithmetic operation result, so we can move it to the refactored method to reduce it's scope to suit it's purpose.

using System.Windows.Forms;
using System;

static void main(string[] args)
{
int FirstNumber = 0;
int SecondNumber = 0;
int ThirdNumber = 0;

RefactoredArithmeticMethod(FirstNumber, SecondNumber, ThirdNumber);
RefactoredArithmeticMethod(FirstNumber, SecondNumber, ThirdNumber);
RefactoredArithmeticMethod(FirstNumber, SecondNumber, ThirdNumber);
RefactoredArithmeticMethod(FirstNumber, SecondNumber, ThirdNumber);
}


As shown above, doing the refactoring drastically reduces the amount of code to do the exact same thing. And the best thing about refactoring the method is that you can reuse the method anywhere within the namespace it is declared in.

Next in the series I'll be continuing code formatting, focusing on line continuation and line indention. Hope you guys stick around!

Links to the other articles in the Series:
  1. Part 1
  2. Part 2
  3. Part 3


Permalink

No comments: