Added consistency in code formatting

2 spaces for tabs, outer element without indentation
namespace bit was incorrectly color formatted, fixed it by adding brackets
Indented continuation of a statement on the next line
Removed //... from class content, added them to method content
Fixed typos where void was used instead of class
Added empty lines between headers and code snippets
Added colons after headers
This commit is contained in:
RunninglVlan
2018-11-05 19:30:29 +02:00
committed by GitHub
parent 4e330f5aaa
commit 4399dcad84
+258 -221
View File
@@ -1,16 +1,16 @@
# C# Coding Standards and Naming Conventions # C# Coding Standards and Naming Conventions
| SQL Server Object Name | Notation | Length | Plural | Prefix | Suffix | Abbreviation | Char Mask |Underscores | | SQL Server Object Name | Notation | Length | Plural | Prefix | Suffix | Abbreviation | Char Mask | Underscores |
|:--------------------------|:-----------|-------:|:-------|:-------|:-------|:-------------|:-------------------|:------------| |:--------------------------|:-----------|-------:|:-------|:-------|:-------|:-------------|:-------------------|:------------|
| Class name | PascalCase | 128 | No | No | Yes | No | [A-z][0-9] | No | | Class name | PascalCase | 128 | No | No | Yes | No | [A-z][0-9] | No |
| Constructor name | PascalCase | 128 | No | No | Yes | No | [A-z][0-9] | No | | Constructor name | PascalCase | 128 | No | No | Yes | No | [A-z][0-9] | No |
| Method name | PascalCase | 128 | Yes | No | No | No | [A-z][0-9] | No | | Method name | PascalCase | 128 | Yes | No | No | No | [A-z][0-9] | No |
| Method arguments | camelCase | 128 | Yes | No | No | Yes | [A-z][0-9] | No | | Method arguments | camelCase | 128 | Yes | No | No | Yes | [A-z][0-9] | No |
| Local variables | camelCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No | | Local variables | camelCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
| Constants name | PascalCase | 50 | No | No | No | No | [A-z][0-9] | No | | Constants name | PascalCase | 50 | No | No | No | No | [A-z][0-9] | No |
| Field name | camelCase | 50 | Yes | No | No | Yes | [A-z][0-9] | Yes | | Field name | camelCase | 50 | Yes | No | No | Yes | [A-z][0-9] | Yes |
| Properties name | PascalCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No | | Properties name | PascalCase | 50 | Yes | No | No | Yes | [A-z][0-9] | No |
| Delegate name | PascalCase | 128 | No | No | Yes | Yes | [A-z] | No | | Delegate name | PascalCase | 128 | No | No | Yes | Yes | [A-z] | No |
| Enum type name | PascalCase | 128 | Yes | No | No | No | [A-z] | No | | Enum type name | PascalCase | 128 | Yes | No | No | No | [A-z] | No |
@@ -19,116 +19,122 @@
```csharp ```csharp
public class ClientActivity public class ClientActivity
{ {
public void ClearStatistics() public void ClearStatistics()
{ {
//... //...
} }
public void CalculateStatistics() public void CalculateStatistics()
{ {
//... //...
} }
} }
``` ```
***Why: consistent with the Microsoft's .NET Framework and easy to read.*** ***Why: consistent with the Microsoft's .NET Framework and easy to read.***
#### 2. Do use camelCasing for method arguments and local variables: #### 2. Do use camelCasing for method arguments and local variables:
```csharp ```csharp
public class UserLog public class UserLog
{ {
public void Add(LogEvent logEvent) public void Add(LogEvent logEvent)
{ {
int itemCount = logEvent.Items.Count; int itemCount = logEvent.Items.Count;
// ... // ...
} }
} }
}
``` ```
***Why: consistent with the Microsoft's .NET Framework and easy to read.*** ***Why: consistent with the Microsoft's .NET Framework and easy to read.***
#### 3. Do not use Hungarian notation or any other type identification in identifiers #### 3. Do not use Hungarian notation or any other type identification in identifiers
```csharp ```csharp
// Correct // Correct
int counter; int counter;
string name; string name;
// Avoid // Avoid
int iCounter; int iCounter;
string strName; string strName;
``` ```
***Why: consistent with the Microsoft's .NET Framework and Visual Studio IDE makes determining types very easy (via tooltips). In general you want to avoid type indicators in any identifier.*** ***Why: consistent with the Microsoft's .NET Framework and Visual Studio IDE makes determining types very easy (via tooltips). In general you want to avoid type indicators in any identifier.***
#### 4. Do not use Screaming Caps for constants or readonly variables #### 4. Do not use Screaming Caps for constants or readonly variables:
```csharp ```csharp
// Correct // Correct
public static const string ShippingType = "DropShip"; public static const string ShippingType = "DropShip";
// Avoid // Avoid
public static const string SHIPPINGTYPE = "DropShip"; public static const string SHIPPINGTYPE = "DropShip";
``` ```
***Why: consistent with the Microsoft's .NET Framework. Caps grap too much attention.*** ***Why: consistent with the Microsoft's .NET Framework. Caps grap too much attention.***
#### 5. Use meaningful names for variables. The following example uses seattleCustomers for customers who are located in Seattle: #### 5. Use meaningful names for variables. The following example uses seattleCustomers for customers who are located in Seattle:
```csharp ```csharp
var seattleCustomers = from cust in customers var seattleCustomers = from cust in customers
where cust.City == "Seattle" where cust.City == "Seattle"
select cust.Name; select cust.Name;
``` ```
***Why: consistent with the Microsoft's .NET Framework and easy to read.*** ***Why: consistent with the Microsoft's .NET Framework and easy to read.***
#### 6. Avoid using Abbreviations. Exceptions: abbreviations commonly used as names, such as Id, Xml, Ftp, Uri. #### 6. Avoid using Abbreviations. Exceptions: abbreviations commonly used as names, such as Id, Xml, Ftp, Uri.
```csharp ```csharp
// Correct // Correct
UserGroup userGroup; UserGroup userGroup;
Assignment employeeAssignment; Assignment employeeAssignment;
// Avoid // Avoid
UserGroup usrGrp; UserGroup usrGrp;
Assignment empAssignment; Assignment empAssignment;
// Exceptions // Exceptions
CustomerId customerId; CustomerId customerId;
XmlDocument xmlDocument; XmlDocument xmlDocument;
FtpHelper ftpHelper; FtpHelper ftpHelper;
UriPart uriPart; UriPart uriPart;
``` ```
***Why: consistent with the Microsoft's .NET Framework and prevents inconsistent abbreviations.*** ***Why: consistent with the Microsoft's .NET Framework and prevents inconsistent abbreviations.***
#### 7. Do use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase) #### 7. Do use PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase):
```csharp ```csharp
HtmlHelper htmlHelper; HtmlHelper htmlHelper;
FtpTransfer ftpTransfer; FtpTransfer ftpTransfer;
UIControl uiControl; UIControl uiControl;
``` ```
***Why: consistent with the Microsoft's .NET Framework. Caps would grap visually too much attention.*** ***Why: consistent with the Microsoft's .NET Framework. Caps would grap visually too much attention.***
#### 8. Do not use Underscores in identifiers. Exception: you can prefix private static variables with an underscore: #### 8. Do not use Underscores in identifiers. Exception: you can prefix private static variables with an underscore:
```csharp ```csharp
// Correct // Correct
public DateTime clientAppointment; public DateTime clientAppointment;
public TimeSpan timeLeft; public TimeSpan timeLeft;
// Avoid // Avoid
public DateTime client_Appointment; public DateTime client_Appointment;
public TimeSpan time_Left; public TimeSpan time_Left;
// Exception (Class field) // Exception (Class field)
private DateTime _registrationDate; private DateTime _registrationDate;
``` ```
***Why: consistent with the Microsoft's .NET Framework and makes code more natural to read (without 'slur'). Also avoids underline stress (inability to see underline).*** ***Why: consistent with the Microsoft's .NET Framework and makes code more natural to read (without 'slur'). Also avoids underline stress (inability to see underline).***
#### 9. Do use predefined type names instead of system type names like Int16, Single, UInt64, etc #### 9. Do use predefined type names instead of system type names like Int16, Single, UInt64, etc.
```csharp ```csharp
// Correct // Correct
string firstName; string firstName;
int lastIndex; int lastIndex;
bool isSaved; bool isSaved;
// Avoid // Avoid
String firstName; String firstName;
Int32 lastIndex; Int32 lastIndex;
Boolean isSaved; Boolean isSaved;
``` ```
***Why: consistent with the Microsoft's .NET Framework and makes code more natural to read.*** ***Why: consistent with the Microsoft's .NET Framework and makes code more natural to read.***
@@ -136,12 +142,12 @@ public class ClientActivity
#### 10. Do use implicit type var for local variable declarations. Exception: primitive types (int, string, double, etc) use predefined names. #### 10. Do use implicit type var for local variable declarations. Exception: primitive types (int, string, double, etc) use predefined names.
```csharp ```csharp
var stream = File.Create(path); var stream = File.Create(path);
var customers = new Dictionary(); var customers = new Dictionary();
// Exceptions // Exceptions
int index = 100; int index = 100;
string timeSheet; string timeSheet;
bool isCompleted; bool isCompleted;
``` ```
***Why: removes clutter, particularly with complex generic types. Type is easily detected with Visual Studio tooltips.*** ***Why: removes clutter, particularly with complex generic types. Type is easily detected with Visual Studio tooltips.***
@@ -149,30 +155,31 @@ public class ClientActivity
#### 11. Do use noun or noun phrases to name a class. #### 11. Do use noun or noun phrases to name a class.
```csharp ```csharp
public class Employee public class Employee
{ {
} }
public class BusinessLocation public class BusinessLocation
{ {
} }
public class DocumentCollection public class DocumentCollection
{ {
} }
``` ```
***Why: consistent with the Microsoft's .NET Framework and easy to remember.*** ***Why: consistent with the Microsoft's .NET Framework and easy to remember.***
#### 12. Do prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives. #### 12. Do prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.
```csharp ```csharp
public interface IShape public interface IShape
{ {
} }
public interface IShapeCollection public interface IShapeCollection
{ {
} }
public interface IGroupable public interface IGroupable
{ {
} }
``` ```
***Why: consistent with the Microsoft's .NET Framework.*** ***Why: consistent with the Microsoft's .NET Framework.***
@@ -180,16 +187,14 @@ public class ClientActivity
#### 13. Do name source files according to their main classes. Exception: file names with partial classes reflect their source or purpose, e.g. designer, generated, etc. #### 13. Do name source files according to their main classes. Exception: file names with partial classes reflect their source or purpose, e.g. designer, generated, etc.
```csharp ```csharp
// Located in Task.cs // Located in Task.cs
public partial class Task public partial class Task
{ {
//... }
} // Located in Task.generated.cs
// Located in Task.generated.cs public partial class Task
public partial class Task {
{ }
//...
}
``` ```
***Why: consistent with the Microsoft practices. Files are alphabetically sorted and partial classes remain adjacent.*** ***Why: consistent with the Microsoft practices. Files are alphabetically sorted and partial classes remain adjacent.***
@@ -197,160 +202,188 @@ public class ClientActivity
#### 14. Do organize namespaces with a clearly defined structure: #### 14. Do organize namespaces with a clearly defined structure:
```csharp ```csharp
// Examples // Examples
namespace Company.Product.Module.SubModule namespace Company.Product.Module.SubModule
namespace Product.Module.Component {
namespace Product.Layer.Module.Group }
namespace Product.Module.Component
{
}
namespace Product.Layer.Module.Group
{
}
``` ```
***Why: consistent with the Microsoft's .NET Framework. Maintains good organization of your code base.*** ***Why: consistent with the Microsoft's .NET Framework. Maintains good organization of your code base.***
#### 15. Do vertically align curly brackets: #### 15. Do vertically align curly brackets:
```csharp ```csharp
// Correct // Correct
class Program class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
} //...
} }
}
``` ```
***Why: Microsoft has a different standard, but developers have overwhelmingly preferred vertically aligned brackets.*** ***Why: Microsoft has a different standard, but developers have overwhelmingly preferred vertically aligned brackets.***
#### 16. Do declare all member variables at the top of a class, with static variables at the very top. #### 16. Do declare all member variables at the top of a class, with static variables at the very top.
```csharp ```csharp
// Correct // Correct
public class Account public class Account
{ {
public static string BankName; public static string BankName;
public static decimal Reserves; public static decimal Reserves;
public string Number {get; set;} public string Number { get; set; }
public DateTime DateOpened {get; set;} public DateTime DateOpened { get; set; }
public DateTime DateClosed {get; set;} public DateTime DateClosed { get; set; }
public decimal Balance {get; set;} public decimal Balance { get; set; }
// Constructor // Constructor
public Account() public Account()
{ {
// ... // ...
} }
} }
``` ```
***Why: generally accepted practice that prevents the need to hunt for variable declarations.*** ***Why: generally accepted practice that prevents the need to hunt for variable declarations.***
#### 17. Do use singular names for enums. Exception: bit field enums. #### 17. Do use singular names for enums. Exception: bit field enums.
```csharp ```csharp
// Correct // Correct
public enum Color public enum Color
{ {
Red, Red,
Green, Green,
Blue, Blue,
Yellow, Yellow,
Magenta, Magenta,
Cyan Cyan
} }
// Exception // Exception
[Flags] [Flags]
public enum Dockings public enum Dockings
{ {
None = 0, None = 0,
Top = 1, Top = 1,
Right = 2, Right = 2,
Bottom = 4, Bottom = 4,
Left = 8 Left = 8
} }
``` ```
***Why: consistent with the Microsoft's .NET Framework and makes the code more natural to read. Plural flags because enum can hold multiple values (using bitwise 'OR').*** ***Why: consistent with the Microsoft's .NET Framework and makes the code more natural to read. Plural flags because enum can hold multiple values (using bitwise 'OR').***
#### 18. Do not explicitly specify a type of an enum or values of enums (except bit fields) #### 18. Do not explicitly specify a type of an enum or values of enums (except bit fields):
```csharp ```csharp
// Don't // Don't
public enum Direction : long public enum Direction : long
{ {
North = 1, North = 1,
East = 2, East = 2,
South = 3, South = 3,
West = 4 West = 4
} }
// Correct // Correct
public enum Direction public enum Direction
{ {
North, North,
East, East,
South, South,
West West
} }
``` ```
***Why: can create confusion when relying on actual types and values.*** ***Why: can create confusion when relying on actual types and values.***
#### 19. Do not use an "Enum" suffix in enum type names. #### 19. Do not use an "Enum" suffix in enum type names:
```csharp ```csharp
// Don't // Don't
public enum CoinEnum public enum CoinEnum
{ {
Penny, Penny,
Nickel, Nickel,
Dime, Dime,
Quarter, Quarter,
Dollar Dollar
} }
// Correct // Correct
public enum Coin public enum Coin
{ {
Penny, Penny,
Nickel, Nickel,
Dime, Dime,
Quarter, Quarter,
Dollar Dollar
} }
``` ```
***Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.*** ***Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.***
#### 20. Do not use "Flag" or Flags" suffixes in enum type names. #### 20. Do not use "Flag" or "Flags" suffixes in enum type names:
```csharp ```csharp
//Don't // Don't
[Flags] [Flags]
public enum DockingsFlags public enum DockingsFlags
{ {
None = 0, None = 0,
Top = 1, Top = 1,
Right = 2, Right = 2,
Bottom = 4, Bottom = 4,
Left = 8 Left = 8
} }
//Correct // Correct
[Flags] [Flags]
public enum Dockings public enum Dockings
{ {
None = 0, None = 0,
Top = 1, Top = 1,
Right = 2, Right = 2,
Bottom = 4, Bottom = 4,
Left = 8 Left = 8
} }
``` ```
***Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.*** ***Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.***
#### 21. Do use suffix EventArgs at creation of the new classes comprising the information on event: #### 21. Do use suffix EventArgs at creation of the new classes comprising the information on event:
```csharp ```csharp
// Correct // Correct
public void BarcodeReadEventArgs : System.EventArgs public class BarcodeReadEventArgs : System.EventArgs
{
}
``` ```
***Why: consistent with the Microsoft's .NET Framework and easy to read.*** ***Why: consistent with the Microsoft's .NET Framework and easy to read.***
#### 22. Do name event handlers (delegates used as types of events) with the "EventHandler" suffix, as shown in the following example: #### 22. Do name event handlers (delegates used as types of events) with the "EventHandler" suffix, as shown in the following example:
```csharp ```csharp
public delegate void ReadBarcodeEventHandler(object sender, ReadBarcodeEventArgs e); public delegate void ReadBarcodeEventHandler(object sender, ReadBarcodeEventArgs e);
``` ```
***Why: consistent with the Microsoft's .NET Framework and easy to read.*** ***Why: consistent with the Microsoft's .NET Framework and easy to read.***
#### 23. Do not create names of parametres in methods (or constructors) which differ only the register: #### 23. Do not create names of parametres in methods (or constructors) which differ only by the register:
```csharp ```csharp
// Avoid // Avoid
private void MyFunction(string name, string Name) private void MyFunction(string name, string Name)
{
//...
}
``` ```
***Why: consistent with the Microsoft's .NET Framework and easy to read, and also excludes possibility of occurrence of conflict situations.*** ***Why: consistent with the Microsoft's .NET Framework and easy to read, and also excludes possibility of occurrence of conflict situations.***
#### 24. DO use two parameters named sender and e in event handlers. The sender parameter represents the object that raised the event. The sender parameter is typically of type object, even if it is possible to employ a more specific type. #### 24. DO use two parameters named sender and e in event handlers. The sender parameter represents the object that raised the event. The sender parameter is typically of type object, even if it is possible to employ a more specific type.
@@ -360,10 +393,14 @@ public class ClientActivity
***Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.*** ***Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.***
#### 25. Do use suffix Exception at creation of the new classes comprising the information on exception: #### 25. Do use suffix Exception at creation of the new classes comprising the information on exception:
```csharp ```csharp
// Correct // Correct
public void BarcodeReadException : System.Exception public class BarcodeReadException : System.Exception
{
}
``` ```
***Why: consistent with the Microsoft's .NET Framework and easy to read.*** ***Why: consistent with the Microsoft's .NET Framework and easy to read.***
## Offical Reference ## Offical Reference