Every programming language has its own coding style. It is very essential that every developer, who uses the language, keeps to its style. When a team of developers work over a project, they should be able to read easily the code written by everyone in the team. It does make sense how the code is written!
Here are some coding rules, when using C#:
- Avoid writing very long methods. A method should typically have 1~25 lines of code. If a method has more than 25 lines of code, you must consider re factoring into separate methods.
- Use String.Empty instead of “”Good:
1if (name == String.Empty) { // do something }
Bad:
1if (name == "") { // do something } - Convert strings to lowercase or upper case before comparing. This will ensure the string will match even if the string being compared has a different case.
1if (name == String.Empty) { // do something }
StyleCop analyzes C# source code to enforce a set of style and consistency rules. It can be run from inside of Visual Studio or integrated into an MSBuild project. Don’t hesitate and download this great tool!