Skip to content

[MAINTENANCE] Refactor mainTrafficLayer.cpp for improved code quality and maintainability #113

@yunlishao

Description

@yunlishao

Task Summary
Comprehensive refactoring of mainTrafficLayer.cpp to improve code quality, reduce technical debt, and enhance maintainability. This includes cleaning up printout/banner messages, removing dead/commented code, consolidating duplicate logic, improving code structure, and implementing memory safety improvements.

Why is this needed?
The current mainTrafficLayer.cpp has grown to ~950 lines with accumulated technical debt:

  • Inconsistent and scattered console output during startup
  • Dead code and TODO comments that should be tracked issues
  • Duplicated logic patterns that increase maintenance burden
  • Large monolithic main() function that's difficult to navigate
  • Unsafe memory patterns (volatile vs atomic, raw file handles)

Refactoring will improve code readability, reduce bugs, and make future development more efficient.

Scope of Work
Files involved:

  • TrafficLayer/TrafficLayer/mainTrafficLayer.cpp

Tasks:

1. Printout/Banner Cleanup

  • Remove extra \n\n formatting inconsistencies (completed in initial cleanup)
  • Remove "No verbose logging" negative info print (completed)
  • Consolidate scattered port prints into single summary (completed)
  • Add build date/time to banner (__DATE__, __TIME__)
  • Add configuration summary block after config parsing

2. Remove Dead/Commented Code

  • Remove old TODO comments (lines 132-141) - convert to tracked issues
  • Remove unnecessary file existence check (lines 359-366)
  • Remove empty debug blocks (int aa = 1; at lines 924-925, 1145)
  • Remove empty verbose block (lines 481-486)
  • Remove large commented-out code blocks (lines 623-641)

3. Consolidate Duplicate Logic

  • Extract subscription handling to helper function (lines 488-523 duplicate pattern)
  • Create extractPortsFromSubscription() function (lines 537-584 repeated pattern)
  • Consolidate identical sendData branches (lines 1002-1036)

4. Improve Code Structure

  • Extract functions from main():
    • parseCommandLineArgs()
    • initializeLogging()
    • setupConfiguration()
    • setupConnections()
    • runSimulationLoop()
  • Move Timer_t struct closer to usage or remove if ENABLE_TIMING always false
  • Move global variables Config_c, Sock_c into main or pass as parameters

5. Memory/Resource Safety Improvements

  • Replace volatile bool with std::atomic<bool> for g_shutdown.shutdownRequested (line 17)
  • Use RAII for file handles - replace fopen/fclose pairs with std::ofstream

6. Configuration Cleanup

  • Remove ENABLE_TIMING code if hardcoded to false, or make configurable via config.yaml

Environment (if version-specific)

  • C++ compiler/version: Visual Studio 2022

Risks or Dependencies

  • Changes to main executable require thorough testing across all simulator combinations (VISSIM/SUMO, with/without CarMaker, with/without Carla)
  • Should be done incrementally with testing between each major change
  • No external dependencies

Additional Context (optional)
This issue was created after initial cleanup of startup printout formatting. The initial cleanup normalized console output formatting and consolidated scattered port information into a single summary line.

Related line references are based on the current state after initial cleanup and may shift as changes are made.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions