Tinfour.NET Coding Standards and Conventions
November 27, 2025 · View on GitHub
Purpose: Guidelines for maintaining consistency in the Tinfour.NET codebase
Audience: Contributors and developers
General Principles
- Maintain Mathematical Correctness: The mathematical logic and algorithms must remain identical to the Java implementation.
- Optimize for Performance: Memory efficiency and processing speed are critical priorities.
- Write Clean, Maintainable Code: Prefer clear, maintainable code over clever optimizations.
- Cross-Platform Compatibility: Code must work on Windows, macOS, Linux, and WebAssembly.
Language Usage
C# Features
- Use C# 8.0+ features where they improve code quality or performance.
- Always use
varwhen the type is obvious from the right side of the assignment. - Use nullable reference types with proper annotations (
?,!). - Use expression-bodied members for simple methods and properties.
- Use pattern matching where it enhances readability.
- Use tuple returns for methods that need to return multiple values.
Types and Memory Management
- Consider structs vs classes carefully:
- Use structs for small (≤16 bytes), immutable value types
- Use classes for larger objects and when inheritance is needed
- Use
readonly structfor immutable value types. - Use
Span<T>andMemory<T>for zero-allocation array operations. - Consider object pooling for frequently allocated/deallocated objects.
- Be mindful of boxing/unboxing operations with value types.
Asynchronous Programming
- Async all the way down when appropriate.
- Use Task Parallel Library (TPL) for computationally intensive operations.
- Consider
ValueTaskfor high-performance async operations. - Provide synchronous alternatives for performance-critical paths.
Naming and Formatting
- Match .NET naming conventions:
- PascalCase for types, methods, properties, and public fields
- camelCase for local variables and parameters
- _camelCase for private fields
- Use descriptive names; avoid abbreviations except for common ones (e.g., Id, Xml, Http).
- One statement per line; no compound statements.
- Preserve original comments and add new ones explaining porting decisions.
- Use XML documentation comments for public APIs.
Type Mappings (Java to C#)
| Java Type | C# Equivalent | Notes |
|---|---|---|
| int | int | |
| long | long | |
| double | double | |
| float | float | |
| boolean | bool | |
| String | string | |
| Object | object | |
| ArrayList<T> | List<T> | |
| HashMap<K,V> | Dictionary<K,V> | Consider performance characteristics |
| Set<T> | HashSet<T> | |
| Iterator<T> | IEnumerator<T> | Consider using foreach instead |
| Iterable<T> | IEnumerable<T> |
Collection Usage
- Prefer
IEnumerable<T>for method return types when the caller only needs to enumerate. - Use concrete collection types (List<T>, Dictionary<K,V>) for properties and fields.
- Consider immutable collections for thread safety and to prevent modification.
- Use collection initializers where appropriate.
Error Handling
- Use exceptions for exceptional conditions, not control flow.
- Prefer specific exception types over general ones.
- Consider returning
Result<T>or tuple for operations that may fail in expected ways. - Use proper null handling with nullable reference types.
Testing
- Write unit tests for all functionality using xUnit.
- Match Java test data and test cases where possible.
- Ensure tests validate mathematical correctness against known values or Java results.
- Include performance benchmarks for critical operations.
Project Organization
- Maintain namespace hierarchy matching Java package structure.
- Organize files by feature, not by type.
- Keep interfaces and implementations in separate files.
- Group related functionality in the same namespace.
Documentation
- Preserve algorithm descriptions from Java comments.
- Document performance characteristics and constraints.
- Include XML documentation for public APIs.
- Document any deviations from the Java implementation.
Version Control
- Commit logical units of work.
- Include descriptive commit messages explaining the changes.
- Reference the original Java code in comments when necessary.
Performance Considerations
- Profile before optimizing to identify actual bottlenecks.
- Consider SIMD operations for vector math.
- Minimize memory allocations in tight loops.
- Use value types appropriately to reduce heap allocations.
- Consider unsafe code blocks only when absolutely necessary for performance.
Last Updated: November 2025