This document outlines the coding conventions for the GenHub C# project. The goal is to ensure consistency, readability, and maintainability across the codebase. These conventions are based on the Microsoft C# Coding Conventions, with selected practices from the CoreFX C# and Google C# style guides, and additional project-specific preferences.
- Consistency: Follow these conventions throughout the codebase.
- Readability: Prioritize code clarity and maintainability.
- Common Sense: Use good judgment; code reviews will enforce readability and style.
- Indentation: Use 4 spaces per indentation level (no tabs).
- Line Length: No enforced maximum; use common sense for readability.
- Braces: Use Allman braces style (opening brace on their own line with same indentation as the declaration). Single line statements (e.g. if, for, while) may omit braces.
- Blank Lines: Use single blank lines to separate logical sections of code.
- Whitespace:
- Use spaces around binary operators (e.g.,
a + b). - No spaces after method names before parentheses (e.g.,
MethodName()). - No spaces before commas, semicolons, or colons.
- Use spaces around binary operators (e.g.,
- Comments:
- Use
//for single-line comments. - Avoid the use of
/**/block comments. - Place comments on their own line above the code they describe, not at the end of a line.
- Use
- Namespaces: PascalCase (e.g.,
GenHub.Core). - Classes, Structs, Enums, Delegates: PascalCase.
- Methods, Properties, Events: PascalCase.
- Interfaces: Prefix with
I, PascalCase (e.g.,IService). - Fields:
privateandinternal:_camelCase(prefix with underscore).publicandprotected: PascalCase.- Local Variables and Parameters: camelCase.
- Constants: PascalCase.
- Class Member Order (strict, as per Google/CoreFX/StyleCop):
- Nested types
- Static fields
- Instance fields
- Constructors
- Finalizers
- Properties
- Indexers
- Events
- Methods
- Static methods go first, then instance methods.
- Methods should be ordered by visibility:
public,protected,internal,private.
- Using Directives:
- Alphabetical order (no special treatment for
Systemnamespaces). - Place outside the namespace declaration.
- LINQ: Prefer the fluent (method chain) syntax over query expressions.
- Primary Constructors: Use primary constructors and access parameters directly when possible.
- Nullable Reference Types: Enable and use explicit nullable annotations.
- Comments: Use XML documentation comments for all public and protected classes, interfaces, properties and methods.
- File Structure: One top-level type per file.
- Error Handling: Use exceptions appropriately; avoid empty catch blocks.
- Use Visual Studio's default formatting (Edit > Advanced > Format Document).
- StyleCop and similar analyzers are used to enforce ordering and style.
- Enable nullable reference types in project settings.
Adhering to these conventions will help keep the GenHub codebase clean, consistent, and easy to maintain.