Translate

Friday, July 19, 2013

C# Programming Guidelines (C# programming best practices)

Scott Allen  in his plural sight video makes these 10 recommendations for writing quality software



  1. Keep classes small.
  2. Keep methods short. Ideally a method should sit on one screen. If its bigger than that, break it up into smaller methods.
  3. Except for API documentation comments (the one that goes on top of a method with ///) avoid comments. Good code with meaningful method names, is so readable and intention revealing that it does not need comments.
  4. Avoid multiple exit points (multiple returns) from a method. That is bad for code readability. Have just one exit at the bottom of the method.
  5. Encapsulate complex expressions.Instead of having an if condition with many checks, replace it with a method call. 
  6. Set up your project build warning level to 4 and set "Treat warnings as errors" to All.
  7. Avoid having more than 3 parameters to a method. If it needs 4 or more parameters, it should be one parameter which is an instance of a class that has those 4 parameters as properties.
  8. Avoid using boolean parameters to a method, because they reduce the code readability.
  9. Do not use exceptions for jumping around in the code.Use exceptions for errors only.
  10. Avoid using regions in code. According to Scott if your code is well organized using appropriately sized classes (a class that is NOT a swiss army knife) then you do not need regions.

No comments:

Post a Comment

Comments will appear once they have been approved by the moderator