.NET Daily

C#

C# Coding Standards Series – Indentation, Spacing and Coding Style

Posted on .

C# Coding Standards Series – Indentation, Spacing and Coding Style

Introduction

This post is part of a series that covers most of notable C# coding standards that you should follow.

You can download the document with all of the post from here: [download id=”2″ format=”1″]

You can see here the other posts within the series.


Each programmer has a unique way of coding. While this fact is not a problem when you do it for yourself, working in an organization and code in your own way may represent a big problem for your team mates.

So it’s very important to follow some indentation and coding style rules for keeping the code clean and readable for all programmers from the project.

1  General Rules

1.1 Use TAB for indentation. Do not use spaces. Recommended TAB size is 4;
1.2 Never declare more than one namespace per file;
1.3 Avoid putting multiple classes in a single file;
1.4 Comments should be in the same level as the code:
Ex:

// Format a message and display
string fullMessage = "Hello " + name;
DateTime currentTime = DateTime.Now;

1.5 Always use curly braces ({ and }) in a new line and in conditional statements:

Ex:

protected void Page_Load(object sender, EventArgs e)
{
     bool IsTrue;
     if (IsTrue == true)
     {
       // Do something;
       //....
       return false;
     }
}

1.6 Use one blank line to separate logical groups of code:
Ex:

public void SayHello(string name)
{
    string fullMessage = "Hello " + name;
    DateTime currentTime = DateTime.Now;

    string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();

    MessageBox.Show(message);
}

2  Grouping Code

2.1 Group internal class implementation by type in the following order:

1) Member variables;

2) Constructors and finalizers;

3) Nested Enums, Structs and Classes;

4) Properties;

5) Methods;

2.2 Sequence declarations within type groups based upon access modifier and visibility:

1) Public;

2) Protected;

3) Internal;

4) Private;

2.3 Use regions (#region) to group related pieces of code together:

Ex:

namespace SampleApplication
{
 public partial class _Default : System.Web.UI.Page
 {

   #region Private Members
   // code here
   #endregion

   #region Constructors
   // code here
   #endregion

   #region Public Properties
   // code here
   #endregion

   #region Private Properties
   // code here
   #endregion

   #region Public Methods
   // code here
   #endregion

   #region Private Methods
   // code here
   #endregion

2.4 Do not hardcode numbers and strings. Use constant variables or resource files;

2.5 Declare readonly or static readonly variables instead of constants for complex types;

2.6 Avoid having very large files. If a file has more than 1000 lines of code then it’s a good candidate for refactoring. Split it logically in two or more classes;

Please share your opinion about this post by commenting below.

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.

There are no comments.

View Comments (0) ...
Navigation

Privacy Preference Center