Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Comments in C#

Posted by Ravi Varma Thumati on February 12, 2015

Comments are used to document what the program does and what specific blocks or lines of code do. C# compiler ignores the comments.

A comment is preceded by a double forward slash. The code of line after the double forward slash is simply ignored by the compiler.

There are 3 types of comments available in C#

  • Single line comments// for single line comments
  • Multiple line comments/* for multi line comments */
  • XML tags comments
    /// XML tags displayed in a code comment

XML Commenting Example

///<summary>

///summary description

///</summary>

///<remarks>

///This is a test.

///</remarks>

 

/// <summary>

/// Connects to the database and attempts to apply

/// all adds, updates and deletes

/// </summary>

/// <param name=”data”>a dataset, passed by reference,

/// that contains all the

/// data for updating>/param>

public void SaveData(ref DataSet data)

{

}

Tips to Comment your code

  • Use comments only for portions of code that are difficult to understand
  • Comments are used to help document what a program does and what the code with it does. Keep the comments simple and direct. Avoid ASCII art, jokes, poetry and hyper verbosity.
  • Make sure that your comments are correct and up-to-date. There is no point in commenting correctly on code if the comments are not changed with the code. Both code and comments must move in parallel, otherwise the comments will actually make life more difficult for developers who maintain your code.
  • During testing, you can comment out lines of code by coding an apostrophe before them. This is useful for testing new statements without deleting the old statement.
  • Align comments in consecutive linesfor multiple lines of code with trailing comments, align the comments so they will be easy to read.

    const int level = 100;                       // The level mark is constant to 100 can’t be changed
    const decimal turnaround = 45.67;    // The turnaround is constant and can’t be placed more
    Some developers use tabs to align comments, while others use spaces. Because tab stops can vary among editors and IDEs, the best approach is to use spaces.

  • Use paragraph comments If you comment is very long in line of text break into paragraph show it can be seen in a single view.

    Instead of writing like this:

    // Author Puran Singh Mehra. Xyz Company. Created on 7th December 2008 12:23:34 P.M.

    Break into different lines like this:

    // Author Puran Singh Mehra.
    // Xyz Company.
    // Created on 7th December 2008 12:23:34 P.M.

  • Consistent style of commenting

    some people believe that comments should be written so that non-programmers can understand them. Others believe that comments should be directed at developers only.Comments are consistent and always targeted to the same audience. Comments should target to developers and non-developers.
  • Don’t insult the reader’s intelligence

    Avoid obvious comments such as:int age = 45;
    if (age >= 18)  // If age is greater than 18 a person can vote
    Console.WriteLine(“Vote”);
    else
    Console.WriteLine(“Can’t Vote”);

    This wastes your time writing needless comments and distracts the reader with details that can be easily removed from the code.

  • Comment code while writing it

    Add comments while you write code and it’s fresh in your memory. If you leave comments until the end, it will take you twice as long, if you do it at all. “I have no time to comment,” “I’m in a hurry,” and “The project is delayed” are all simply excuses to avoid documenting your code. Some developers believe you should write comments before code as a way to plan out your ultimate solution.For example:

    protected void Button1_Click(object sender, EventArgs e)
    {
    // On click of button check prior that values are entered properly in order
    // Validate date for range and data types
    // ask before submitting values
    // make this entry in the database
    // then you can follow your code
    }

  • Write comments as if they were for you (in fact, they are)

    When it comes to commenting code, think not only about the developers who will maintain your code in the future, but also think about yourself. In the words of the great Phil Haack:”As soon as a line of code is laid on the screen, you’re in maintenance mode on that piece of code.”

    As a result, we ourselves will be the first beneficiaries (or victims) of our good (or bad) comments.

  • Write comment in a polite manner

    Avoid rude and harsh comments. You never know who may read these comments in the future: your boss, a customer, or the pathetically inept developer you just insulted. So maintain your respect and dignity.
  • The golden rule. 

    Let the code speak for itself. You code should be written in such a way that everyone should easily understand it.For example:

    public Form1()
    {
    InitializeComponent();
    SqlConnection con = new SqlConnection(“Data Source=MCN002;Initial Catalog=puran; Integrated Security=True”);
    string cmd = “Select * from testpuran”;
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter(cmd, con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dataGrid1.DataSource = ds;
    }

 

Leave a comment