Naming Conventions & Code Structure

Naming Standards

Private fields

Use _camelCase with a leading underscore (e.g., _userName) 

Constants

Use PascalCase without underscores (e.g., MaxRetryCount) 

Public properties

Use PascalCase (e.g., UserName) 

Local variables & parameters

Use camelCase (e.g., userId) 

Interfaces

Prefix with I using PascalCase (e.g., IUserRepository) 

Async methods

Suffix with Async (e.g., GetUserAsync) 

 

Order of content within the class

  1. Private readonly fields (top of class)
  2. Constants
  3. Constructors
  4. Public properties
  5. Public methods
  6. Protected methods
  7. Private methods
  8. Nested classes/structs (if any) 

 

Code Formatting 

Separate all methods with one blank line 

Split constructor parameters one per line for improved readability 

Use Ctrl + K + D to format code automaticallyMaintain consistent indentation (typically 4 spaces) 

Remove unused usings via Ctrl + R + G 

 

Examples: 

Classes/methods: 

 

namespace EGFlow.Parser.Services 

{ 

    public class Blah 

    { 

        public Blah( 

            string thing, 

            int anotherThing 

        ) 

        { 

  

        } 

  

        public string DoSomething( 

            string something, 

            int anotherSomething 

        ) 

        { 

            return "Hello"; 

        } 

    } 

} 

 

 

 String Handling Best Practices

  1. Use string.Empty instead of "" 
  1. Use string.IsNullOrEmpty() for null or empty checks 
  1. Use string.IsNullOrWhiteSpace() for null, empty, or whitespace-only checks 
  1. Use string interpolation ($"{variable}") instead of concatenation for readability 
  1. Consider StringBuilder for extensive string manipulation in loops 

 

  1. LINQ Best Practices 
  1. Use .Count > 0 when you need the actual count 
  1. Use .Any() for existence checks (more performant) 
  1. Prefer Method syntax over Query syntax for consistency and readability 
  1. Avoid multiple enumeration; materialize collections with .ToList() when needed 
  1. Use .FirstOrDefault() or .SingleOrDefault() appropriately based on expected results 

 

  1. Async/Await Best Practices 
  1. Always use async/await for I/O-bound operations (database, file, network) 
  1. Never use .Wait() or .Result to prevent deadlocks 
  1. Use ConfigureAwait(false) in library code to avoid context capture 
  1. Return Task or Task<T> for async methods (not async void except event handlers) 

 

  1. Enum Best Practices 
  1. Use Enums in models instead of byte, int, or long for type safety 
  1. Avoid nullable enums in LINQ queries unless explicitly required 
  1. Define enums with explicit values 

 

  1. Method & Parameter Best Practices 
  1. Minimize method parameters (ideally ≤3-4 parameters) 
  1. Follow the Single Responsibility Principle (SRP) 
  1. Split large functions into smaller, focused methods 
  1. Use meaningful method names that describe intent (Follow ActionVerb pattern in the name 
  1. Use optional parameters or method overloading judiciously 
  1. Optional/default parameters should be stated last in the sequence of parameters.  - AVOID optional/default parameters if possible.  

 

  1. Concurrency & Threading Best Practices 
  1. Use locking mechanisms (lock, SemaphoreSlim) to prevent race conditions 
  1. Prefer async/await over explicit thread management 
  1. Use thread-safe collections (ConcurrentDictionary, ConcurrentQueue) when appropriate 
  1. Avoid shared mutable state where possible 
  1. Consider using Interlocked class for simple atomic operations 

 

  1. Dependency Injection Best Practices 
  1. Avoid creating objects via new inside methods; use DI container 
  1. Inject dependencies through constructors (constructor injection) 
  1. Register services with appropriate lifetimes 

Transient: New instance per request 

Scoped: Single instance per HTTP request/scope 

Singleton: Single instance for application lifetime 

Depend on interfaces/abstractions, not concrete implementations 

Avoid service locator pattern; use explicit injection 

 

  1. Exception Handling Best Practices 
  1. Use specific exception types rather than generic Exception 
  1. Avoid catching exceptions you cannot handle meaningfully 
  1. Log exceptions with sufficient context for debugging 
  1. Use finally blocks or using statements for resource cleanup 
  1. Don't swallow exceptions silently without logging 
  1. Consider custom exception types for domain-specific errors 

 

  1. Performance & Memory Management 
  1. Dispose of resources properly using statements or IDisposable 
  1. Profile before optimizing; measure actual performance bottlenecks 
  1. Consider lazy initialization (Lazy<T>) for expensive object creation 

 

  1. Code Quality & Maintainability 
  1. Write XML documentation comments for public APIs 
  1. Follow DRY principle (Don't Repeat Yourself) 
  1. Use nullable reference types (C# 8.0+) to reduce null reference exceptions 
  1. Implement proper logging at appropriate levels (Debug, Info, Warning, Error) 
  1. Write unit tests with good coverage for critical business logic 
  1. Test driven development! Write tests as you go. 

 

  1. Security Best Practices 
  1. Validate all inputs at API boundaries 
  1. Avoid exposing sensitive information in logs or error messages 
  1. Implement proper authentication and authorization 
  1. Use secure communication protocols (HTTPS, TLS) 
  1. Keep dependencies updated to patch security vulnerabilities 

 

  1. API Design Best Practices 
  1. Follow RESTful conventions for HTTP APIs 
  1. Use appropriate HTTP status codes (200, 201, 400, 404, 500, etc.) 
  1. Version your APIs for backward compatibility 
  1. Implement proper error response models 
  1. Use DTOs (Data Transfer Objects) to separate API contracts from domain models 
  1. Document APIs using Swagger/OpenAPI