|
| 1 | +# C# Coding Standards for AI Coding Assistant 'copilot' |
| 2 | + |
| 3 | +## General Principles |
| 4 | +- **MANDATORY**: Adhere to the latest C# language version to ensure code uses the most current features and improvements. |
| 5 | +- **ALWAYS** write code that is clear, understandable, and maintainable. |
| 6 | +- **NEVER** use obsolete or deprecated C# features unless absolutely necessary for maintaining legacy systems. |
| 7 | + |
| 8 | +## Formatting and Style |
| 9 | +- **MUST** follow Microsoft's C# coding conventions regarding naming, layout, and commenting. |
| 10 | + - Use PascalCase for class names and method names. |
| 11 | + - Use camelCase for local variables and method arguments. |
| 12 | + - **REQUIRED**: Place opening braces on a new line for classes, methods, and control statements. |
| 13 | +- **ALWAYS** indent code blocks with four spaces, not tabs. |
| 14 | +- **ALWAYS** use explicit typing over implicit var declarations, except in cases of obvious typing (e.g., instantiation). |
| 15 | + |
| 16 | +## Error Handling |
| 17 | +- **MANDATORY**: Handle all possible exceptions using try, catch, finally blocks where applicable. |
| 18 | +- **NEVER** allow exceptions to go unhandled unless explicitly justified by the application logic. |
| 19 | +- Exceptions **SHOULD** be logged with a detailed error message and, if applicable, stack trace. |
| 20 | + |
| 21 | +## Security Practices |
| 22 | +- **MUST** validate all inputs to prevent injection attacks and ensure data integrity. |
| 23 | +- **REQUIRED**: Utilize secure methods and libraries for handling sensitive data such as passwords, API keys, and personal user information. |
| 24 | +- **NEVER** log sensitive information. |
| 25 | + |
| 26 | +## Performance |
| 27 | +- **USE** async and await for asynchronous programming to improve application responsiveness and scalability. |
| 28 | +- **RECOMMENDED**: Optimize LINQ queries by minimizing data fetching and processing operations. |
| 29 | +- **SHOULD** prefer StringBuilder over string concatenation in loops or during extensive string manipulations. |
| 30 | + |
| 31 | +## Testing |
| 32 | +- **MANDATORY**: Write unit tests for all business logic to ensure code quality and catch regressions early. |
| 33 | +- Unit tests **SHOULD** cover both positive and negative scenarios. |
| 34 | +- **ALWAYS** use a consistent test naming convention that clearly describes the test purpose. |
| 35 | + |
| 36 | +## Code Reviews and Collaboration |
| 37 | +- **MUST** use pull requests for merging code into shared branches and repositories. |
| 38 | +- **ALWAYS** perform thorough code reviews to catch issues early and share knowledge among team members. |
| 39 | +- **REQUIRED**: Follow a defined Git workflow suitable for your team structure (e.g., Git flow, GitHub flow). |
| 40 | + |
| 41 | +## Documentation |
| 42 | +- **REQUIRED**: Document all public classes, methods, properties, and enums. |
| 43 | +- **ALWAYS** update documentation to reflect changes in the logic or implementation. |
| 44 | +- **SHOULD** include inline comments to clarify complex code blocks. |
| 45 | + |
| 46 | +## Dependency Management |
| 47 | +- **MANDATORY**: Keep external dependencies to a minimum to reduce potential security risks and lower the maintenance burden. |
| 48 | +- **ALWAYS** audit dependencies for security vulnerabilities on a regular schedule. |
| 49 | +- When new libraries are added, **SHOULD** assess their stability and support level. |
| 50 | + |
| 51 | +## Continuous Integration/Continuous Deployment |
| 52 | +- **MUST** have CI/CD pipelines in place to automate testing and deployment processes. |
| 53 | +- Deployment scripts **SHOULD** be tested regularly to prevent deployment failures. |
| 54 | + |
| 55 | +## Legacy Code and Migration |
| 56 | +- **ALWAYS** aim to incrementally refactor legacy code as part of ongoing development. |
| 57 | +- When dealing with legacy systems, **SHOULD** plan a clear migration path towards newer technologies or frameworks. |
| 58 | + |
| 59 | +These rules are designed to ensure that the AI coding assistant 'copilot' and its users adhere to the best practices and modern standards prevalent in C# development as of 2025. |
0 commit comments