C#
  Home arrow C# arrow Page 2 - Coding Skills for a Windows Form Applicati...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Coding Skills for a Windows Form Application
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2006-06-29

    Table of Contents:
  • Coding Skills for a Windows Form Application
  • How to code with a readable style
  • How to code comments
  • How to work with the Text Editor toolbar
  • How to use code snippets

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    More C# Articles
    More By Murach Publishing


       · This article is an excerpt from the book "Murach's C# 2005," published by Murach. We...
     

    Buy this book now. 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.

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT