Tag Archives: DRY

C# Code Guidelines

Coding Guidelines

Every development team or software development team need guidelines to follow to help them write consistent code that keeps maintenance costs low, and development productivity and code reuse high.

I recently updated the ones I use with my team, and though I’d share them to save others having to create their own.  Feel free to reuse in part or full for yourself or your team, and leave a comment with any general suggestions.  Style can be a very personal thing, so don’t be afraid to adapt them to meet your teams own preferences, the key is that all the team follow the same convention, and wherever possible that convention matches the underlying framework you are using.

Naming Conventions


Use PascalCasing for namespaces, types, and member names.

Use camalCasing for local variables, parameters, and user interface fields.

Use camalCasing with an “m_” prefix for private non-user interface fields.

In 3rd Party Templates or code that uses a “_” prefix within a class rather than a “m_” prefix then be consistant within the class and either rename all to “m_” or continue with the “_” prefix.

Type Notations in Variable Names

Hugarian Notion must not be used – When the type is an important part of the variable’s purpose include it in the name by prefixing or postfixing it to the name without abbreviation.

Do not use abbreviations – If an acronym is widely accepted and used in the framework or toolkit being referenced then it acronym may be used despite the rule to avoid abbreviations.

.NET Word Conventions

Following .NET conventions for the words:

  1. Indexes (not Indices)
  2. UserName (not Username)

Use the following symmetric words when defining pairs of functionality:

  1. Add / Remove
  2. Insert / Delete
  3. Create / Destroy
  4. Initialize / Finalize
  5. Get / Set
  6. LogOn / LogOff
  7. Begin / End
  8. Register / Unregister

Use the following American words rather than the British words for member names:

  1. Color
  2. Initialize

Use of Plural and Singular Words

Name all classes with a singular word or phrase.

Name all collections or with plural words or phrases.

Name all namespaces as plural words or phrases unless the namespace has a special meaning in the MVC framework.

Compatibility with COM and other CLR Languages

Do not name two public or protected members with names that are the same excluding character case.  This would prevent reuse of the classes in languages such as VB.NET.

When an argument for a class is passed in to a method or constructor with the purpose of setting the public member, then use of the same name in camelCase rather than PascalCase is recommend over prefixes or suffixes.


Name all static singletons that initialise themselves “Default” unless there is a specific reason to use another name.

Name all static singletons that do not self-initialise “Current” unless there is a specific reason to use another name.

Simple Names

The following simple names are allowed for the uses specified:

  1. obj – to store a generic object who’s type is unimportant.
  2. i, j, k – for control of loops.
  3. s – to store a string value of a variable already in scope in a different type.
  4. e, ea, ee – for subclasses of EventArgs.
  5. e, ex – for subclasses of Exception.
  6. item, it – for the control variable in LINQ query expressions.

Use of Types in Variable Declarations

Use the most specific type available in a variable declaration that is not initialised in-line.

Use var for types that are initialised in-line.

Do not use var for types that are initialised in-line to values of a standard type (e.g. int, string, decimal etc.).

Use var for variables that are initialised as the result of method calls that return collections.

Use var for variables that are storing anonymous types.

Use var when the exact type of a variables is unimportant to the method as more than a return value from an invocation.

Use object for types that are initialised as the result of method calls where the return type is going to be worked with only via reflection.

Use interfaces for variable types if the work being done is dependent only on the interface.

Use dynamic as a variable type only if the type could not be known at compile time, and the code is not going to reflect on the type.


Constructor Performance

Avoid use of database connections in constructors.

Avoid use of network calls in constructors.

User Interface Design Time Requirements

Constructors for User Interface classes must be design time compatible.

Member Initialisation

Do not add constructor overloads that perform member initialisation as part of its parameters, these should now be handled by the member initialisation syntax.

Factory and Dependency Injection

Provide a zero parameter constructor unless the class is entirely unusable without a parameter.

Ensure the zero parameter constructor is suitable for use in a class factory or service locator.

Ensure the zero parameter constructor is suitable for use in dependency injection.

Ensure constructors do not over initialise to prevent subclasses from changing implementation details.

Virtual Calls

Avoid calling virtual members from constructors as the behaviour is unpredictable.

Static Constructors


Avoid initialising static members within static constructors.

Wherever possible initialise static members on first use.

Asynchronous Code

Use the await and async keywords when creating asynchronous code.

Prefix “Async” onto all methods that need to be awaited.

Do not prefix “Async” onto any method that cannot be awaited, even if the method contains asynchronous code.

When blocking methods are part of a Portable Class Library and cannot use the await and async keywords, use extension methods with the “Async” prefix to provide asynchronous alternatives for platforms that support it.

When to use Properties, Methods, and Extension Methods


