Coding guidelines

download Coding guidelines

If you can't read please download the document

  • date post

    06-May-2015
  • Category

    Technology

  • view

    662
  • download

    4

Embed Size (px)

description

Coding Guidelines

Transcript of Coding guidelines

  • 1.Microsoft .NET Coding Guidelines and BestPracticesConfidential e-Zest Solutions Ltd.Page 1 of 54

2. Version and HistoryVersion Date Author Reviewed By Comments1.0 23-May-2012Lalit Kale DraftConfidential e-Zest Solutions Ltd. Page 2 of 54 3. Table of Contents1 Overview ................................................................................................................................. 52 Principles and Themes .............................................................................................................. 53 How to follow the standards across the team ........................................................................... 64 General Coding Standards ........................................................................................................ 74.1Clarity and Consistency ...............................................................................................................................74.2Formatting and Style...................................................................................................................................84.3Using Libraries.............................................................................................................................................94.4Variable Declarations and Initalizations .....................................................................................................94.5Function Declarations and Calls ................................................................................................................114.6Enums........................................................................................................................................................124.7Comments .................................................................................................................................................144.7.1Inline Code Comments 165 .NET Coding Standards ........................................................................................................... 195.1Design Guidelines for Developing Class Libraries .....................................................................................195.2Files and Structure ....................................................................................................................................195.3Assembly Properties .................................................................................................................................195.4Naming Convensions.................................................................................................................................195.4.1General Naming Conventions 195.4.2Capitalization Naming Rules for Identifiers195.4.3Hungarian Notation 215.4.4UI Control NamingConventions 215.5Constants ..................................................................................................................................................225.6Strings .......................................................................................................................................................225.7Arrays and Collections ..............................................................................................................................245.8Structures ..................................................................................................................................................265.8.1Structures vs. Classes 275.9Classes .......................................................................................................................................................275.9.1Fields 275.9.2Properties 275.9.3Constructors 285.9.4Methods285.9.5Events 285.9.6Member Overloading 295.9.7Interface Members305.9.8Virtual Members305.9.9Static Classes 30Confidential e-Zest Solutions Ltd. Page 3 of 54 4. 5.9.10Abstract Classes 30 5.10 Namespaces ..............................................................................................................................................31 5.11 Errors and Exceptions ...............................................................................................................................315.11.1Exception Throwing 315.11.2Exception Handling 32 5.12 Resource Cleanup .....................................................................................................................................355.12.1Try-finally Block355.12.2Basic Dispose Pattern375.12.3Finalizable Types455.12.4Overriding Dispose 52Confidential e-Zest Solutions Ltd.Page 4 of 54 5. 1 OverviewThis document defines .NETcoding guidelines for the e-Zest project teams. This standard derives fromthe experience of project and product development efforts and is continuously evolving. If you discovera new best practice or a topic that is not covered, please bring that to the attention of the ArchitectGroup and have the conclusion added to this document.No set of guidelines will satisfy everyone. The goal of a standard is to create efficiencies across all of thedevelopers. Applying a set of well-defined coding standards will result in code with fewer bugs, andbetter maintainability.Adopting an unfamiliar standard may be awkward initially, but the pain fadesquickly and the benefits are quickly realized, especially when you inherit ownership of others code.2 Principles and ThemesHigh-quality code exhibits the following characteristics: 1. Understandable.Code must be clearly readable and straightforward. Code must showcase thekey things it isdesigned and implemented for. The relevant parts of entire project/Solution codeshould be easy to reuse. Projects/Solution should not contain unnecessary code.Projects/Solutionsmust include appropriate documentation. 2. Correct.Projects/Solutions must demonstrateproperly how to perform the key things they aredesigned to do. Projects/Solutions must compile cleanly, run correctly as documented, and betested. 3. Consistent.Projects/Solutions should follow consistent coding style and layout to make the codeeasier to read. Likewise, Projects/Solutions should be consistent with each other to make themeasier to use together. Consistency shows craftsmanship and attention to detail. 4. Safe. Projects/Solutions must comply with legal, privacy, and policy standards. They must notdemonstrate hacks or poor programming practices. 5. Secure.Projects/Solutions should demonstrate how to use secure programming practices suchas least privilege, secure versions of runtime library functions, and SDL-recommended projectsettings. 6. Reliable 7. Maintainable 8. EfficientThe proper use of programming practices, design, and language features determines how wellProjects/Solutions can achieve these. This code standard is designed to help you createProjects/Solutions that serve as best practices for customers to emulate.Confidential e-Zest Solutions Ltd.Page 5 of 54 6. 3 How to follow the standards across the teamIf you have a team of different skills and tastes, you are going to have a tough time convincing everyoneto follow the same standards. The best approach is to have a team meeting and developing your ownstandards document. You may use this document as a template to prepare your own document.Distribute a copy of this document (or your projects tailored coding guidelines document) well ahead ofthe coding standards meeting. All members should come to the meeting prepared to discuss pros andcons of the various points in the document. Make sure you have a technical manager present in themeeting to resolve conflicts.Discuss all points in the document. Everyone may have a different opinion about each point, but at theend of the discussion, all members must agree upon the standard you are going to follow. Prepare anew standards/guidelines document with appropriate changes based on the suggestions from all of theteam members. Print copies of it and post it in all workstations.After you start the development, you must schedule code review meetings to ensure that everyone isfollowing the rules.3 types of code reviews are recommended:1.Peer review another team member review the code to ensure that the code follows the codingstandards and meets requirements. This level of review can include some unit testing also. Every file inthe project must go through this process.2.Architect review the architect of the team must review the core modules of the project to ensurethat they adhere to the design and there are no big mistakes that can affect the project in the longrun.3.Group review randomly select one or more files and conduct a group review once in a week.Distribute a printed copy of the files to all team members 30 minutes before the meeting. Let them readand come up with points for discussion. In the group review meeting, use a projector to display the filecontent in the screen. Go through every sections of the code and let every member give theirsuggestions on how could that piece of code can be written in a better way. (Dont forget to appreciatethe developer for the good work and also make sure he does not get offended by the group attack!)Confidential e-Zest Solutions Ltd.Page 6 of 54 7. 4 General Coding StandardsThese general coding standards can be applied to all languages - they provide high-level guidance to thestyle, formatting and structure of your source code.4.1 Clarity and ConsistencyDo ensure that clarity, readability and transparency are paramount. These coding standards strive toensure that the resultant code is easy to understand and maintain, but nothing beats fundamentallyclear, concise, self-documenting code.Do ensure that when applying these coding standards that theyare applied consistently.1. File name should match with class name. For example, for the class HelloWorld, the file name should be helloworld.cs (or, helloworld.vb)2. Use Pascal Case for file names.3. Do not hardcode numbers. Use constants instead. Declare constant in the top of the file and use it in your code.Howe