Monday, June 18, 2007

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

To continue the best practices series, we continue with the best practices of declaring and using variables.

Focused and Unfocused Variables

Focused variables are variables that have one purpose and should hold on to a specific value for that purpose only. Unfocused variables on the other hand, are variables that are declared once but used for more than one purpose. An example:

Dim HelloWorld As String

HelloWorld = "Hello World!"

HelloWorld = "Goodbye World!"

This example may seem trivial, variables like HelloWorld can cause really problematic logical errors in larger projects and you have to maintain a lot of watches in debug mode just to maintain the value of unfocused variables. Instead, define specific variables for specific purposes so you don't have to keep guessing the variable's value.

Define Descriptive Variable Names

Avoid variable names like x, y, i, PrintX, doX, and the like. Not only is it a pain to debug these kinds of variables, but you have almost no readability and maintanance can be a nightmare! Your aim (or mission) when writing code is to make the source code self-documenting, which means you can get the purpose of the functions and variables by giving functions and variables descriptive name that help improve code readability. Example of these include: InsertNewStudentRecord, UpdateStudentRecord, StudentFirstName, StudentOverall. Although x, y, and i are generally acceptable variable names for loops, it would be so much better if they also had descriptive names.

Use Mix Cases when declaring variables

Ever encountered or declared variables like this?

//old school variable declaration that is hard to read.

string enrolledstudent = null;
//no better than the first.

Not only are the variables hard to read, our eyes sometimes gets fooled and can't determine where the words start and end. A better way to declare variable is to use camel casing, which is capitalizing the first letter of the variable and the first letters in the succeeding words of the variable name. An example:

string EnrolledStudents = null; //Better, wouldn't you agree?

Camel casing really improves readability and helps describe the variable in a less confusing way. But be sure to be consistent with mixing cases, if you want to use camel casing or even Pascal casing, feel free to do so as long as you stick to your convention. Here's an example of inconsistent mixing of cases:

Dim TeachersAssigned As Integer 'camel casing
Dim subjectFailureRate As Decimal 'pascal casing
Dim Quarterlytuitionfee As Decimal 'confusing mixed case


We tackled three of the most common programmer practices that need work and applied the following best practices to enchance code readability:
Focused and Unfocused Variables, Define Descriptive Variable Names, and Use Mix Cases when declaring variables. I barely scratched the surface of variable best practices, but hopefully the examples and lessons gave you a helpful insight on improving readability and maintainability in your source code.

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


No comments: