Professional C#_VB .NET Coding Guidelines.pdf

(1108 KB) Pobierz
Developer Performance Tools
CodeIt.Right – Static Code Analysis + Auto Refactoring to Best Practices
CodeIt.Once – Painless Refactoring for .NET TM
PrettyCode.Print – Source Code Documentation Made Easy TM
C#/VB .NET Coding
Guidelines
Steve Sartain
Iridium Software, Senior Architect
October 2006
788049497.008.png 788049497.009.png
2
C#/VB .NET Coding Guidelines
Contents
1
2
7
Code changes made to existing systems not written to this standard
2.2
7
Code written for customers that require that their standards should be adopted
3
8
Overview
3.2
8
Capitalisation Styles
3.2.1
8
Pascal Case
3.2.2
8
Camel Case
3.2.3
8
Uppercase
3.3
9
Case Sensitivity (not applicable to VB)
3.4
10
Abbreviations
3.5
11
Word Choice
3.6
12
Avoid Type Name Confusion
3.7
14
Namespace Naming Guidelines
3.8
15
Class Naming Guidelines
3.9
16
Interface Naming Guidelines
3.10
17
Attribute Naming Guidelines
3.11
17
Enumeration Type Naming Guidelines
3.12
18
Static Field Naming Guidelines
3.13
18
Parameter Naming Guidelines
3.14
18
Method Naming Guidelines
3.15
19
Property Naming Guidelines
3.16
21
Event Naming Guidelines
3.17
22
Control Naming Guidelines
3.17.1
23
Specifying Particular Control Variants
3.17.2
23
Table of Standard Control Prefixes
3.17.3
25
Menu Controls
3.18
25
Data Naming Guidelines
3.18.1
26
Fields in Databases
4
27
Property Usage Guidelines
CodeIt.Right – Static Code Analysis + Auto Refactoring to Best Practices
Copyright © Iridium Software 2006 Partial © SubMain 2006
788049497.010.png
 
788049497.001.png
3
C#/VB .NET Coding Guidelines
4.1.1
27
Property State Issues
4.1.2
31
Raising Property-Changed Events
4.1.3
34
Properties vs. Methods
4.1.4
36
Read-Only and Write-Only Properties
4.1.5
36
Indexed Property Usage
4.2
37
Event Usage Guidelines
4.3
41
Method Usage Guidelines
4.3.1
45
Methods With Variable Number of Arguments
4.4
46
Constructor Usage Guidelines
4.5
48
Field Usage Guidelines
4.6
52
Parameter Usage Guidelines
4.7
54
Type Usage Guidelines
4.8
54
Base Class Usage Guidelines
4.9
54
Base Classes vs. Interfaces
4.9.1
55
Protected Methods and Constructors
4.10
56
Sealed Class Usage Guidelines
4.11
57
Value Type Usage Guidelines
4.12
57
Structure Usage Guidelines
4.13
59
Enum Usage Guidelines
4.14
61
Delegate Usage Guidelines
4.14.1
61
Event notifications
4.14.2
61
Callback functions
4.15
62
Attribute Usage Guidelines
4.16
63
Nested Type Usage Guidelines
5
65
Marshal By Reference
5.1.1
65
Marshal By Reference Guidelines
6
6.1
70
Standard Exception Types
6.2
71
Wrapping Exceptions
7
73
Arrays vs. Collections
7.2
73
Using Indexed Properties in Collections
7.3
73
Array Valued Properties
CodeIt.Right – Static Code Analysis + Auto Refactoring to Best Practices
Copyright © Iridium Software 2006 Partial © SubMain 2006
788049497.002.png
 
788049497.003.png
4
C#/VB .NET Coding Guidelines
7.4
73
Returning Empty Arrays
8
77
Guidelines for Implementing Equals and the Equality Operator (==)
8.1.1
77
Implementing the Equality Operator on Value Types
8.1.2
77
Implementing the Equality Operator on Reference Types
8.1.3
77
Implementing the Equals Method
9
10
80
Implementing Finalize and Dispose to Clean Up Unmanaged Resources
10.2
82
Customizing a Dispose Method Name
10.2.1
82
Finalize
10.2.2
82
Dispose
11
84
Events
11.2
84
Delegates
11.3
84
Interfaces
12
13
88
Protecting Objects with Permissions
13.2
88
Fully Trusted Class Library Code
13.3
89
Precautions for Highly Trusted Code
13.4
89
Performance
13.4.1
89
Summary of Class Security Issues
14
15
93
White Space and Indentation
16
95
XML Comments
16.2
95
In-line Comments
16.3
96
End of Line Comments
17
CodeIt.Right – Static Code Analysis + Auto Refactoring to Best Practices
Copyright © Iridium Software 2006 Partial © SubMain 2006
788049497.004.png
 
788049497.005.png
5
C#/VB .NET Coding Guidelines
18
98
Procedure Length
18.2
99
“If”
18.2.1
Write the nominal path through the code first, then write the exceptions 99
18.2.2
99
Make sure that you branch correctly on equality
18.2.3
99
Put the normal case after the If rather than after the Else
18.2.4
99
Follow the If with a meaningful statement
18.2.5
99
Always at least consider using the Else clause
18.2.6
99
Simplify complicated conditions with Boolean function calls
18.2.7
99
Don't use chains of If statements if a Select Case statement will do
18.3
99
“Select Case”
18.3.1
99
Put the normal case first
18.3.2
100
Order cases by frequency
18.3.3
100
Keep the actions of each case simple
18.3.4
100
Use the Case Else only for legitimate defaults
18.3.5
100
Use Case Else to detect errors.
18.3.6
100
Exceptions to the rule
18.4
101
“Do”
18.4.1
101
Keep the body of a loop visible on the screen at once
18.4.2
101
Limit nesting to three levels
18.5
101
“For”
18.5.1
101
Never omit the loop variable from the Next statement
18.5.2
101
Try not to use i, j and k as the loop variables
18.6
101
“Goto”
18.7
102
“Exit Sub” / “Exit Function” And “Return”
18.8
103
“Exit Do”
19
CodeIt.Right – Static Code Analysis + Auto Refactoring to Best Practices
Copyright © Iridium Software 2006 Partial © SubMain 2006
788049497.006.png
 
788049497.007.png
Zgłoś jeśli naruszono regulamin