Yart C# Coding Standards

 

Philosophy

by Petras Surna

The purpose of coding standards is to:

  • To prevent confusion.
  • To enable you to find things.
  • Make the code look like it was written by the same person no matter who actually wrote it. Granted, this is probably impossible to achieve, but standards help.

This standard has evolved by democratic resolution. If you feel there is something silly in here or something should be added, suggest it. We will take a vote and if the majority agree, the standard will be changed.

Mandatory Requirements

1. Classes must have the following regions defined in them.

 public class Sample
  {
    #region *********************** Constants ***********************
    #endregion

    #region *********************** Fields ***********************
    #endregion

    #region *********************** Properties ***********************
    #endregion

    #region *********************** Initialisation ***********************
    #endregion

    #region *********************** Methods ***********************
    #endregion

    #region *********************** Interfaces ***********************
    #endregion
  }

2. Within a region, all methods and properties are ordered alphabetically and never abbreviated.

#region *********************** Properties ***********************

public string AlternativeName {get; set;} 
public string FirstName {get; set;}
public string LastName {get; set;}

#endregion

We never abbreviate like this "LName" for LastName". This is never does in class names, fields names, property names - anywhere.

3. Method, property and variable names follow this convention

//class level variables - underscore in front of the name
private string _LastName; 

//constants are upper case
private const string LAST_NAME = "Last Name"; 

//method level variables - lower case first letter
private string lastName; 

//property - no abbreviations, capital letter for start of each word, no underscores
public string LastName {get; set;}

public string LastName ()

public string SomeMethod()
{
  //method variable - first character is lower case
  string lastName;
}

//Method variable names have lower case first letter
public string SomeMethod(int lastName)
{
  LastName = lastName;//assiging lastName to a Property
}

4. Controls in WebForms should have an ID that is prefixed by the first and last letter of the Control

In a MVC project or a WebForms project, never write code that operates on or filters a model. Any filtering in a model belongs in the Model itself

eg Don't do this:

var clients = Clients.Addresses.Where(a => a.PostCode == postocde.Text);

Instead, put this code in the Model:

public Clients
{
  public IEnumerable Addresses(string postcode)
  {
    return Clients.Addresses.Where(a => a.PostCode == postocde.Text);
  }
}

This may seem a bit harsh, but the principal stops Model code creeping into the code behind or Controllers.

5. Comments should be written for all public Methods and Properties. A comment should tell you why you did something, not what the method does. This can be difficult to achieve.

Here is why a comment telling you "what" something does is useless:

//Returns the first name
public string FirstName {get; set;}

Obviously a totally useless comment.

Here is a better comment telling you why you did something:

//FirstName and LastName completely define someone's name
public string FirstName {get; set;}

The comment above is not earth shattering as the Property itself is pretty obvious. But at least it tells you something about the idea of the Property rather than simply stating what the Property does.

6. A Property or Method should do "one thing". If the code in a property or a method exceeds one screen in length it must be split into two methods.

Database Conventions

1. All code representing a "Model" must be stored in a separate project. eg

class Person
{
  public string FirstName {get; set;}
  public string LastName {get; set;}
}

If this code was used in a WebForms project, the Person class and its accompanying methods must be located in another project called "Model" or something similar. The same goes for MVC, the Model is in another project from the MVC application.

2. Create Models using Entity Framework 4. Never start using a Model unless the Model is discussed and approved by another programmer. It is notoriously difficult to see errors/improvements in your own attempts to generate a Model but other programmers see them almost instantly.

Once you have create a Model, duplicate the partial classes produced by EF to add your business logic to.

3. Never define a connection string to a database in more than one place in Web.Config.

4. Database table names are plural eg Orders is a table of many Order objects.

Share