Geeks With Blogs

Jeremy Morgan Ramblings of a Polyglot coder
So I started a little project on GitHub of some C# coding guidelines. I want to do this to open up a community driven discussion about guidelines and best practices. I don't want it to be written gospel by me, but rather contributions from other C# coders that we can all learn from. 

So if you feel like helping out make a pull request! I think it would be pretty cool to have inputs from several sources for this. 

Here's some of what I have so far:

This is a set of C# Coding Guidelines I'm putting together for myself and others in the community who would like to contribute. It will be both coding guidelines and tips / best practices for C#.

I have also added some snippets to load into Visual Studio for time saving purposes.

Feel free to contribute!

Code Layout

Use Visual Studio Defaults - 4 character indentation - tabs saved as spaces

Source Code Layout

  • One statement per line
  • One declaration per line

Good:

int price;
int tax;
int total;

Bad:

int price, tax, total;
  • separate method definitions from property definitions ( 1 blank line )
  • bracket on line following statement:

Good:

public int returnInt()
{
    // stuff
}

Bad:

public int returnInt(){
    // stuff
}

Variables

use camel case for fields and local variables:

string myString

Use Pascal casing for all public member, type, and namespace names consisting of multiple words.

(From MSDN)

public class SampleClass
{
   public Color BackColor 
   {
      // Code for Get and Set accessors goes here.
   }
}

Use camel casing for parameters:

public void RemoveString(string ourString) 

DO NOT use Hungarian notation or language specific naming ( IntMyInteger or ConvertToShort )

Classes

A class or interface should have a single purpose.

A class can represent a primitive type or datastructure, abstraction or handle interaction between other classes. But don't mix any of these things. Follow the "Single Responsibility Principle" (from SOLID)

Use design patterns to communicate the intent of the class.

Constructors

Constructors should only be used to create a useful object. If you have too many paramaters in your constructor your class may have too much responsibility.

Use a method instead of a property

Whenever you have a property that:

  • Contains logic
  • performs more work than setting a value
  • returns a different value with similar arguments (rand, guid, etc)
  • Changest the state of another property or object

Use a method instead.

Use Pascal casing

Miscellaneous

Throw exceptions not values

It's common for people throw a status value, such as a plain text friendly message or a boolean value to indicate success. Use the exception system to it's advantage. Catch the exception and out output it to a debug console or log it in another way. It's ok to append a friendly message or hint to what may have happened, but don't rely on this. Structured exception handling reduces debugging and repair time dramatically.



About the Author

Jeremy Morgan is a software engineer with over a decade of experience as a polyglot coder, dabbling in both LAMP and .Net worlds equally. He enjoys creating tutorials for new programmers in the hopes it will encourage more people to join this exciting hobby. Check out his programming blog here or say hi on Google+ sometime.

Posted on Friday, April 18, 2014 3:24 PM Object Oriented Programming , Programming , asp , asp.net , C# , .Net , Development , standards | Back to top


Comments on this post: Project: C# Coding Guidelines

# re: Project: C# Coding Guidelines
Requesting Gravatar...
I would really like to apply and use this process. Thanks for sharing. - The Review Solution
Left by Daniel Scott on Aug 07, 2016 5:22 PM

# re: Project: C# Coding Guidelines
Requesting Gravatar...
It’s a tricky question and you have done a great job providing information regarding C# Coding Guidelines. The detailed explanation to each and every topic give a better idea about everything to be considered in coding. I would really be happy to contribute any information if I can. Gems Parties
Left by Taylor Shaw on Apr 11, 2018 1:14 AM

Your comment:
 (will show your gravatar)


Copyright © Jeremy Morgan | Powered by: GeeksWithBlogs.net