Use a property if:

  1. The functionality behaves like a field.
  2. Is a logical attribute of the type.
  3. Is unlikely to throw exceptions.
  4. The contained code has minimum value when debugging.
  5. Does not have a dependency on the order being set.

Never use a property if:

  1. A get implementation is not provided.


Use a method if:

  1. The operation is a conversion.
  2. There is an observable side-effect from the call.
  3. The order of execution is important.
  4. The method may not return.
  5. The method may run code asynchronously.
  6. The result should be cached for reuse within the calling method for performance.

Do not use a method if:

  1. The return value is a collection that remains linked to the instance.

Extension Methods

Use an extension method if:

  1. The operation needs extend a sealed type.
  2. The operation needs to apply to anonymous types.
  3. The operation needs to apply to general IEnumerable types.
  4. Base functionality is provided for an Interface.

Do not use extension methods if:

  1. The behaviour may want to be specialised by a base class.

Place extension methods that form part of a classes or interfaces core API, or platform specific extensions to the core API, in the same namespace as the class, even if it is provided by another assembly.

Always place extension methods that extend core .NET types outside of the System.Collections namespace in a namespace ending in “.Extensions” to avoid littering the namespace of these core objects.

Method Arguments

Name method arguments with names that inform the caller of the intended purpose, not of its internal use.

When a Boolean argument is used as a flag to change fundamental functionality of a method, always call it using the “name: true” style.

Use Boolean arguments called with the “name: true” instead of two member enumerators for all methods, unless the method extends an API where an existing enumerator is better suited.


Catching Exceptions

Only catch exceptions of specific types if the error message being returned will be specialised for the type.

Catch the generic Exception type to isolate calls from the calling code.

A user must be informed of an exception with an error or warning message.

An error or warning message can be omitted if an exception is caught and ignored to avoid a known framework or platform issue and the issue is commented within the catch block.

Do not catch the general Exception type and hide its value from the user.

Throwing Exceptions

Use the existing Exception types to throw your own exceptions.

Only create Exception subclasses when they are to be used multiple times within a library.

LINQ, for, and foreach.

Use LINQ for queries to external data sources.

Use LINQ for queries over in memory data that is designed for use as a dictionary or database.

Use foreach () for iterating over any IEnumerable.

Use var as the item type for the foreach() loop whenever the IEnumerable implements IEnumerable<T>.

Use for() when the iteration is controlled by a numeric block.

Use for() to walk hierarchies.

Use for() or while() when the iteration is controlled by a non-numeric exit condition.

Use do { } while () only where it reduces code compared to a for() or while().


Member Comments

Every public member or class must be commented with an XML comment.

Every protected method and property must be commented.

The comment must be written to explain why somebody would want to invoke the code.

The comment must not be a substitute for a bad member name.

The comment should not attempt to list all exceptions that could be thrown but should exceptions of special Exception subclasses thrown directly by the code.

Do not comment private or internal methods that have obvious use.

Do not comment event handlers unless their implementation is non-obvious.

Ensure member comments are suitable for extraction into API documentation.

Do not repeat the XML comment for overridden methods if the comment has nothing new to add to the base type comment.

Do not repeat the XML comment for the implementation of a member for an interface if the comment has nothing new to add to the interface member comment.

Code Block Comments

Comment code with headers within blocks that perform multiple tasks discrete as part of its implementation.

Comment code blocks that cannot be understood by the code alone.

Comment code wherever a special or magic value is used.

Comment code when a condition in a control block could be misunderstood.

Comment Styles

Use /// Comments when commenting members or classes.

Use // Comments when commenting code blocks or statements

Use /* */ comments only if the comment has to be placed in the middle of a line of code or within a Razor syntax document.

Special Developer Comments

Always mark comments that are reminders of code to fix with “TODO” in upper case followed by a message.  This allows all TODO items to be found and completed or before the release of any solution.

If the last statement of a non-void method is not a return statement, place the lint style comment “/* NOTREACHED */ at the bottom of the method so developers know to maintain this when editing the code.

Braces and Brackets

Brace Positioning

Place opening braces for classes, namespaces, and members on new lines.

Place opening braces for code blocks such as if, for, foreach, while, do, and switch on the same line as the code control statement.

Place all closing braces on new lines.

Place both the opening and closing braces for automatic properties on the same line as the property name e.g. public int MyProperty { get; set; }

Place both the opening and closing braces for anonymous types used as property bags on the same line e.g. Html.Link(“Test”, new { this = “that” })

Place else and else if statements on the same line as the closing brace and keep the next opening brace on the same line too.

Bracket Positioning

Place a space between keywords such as if, for, foreach, and while, and the opening bracket.

Do not place a space between a method name and its opening bracket.

Keep the closing bracket on the same line as the opening bracket in the argument list is small.

