c# code convention
DESCRIPTION
TRANSCRIPT
Kerry Jiang
1. Importance of Good Code Convention2. Game: Finding Faults3. Principle of Good Code Convention4. How to write C# code with Good Code Convention?5. XAML Code Convention6. Discussion
Good code convention is a basic factor of high quality software
Good code convention can reduce maintenance cost (Demo) Good code convention can add value on your software Improve code convention is easiest way to improve code quality
Bad code convention interfere team developing Bad code convention may introduce bugs (directly or indirectly) Bad code convention discourage other developers in your team Bad code convention waste developing/testing time
(Fix convention issue, checkout/check in code require time, whether report QA to test changes?)
Demo Code
Consistent (consistent scale: class, project, solution, product, company) Consistent space, indent rules Consistent newline rule Consistent name convention etc.
Convenient Convenient for writing (simple, needn’t do more things , follow
English text writing style) Convenient for identification (keep spaces and new lines properly) Convenient for comparing Convenient for comprehension
(No matter how well you document your code, the best way to document your code is still:
Good class name, good function name, good variable name, good filename. The rule of thumb is: Give it a name to describe what it does!)
Clear Clear, organized appearance Clear logic structure
Consistent IDE and IDE Setting Different IDE have different default code styles (e.g. tab in Notepad and
VS) Same IDE have different setting also may have different code style (e.g.
tab size and indent size in VS setting) IDE setting must match your company’s code convention
Let IDE format your code automatically Don’t turn off automatically formatting feature of IDE IDE can fix your most of code convention issues automatically
Take advantage of the IDE’s functions to format your code Format document: CTRL + E, D / CTRL + K, D Format Selection: CTRL + E, F / CTRL + K, F View Whitespaces: Ctrl+E, S (checking spaces and tabs before and
after line) Organize Usings etc.
Follow your company’s code convention strictly, don’t do innovation If your company’s code convention does not match with IDE’s setting, adjustment
your IDE’s settingASP.NET MVC:
http://aspnet.codeplex.com/SourceControl/changeset/view/55373#338569
Review the code on code convention periodically
Learn good code convention from other successful projectsEntLib: http://entlib.codeplex.com/SourceControl/list/changesets
WPF Toolkit: http://wpf.codeplex.com/SourceControl/list/changesets
Consistent
Indent & AligningBad example: See demo
Good example: http://wpf.codeplex.com/SourceControl/changeset/view/
57274#1142515http://wpf.codeplex.com/SourceControl/changeset/view/
57274#564021
Other languages Code Convention
Q & A
Discussion