|
1 | | -# C# coding standards |
2 | | - |
3 | | -These are common standards that apply to coding when using C# and are designed to be used in conjunction with the overall common coding standards. |
4 | | - |
5 | | -## Rationale |
6 | | - |
7 | | - - Ensure consistent C# code and styling across all projects |
8 | | - |
9 | | -### Resource |
10 | | - |
11 | | -[Microsoft's standard conventions](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions) |
12 | | - |
13 | | -### Naming Conventions |
14 | | - |
15 | | -- PascalCasing for class names and method names |
16 | | - |
17 | | -```csharp |
18 | | -public class ClientActivity |
19 | | -{ |
20 | | - public void ClearStatistics() |
21 | | - { |
22 | | - // ... |
23 | | - } |
24 | | - |
25 | | - public void CalculateStatistics() |
26 | | - { |
27 | | - // ... |
28 | | - } |
29 | | -} |
30 | | -``` |
31 | | - |
32 | | -- camelCasing for method arguments and local variables |
33 | | - |
34 | | -```csharp |
35 | | -public class UserLog |
36 | | -{ |
37 | | - public void Add(LogEvent logEvent) |
38 | | - { |
39 | | - int itemCount = logEvent.Items.Count; |
40 | | - // ... |
41 | | - } |
42 | | -} |
43 | | -``` |
44 | | - |
45 | | -- do not use underscores in identifiers (you can prefix private static variables with an underscore) |
46 | | -- use predefined type names instead of system type names for example |
47 | | - |
48 | | -```csharp |
49 | | -// Correct |
50 | | -string firstName; |
51 | | -int lastIndex; |
52 | | - |
53 | | -// Avoid |
54 | | -String firstName; |
55 | | -Int32 lastIndex; |
56 | | -``` |
57 | | - |
58 | | -- use noun or noun phrases |
59 | | - |
60 | | -```csharp |
61 | | -public class Employee |
62 | | -{ |
63 | | - |
64 | | -} |
65 | | - |
66 | | -public class BusinessLocation |
67 | | -{ |
68 | | - |
69 | | -} |
70 | | -``` |
71 | | - |
72 | | -- prefix interfaces with the letter `I`. |
73 | | - |
74 | | -```csharp |
75 | | -public interface IShape |
76 | | -{ |
77 | | - |
78 | | -} |
79 | | -``` |
80 | | - |
81 | | -### Layout Conventions |
82 | | - |
83 | | -[CodeMaid](http://www.codemaid.net/) is a free open source Visual Studio Extension which will cleanup and simplify your C# code. It can be downloaded as an extension to visual studio and can run automatically either on save or on demand on either a selection of code or a whole solution. |
84 | | - |
85 | | -1. Remove unused using statements |
86 | | -2. Sort using statements |
87 | | -3. Add unspecified access modifiers |
88 | | -4. Remove empty regions |
89 | | -5. Add blank line padding |
90 | | -6. Remove blank lines next to braces |
91 | | -7. Run Visual Studio formatting |
92 | | -8. Remove consecutive blank lines |
93 | | -9. Remove end of line whitespace |
94 | | -10. Update endregion tags |
95 | | - |
96 | | -The default settings for CodeMaid should remain in place to adhere to [DEFRA principles](../principles/README.md) (details at http://www.codemaid.net/documentation/) |
97 | | - |
98 | | -### Testing |
99 | | - |
100 | | -As per the general coding standards it is expected that 90% of code is covered with unit tests however given unit test coverage in some C# projects (i.e. MVC) will include coverage of default Microsoft template code this 90% figure can be misleading. It is not expected that unit tests should be written to cover code already written by Microsoft and therefore attributes can be added to this code to exclude them from the code coverage calculation. |
101 | | - |
102 | | -For example in a typical MVC application exclusions can be applied to areas such as anything in `App_Start` folder, any migrations, `global.asax`, etc. |
103 | | - |
104 | | -To exclude test code from the code coverage results and only include application code, add the `ExcludeFromCodeCoverageAttribute` attribute to your test class. |
105 | | - |
106 | | -To customize code coverage, follow these steps: |
107 | | - |
108 | | -1. Add a run settings file to your solution. In Solution Explorer, on the shortcut menu of your solution, choose Add > New Item, and select XML File. Save the file with a name such as `CodeCoverage.runsettings`. |
109 | | -2. Add the content from the [example file](https://docs.microsoft.com/en-us/visualstudio/test/customizing-code-coverage-analysis?view=vs-2017#sample-runsettings-file), and then customize it to your needs as described in the sections that follow. |
110 | | -3. To select the run settings file, on the Test menu, choose Test Settings > Select Test Settings File. To specify a run settings file for running tests from the command line or in a build workflow, see Configure unit tests by using a `.runsettings` file. |
111 | | - |
112 | | -When you select Analyze Code Coverage, the configuration information is read from the run settings file. |
113 | | - |
114 | | -To turn the custom settings off and on, deselect or select the file in the Test > Test Settings menu. |
115 | | - |
116 | | -https://docs.microsoft.com/en-us/visualstudio/test/customizing-code-coverage-analysis?view=vs-2017 |
| 1 | +# C# Standards |
| 2 | + |
| 3 | +This document outlines the standards for writing C# code at Defra. To ensure consistency and maintainability, we align with Microsoft's official C# standards and best practices. |
| 4 | + |
| 5 | +## Key Principles |
| 6 | + |
| 7 | +1. **Alignment with Microsoft Standards**: |
| 8 | + - Follow Microsoft's official C# coding conventions for: |
| 9 | + - Naming conventions |
| 10 | + - Indentation |
| 11 | + - Code formatting |
| 12 | + - Linting |
| 13 | + - Refer to the following resources: |
| 14 | + - [C# Coding Conventions](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions) |
| 15 | + - [Framework Design Guidelines](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/) |
| 16 | + |
| 17 | +2. **Modern C# Practices**: |
| 18 | + - Use modern C# features such as: |
| 19 | + - `async`/`await` for asynchronous programming. |
| 20 | + - Nullable reference types (`string?`) to avoid null reference exceptions. |
| 21 | + - Pattern matching for cleaner and more readable code. |
| 22 | + - Records for immutable data models. |
| 23 | + - Avoid outdated practices and legacy patterns unless required for compatibility. |
| 24 | + |
| 25 | +3. **SOLID Principles**: |
| 26 | + - Follow the **SOLID principles** to ensure your code is maintainable, scalable, and easy to understand. These principles are: |
| 27 | + - **S - Single Responsibility Principle (SRP)**: |
| 28 | + - A class should have only one reason to change. Each class should focus on a single responsibility or functionality. |
| 29 | + - **O - Open/Closed Principle (OCP)**: |
| 30 | + - Classes should be open for extension but closed for modification. This allows new functionality to be added without altering existing code. |
| 31 | + - **L - Liskov Substitution Principle (LSP)**: |
| 32 | + - Subtypes must be substitutable for their base types without altering the correctness of the program. |
| 33 | + - **I - Interface Segregation Principle (ISP)**: |
| 34 | + - A class should not be forced to implement interfaces it does not use. Large interfaces should be split into smaller, more specific ones. |
| 35 | + - **D - Dependency Inversion Principle (DIP)**: |
| 36 | + - High-level modules should not depend on low-level modules. Both should depend on abstractions, often achieved through dependency injection. |
| 37 | + |
| 38 | + For a detailed explanation of the issues with violating SOLID principles, refer to [Microsoft's overview](https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/may/csharp-best-practices-dangers-of-violating-solid-principles-in-csharp). |
| 39 | + |
| 40 | +4. **Testing**: |
| 41 | + - Write unit tests for all business logic. |
| 42 | + - Use mocking frameworks for dependency isolation. |
| 43 | + - Focus on meaningful code coverage rather than arbitrary metrics. |
| 44 | + |
| 45 | +## Additional Resources |
| 46 | + |
| 47 | +For further guidance, refer to the following Microsoft resources: |
| 48 | +- [C# Language Reference](https://learn.microsoft.com/en-us/dotnet/csharp/) |
| 49 | +- [Asynchronous Programming in C#](https://learn.microsoft.com/en-us/dotnet/csharp/async) |
| 50 | +- [Secure Coding Guidelines for .NET](https://learn.microsoft.com/en-us/dotnet/standard/security/secure-coding-guidelines) |
| 51 | +- [Common Web Application Architectures in .NET](https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures) (for general C# architecture guidance) |
0 commit comments