Coding Skills for a Windows Form Application - How to code with a readable style
(Page 2 of 5 )
In figure 3-5, you learned some coding rules that you must follow when you enter the code for an application. If you don’t, Visual Studio reports syntax errors that you have to correct before you can continue. You saw how that worked in the last figure.
Besides adhering to the coding rules, though, you should try to write your code so it’s easy to read, debug, and maintain. That’s important for you, but it’s even more important if someone else has to take over the maintenance of your code. You can create more readable code by following the three coding recommendations presented in figure 3-7.
To illustrate, this figure presents two versions of an event handler. Both versions accomplish the same task. As you can see, however, the first one is easier to read than the second one because it follows our coding recommendations.
The first coding recommendation is to use indentation and extra spaces to align related elements in your code. This is possible because you can use one or more spaces, tabs, or returns to separate the elements in a C# statement. In this example, all of the statements within the event handler are indented. In addition, the if-else statements are indented and aligned so you can easily identify the parts of this statement.
The second recommendation is to separate the words, values, and operators in each statement with spaces. In the unreadable code in this figure, for example, you can see that each line of code except for the method declaration includes at least one operator. Because the operators aren’t separated from the word or value on each side of the operator, the code is difficult to read. In contrast, the readable code includes a space on both sides of each operator.
The third recommendation is to use blank lines before and after groups of related statements to set them off from the rest of the code. This too is illustrated by the first method in this figure. Here, the code is separated into five groups of statements. In a short method like this one, this isn’t too important, but it can make a long method much easier to follow.
Throughout this chapter and book, you’ll see code that illustrates the use of these recommendations. You will also receive other coding recommendations that will help you write code that is easy to read, debug, and maintain.
As you enter code, the Code Editor will automatically assist you in formatting your code. When you press the Enter key at the end of a statement, for example, the Editor will indent the next statement to the same level.
A method written in a readable style
private void btnCalculate_Click(object sender, System.EventArgs e)
{
decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
decimal discountPercent = 0m;
if (subtotal >= 500)
{
discountPercent = .2m;
}
else if (subtotal >= 250 && subtotal < 500)
{
discountPercent = .15m;
}
else if (subtotal >= 100 && subtotal < 250)
{
discountPercent = .1m;
}
decimal discountAmount = subtotal * discountPercent;
decimal invoiceTotal = subtotal - discountAmount;
txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");
txtSubtotal.Focus();
}
A method written in an unreadable style
private void btnCalculate_Click(object sender, System.EventArgs e){
decimal subtotal=Convert.ToDecimal(txtSubtotal.Text);
decimal discountPercent=0m;
if (subtotal>=500) discountPercent=.2m;
else if (subtotal>=250&&subtotal<500) discountPercent=.15m;
else if (subtotal>=100&&subtotal<250) discountPercent=.1m;
decimal discountAmount=subtotal*discountPercent;
decimal invoiceTotal=subtotal-discountAmount;
txtDiscountPercent.Text= discountPercent.ToString("p1");
txtDiscountAmount.Text= discountAmount.ToString("c");
txtTotal.Text=invoiceTotal.ToString("c");
txtSubtotal.Focus();
}
Coding recommendations
- Use indentation and extra spaces to align statements and blocks of code so they reflect the structure of the program.
- Use spaces to separate the words, operators, and values in each statement.
- Use blank lines before and after groups of related statements.
Note
- As you enter code in the Code Editor, Visual Studio may adjust its indentation.
Figure 3-7 How to code with a readable style
Next: How to code comments >>
More C# Articles
More By Murach Publishing
|
This article is excerpted from chapter three of the book Murach's C# 2005, written by Joel Murach (Murach, 2005; ISBN: 9781890774370). Check it out today at your favorite bookstore. Buy this book now.
|
|