views:

306

answers:

4

I couldn't find any question that directly applies to my query so I am posting this as a new question. If there is any existing discussion that may help me, please point it out and close the question.

Question:

I am going to do a presentation on C# coding guidelines but it is not supposed to limit to coding standards.

So I have a rough idea but I think I need to address good programing practices. So the contents will be something like this.

  1. Basic coding standards - Casing, Formatting etc.

  2. Good practices - Usage of Hashset over other data structures, String vs String Builder, String's immutability and using them effectively etc

Really I would like to add more good practices (Especially to improve the performance.) So like to hear some more good practices to be used with C#. Any suggestions??? (No need of large descriptions :) Just the idea is sufficient.)

+8  A: 

IDesign Coding Standards

Lance Hunt's C# Coding Standards

Brad Abrams' Internal Coding Guidelines

Unsurprisingly, I just found a SO question: C# Coding standard / Best practices

Mitch Wheat
+10 : unsurpriseingly. I even guess that it was displayed when he wrote his question's title.
Clement Herreman
I checked this, but unfortunately, it isn't displayed with the OP's title.
Steven
A: 
  • Basic Coding Standards - Make sure it's consistent. Even if they don't follow the conventions set out in this document on msdn. I think consistency is really key here.

  • Unit Tests - You cannot go wrong here.

  • Security - Talk about ensuring that if you are passing sensitive data around that it's secure.

  • Performance - You know, I personally feel that getting the application right and then looking at performance is what I do. I do have it in the back of my mind when writing code, so it's little fine tunings that come in at the end.

PieterG
+2  A: 

Here are a few tips:

  1. Use FxCop for static analysis.
  2. Use StyleCop for coding style validation.
  3. Because of the different semantics of value types, supply them with an alternative color in the IDE (go to Tools / Options / Environment / Fonts and Colors / Display Items and supply User Types (Enums) and User Types (Value types) with a value like #DF7120 [223, 113, 32]).
  4. Because exceptions tend to show bugs in your code, let the IDE break on all exceptions. (go to Debug / Exceptions... / Common Language Runtime Exceptions and check Throw).
  5. Project settings: Disallow unsafe code.
  6. Project settings: Threat warnings as errors.
  7. Project settings: Check for arithmetic overflow/underflow.
  8. Use variables for a single, well defined goal.
  9. Don't use magic numbers.
  10. Write short methods. A method should have a single well defined goal.
  11. A method can never be too small (a method of 20 lines is considered pretty big).
  12. A method should protect itself against bad input.
  13. Consider making a type immutable.
  14. Don't suppress warnings in your code with pragma warning disable.
  15. Don't comment bad code: rewrite it.
  16. Document explicitly in code why you are swallowing an exception.
  17. Note the performance implications of concatenating strings.
  18. Never use goto statements.
Steven
+2  A: 

I'm using Microsoft's Design Guidelines for Developing Class Libraries. And I think it is quite good to start with.

Regent