If the argument list for a method call is long: keep the opening bracket on the same line as the method name, place each argument on a separate line ending with a comma, and place the closing bracket on a new line.

Increase readability with the var keyword and DRY in C#

When the var keyword was first added to the C# language many developers shyed away from it believing it to be a “Variant” type like found in VB.NET or the equivalent of declaring an variable as an object.  Both of these are wrong, but despite this I’ve come across plenty of companies that still ban the use of var in their coding standards stating that it is not type safe.

The var keyword is completely type safe and is actually the equivalent of you typing the name of the type yourself but deciding it was easier to let the compier type the whole name for you.  I find it better simply to look at the var keyword as a timesaver that instructs thecompiler that there is no reason for you to specify the type because: 1. anybody reading it can see the type immediately.  2. The exact type is unimportant as long as it meets the requirements of the code.

Its also worth pointing out at this stage that with modern IDEs I also consider the use of embedding type names into variable names using Hungarian notation or other similar approaches is also not only bad practice, but dangerous compared to proper use of DRY (Don’t Repeat Yourself) principles.

I use the var keyword all the time and find it makes code more readable, not less readable, especially when working with long type names.

For example line one here is much easier to read than line two. In fact in line two you have to search just to find the name of the variable.

Example 1

MyType hello = new MyType();
System.Collection.Generic.Dictionary<string, MyLibrary.Namespace.MyType2> goodbye = new System.Collection.Generic.Dictionary<string, MyLibrary.Namespace.MyType2>();

Using var both lines are equally easy to understand, and the name of both becomes the primary focus of the line rather than the type:

Example 2

var hello = new MyType();
var goodbye = new System.Collection.Generic.Dictionary<string, MyLibrary.Namespace.MyType2>();

Not to mention that you now have one less place to change if you decide to use MyType3 instead of MyType or MyType2 in this code block.

Using var on lines that already perform a cast can give similar readable and time saving advantages.

Example 3

var world = (Button)sender;
var universe = sender as System.Windows.Form;

I also recommend using var for variables that are used to store results from methods where the type is unimportant either because we simply returning it or passing it to another method, or in the of enumerables, we have to specify the type name when we use it anyway.

Lets have another couple of real world examples:

Example 4

var value1 = GetValueFromDatabase(1);
var value2 = GetValueFromDatabase(2);

var value3 = Combine(value1, value2);
return value3;

Reading the example alone you have no idea what type var is. This can upset some people, but if you are using Visual Studio its easy enough to mouse-over each “var” keyword to see the type that’s being used. But if you stop and think for a moment the reason you don’t know each type is because the current code doesn’t need to know. By practicing DRY here you’ve actually created code that’s much easier to maintain and is completely type safe. If in the future GetValueFromDatabase() was changed to return a decimal instead of an int it wouldn’t matter as long as Combine() had an overload that accepted decimals as parameters, or was changed at the same time. If we don’t use var then we would have to edit the code ourselves to switch value1, value2, and value3 from int to decimal, even though the changes has had no real affect on the current code block.

There are of course times when specifying the type explicitly over using var gives important extra information to the user then it should be used instead of var, but you will find these situations are few and far between. I might choose to use “int” for example if I was getting an int defined and returned from a method call in one line, but only if the fact I was performing some integer rather than floating point maths is important within the current code block. Otherwise I’m just making it hard to change the methods definition to work with double or decimal in the future.

Example 5

var form = new Form1();
var res = form1.ShowDialog();
if (res == DialogResult.Cancel) {
    // ...

We’ve already talked here about why line 1 is good practice, but we’ve been “lazy” on line two and used “var” even though we have full knowledge that the return type will be System.Windows.Forms.DialogResult, and whats more that return type will never change as its part of the core .NET framework. Why is var useful in this context then? The better question is why actually would you “repeat” what you already know and specify later here anyway?

You and everybody else who is used to the System.Windows.Forms namespace knows the DialogResult type inside out. But what about people new to the toolkit, is the code still readable to them? I would argue that because we always specify the enumerable name on use, and the only purpose of the variable res is to be checked, specifying the type explicitly gains us nothing in readability, but does cost us more key presses.

I guess we can’t really say line 2 would “repeat yourself” in the same way as declaring a variable with both an explicit variable type and the new keyword on the same line, but we can say if we explicitly put the type on line 2 then we know we are planning to repeat ourselves on line 3, so here we are practicing Don’t Plan to Repeat Yourself to help us keep the DRY pricinple and keeping our code shorter by result.

If you’ve shed away from the var keyword yourself until now hopefully you are now inspired to give it a try and not just when your forced to using anon types and LINQ. You will find that when you follow the suggestions in this post your code will not only start to practice DRY but will actually increase in readability as the code you write become much more focused on the true dependencies and functionality of the method, and not the types your working with.