Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • .Net some good coding practices

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 215
    Comment on it

    Code practice for Readability

    1. Avoid unnecessary regions in class :
      • A class is based on Single responsibility principle.
      • We provide regions in class to surround block of code. I could be single or multiple methods.
      • Regions makes it easier to navigate around large files.
      • But if a class does too many things it will violate Single responsibility principle.
      • So for different kind of task use different class.

    2. Prefer short methods

    3. In most of the projects , we have complex logics which result into large methods. The best practice says , method should be ended in 1-10 LOC. When we have a method with large LOCs, select block of codes and use extract method. This ends up in making another method for a selected purpose. This helps in increasing readability of the code.

    4. Avoid too many parameters

    5. Instead of using too many parameters its better to make a class with required parameters.

    6. Avoid complex expressions

    7. Its better to have a property with complex logics.

    8. Consider the project warnings also. On building , we get errors and warnings both. Usually we solve error and ignore warnings. This is a bad practice. We should have project level settings to give error for more than 4 warnings. Project -> csharp-code Properties -> build -> Error and warnings

    http://www.codeproject.com/Articles/539179/Some-practices-to-write-better-Csharp-NET-code

    Code practices for exceptions

    1. Report execution failure by reporting exceptions.
    2. If a method is failed to do any task its assigned , its a exception. Return it with a proper message.

    3. System.Environment.FailFast(system.String)
    4. If its not safe to continue executing further , terminate the process using System.Environment.FailFast(System.String) instead for throwing exception.

    5. Throwing exceptions can negatively impact performance.
    6. Please refer to : https://msdn.microsoft.com/en-us/library/vstudio/ms229009(v=vs.100).aspx

    7. Do not throw exceptions explicitly from finally block. implicitly thrown exceptions are acceptable
    8. https://msdn.microsoft.com/en-us/library/vstudio/ms229030(v=vs.100).aspx>

    9. Avoid handling non specific exceptions using System.Exception, System.SystemException. These can be used but rarely.This results in unexpected state of the application. Instead of using this, its better to terminate the process.
    10. Do not handle non cls complaint exceptions using parameter less catch blocks.
    11. https://msdn.microsoft.com/en-us/library/vstudio/ms229005(v=vs.100).aspx

    12. For custom exceptions classes, end there names with exception suffix.
    13. Make custom exception classes serializable to support cross platforms.

    Code practice for using Equals() https://msdn.microsoft.com/en-us/library/vstudio/336aedhh(v=vs.100).aspx

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: