Files
nt8-sdk/CODE_STYLE_GUIDE.md
Billy Valentine 92f3732b3d
Some checks failed
Build and Test / build (push) Has been cancelled
Phase 0 completion: NT8 SDK core framework with risk management and position sizing
2025-09-09 17:06:37 -04:00

6.0 KiB

NT8 SDK Code Style Guide

Required Patterns for AI Agents

Class Declaration Pattern

using System;
using System.Collections.Generic;

namespace NT8.Core.SomeModule
{
    /// <summary>
    /// Class description
    /// </summary>
    public class ClassName
    {
        private readonly Type _field;
        
        /// <summary>
        /// Property description
        /// </summary>
        public Type PropertyName { get; set; }
        
        /// <summary>
        /// Constructor description
        /// </summary>
        public ClassName(Type parameter)
        {
            if (parameter == null) throw new ArgumentNullException("parameter");
            _field = parameter;
        }
        
        /// <summary>
        /// Method description
        /// </summary>
        public ReturnType MethodName(Type parameter)
        {
            // Implementation
        }
    }
}

Interface Implementation Pattern

/// <summary>
/// Interface description
/// </summary>
public interface IInterfaceName
{
    /// <summary>
    /// Method description
    /// </summary>
    ReturnType MethodName(Type parameter);
}

/// <summary>
/// Implementation description
/// </summary>
public class Implementation : IInterfaceName
{
    public ReturnType MethodName(Type parameter)
    {
        // Implementation
    }
}

Dictionary Initialization Pattern (C# 5.0)

// ✅ REQUIRED Pattern
var dictionary = new Dictionary<string, object>();
dictionary.Add("key1", value1);
dictionary.Add("key2", value2);

// ❌ FORBIDDEN Pattern
var dictionary = new Dictionary<string, object> 
{
    ["key1"] = value1,
    ["key2"] = value2
};

String Formatting Pattern (C# 5.0)

// ✅ REQUIRED Pattern
var message = String.Format("Processing {0} with value {1:F2}", name, amount);
_logger.LogInformation("Order {0} status: {1}", orderId, status);

// ❌ FORBIDDEN Pattern
var message = $"Processing {name} with value {amount:F2}";
_logger.LogInformation($"Order {orderId} status: {status}");

Async Method Pattern (C# 5.0)

/// <summary>
/// Async method description
/// </summary>
public async Task<ReturnType> MethodNameAsync(Type parameter)
{
    // Async implementation
    var result = await SomeAsyncOperation();
    return result;
}

Error Handling Pattern

public ReturnType MethodName(Type parameter)
{
    if (parameter == null) throw new ArgumentNullException("parameter");
    
    try
    {
        // Implementation
        return result;
    }
    catch (SpecificException ex)
    {
        _logger.LogError("Specific error occurred: {0}", ex.Message);
        throw; // or handle appropriately
    }
    catch (Exception ex)
    {
        _logger.LogError("Unexpected error: {0}", ex.Message);
        throw;
    }
}

Test Class Pattern (MSTest)

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NT8.Core.SomeModule;

namespace NT8.Core.Tests.SomeModule
{
    [TestClass]
    public class ClassNameTests
    {
        private ClassName _target;
        
        [TestInitialize]
        public void TestInitialize()
        {
            _target = new ClassName(/* parameters */);
        }
        
        [TestMethod]
        public void MethodName_Condition_ExpectedResult()
        {
            // Arrange
            var input = /* test data */;
            
            // Act
            var result = _target.MethodName(input);
            
            // Assert
            Assert.IsTrue(result.Success);
            Assert.AreEqual(expected, result.Value);
        }
        
        [TestMethod]
        public void MethodName_InvalidInput_ThrowsException()
        {
            // Act & Assert
            Assert.ThrowsException<ArgumentNullException>(() => _target.MethodName(null));
        }
    }
}

Configuration Class Pattern

/// <summary>
/// Configuration class description
/// </summary>
public class ConfigurationClass
{
    /// <summary>
    /// Property description
    /// </summary>
    public Type PropertyName { get; set; }
    
    /// <summary>
    /// Constructor with all required parameters
    /// </summary>
    public ConfigurationClass(Type parameter1, Type parameter2)
    {
        PropertyName1 = parameter1;
        PropertyName2 = parameter2;
    }
}

Enum Pattern

/// <summary>
/// Enum description
/// </summary>
public enum EnumName
{
    /// <summary>
    /// First value description
    /// </summary>
    FirstValue,
    
    /// <summary>
    /// Second value description
    /// </summary>
    SecondValue
}

Naming Conventions

Classes and Interfaces

  • Classes: PascalCase (RiskManager, PositionSizer)
  • Interfaces: IPascalCase (IRiskManager, IPositionSizer)

Methods and Properties

  • Methods: PascalCase (ValidateOrder, CalculateSize)
  • Properties: PascalCase (Symbol, Quantity)

Fields and Variables

  • Private fields: _camelCase (_logger, _riskConfig)
  • Local variables: camelCase (riskAmount, contracts)
  • Constants: UPPER_CASE (MAX_CONTRACTS, DEFAULT_TIMEOUT)

Parameters

  • Parameters: camelCase (intent, context, config)

File Organization

Using Statements Order

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NT8.Core.Common.Models;
using NT8.Core.Logging;

Namespace and Class Structure

namespace NT8.Core.ModuleName
{
    /// <summary>
    /// Class documentation
    /// </summary>
    public class ClassName
    {
        // 1. Private fields
        private readonly Type _field;
        
        // 2. Public properties
        public Type Property { get; set; }
        
        // 3. Constructor(s)
        public ClassName() { }
        
        // 4. Public methods
        public void PublicMethod() { }
        
        // 5. Private methods
        private void PrivateMethod() { }
    }
}

These patterns MUST be followed by all AI agents to ensure consistency and compatibility.