Refactor: main() in client_cli.c is a 277-line god function #149

Closed
opened 2026-07-29 18:21:26 +02:00 by TapTap · 0 comments
Owner

Description

The main() function in src/client/client_cli.c (lines 65–341) is a 277-line god function that mixes at least four distinct responsibilities:

  1. Environment variable parsing (lines 66–73)
  2. Config creation (lines 76–82)
  3. Argument parsing — a 32-branch if-else chain (lines 87–249)
  4. Business-logic validation — 8 sequential checks for flag incompatibilities (lines 271–319)
  5. TLS initialization and dispatch (lines 321–335)
  6. Cleanup (lines 337–341)

The McCabe cyclomatic complexity of the argument-parsing loop alone exceeds 30.

Additionally:

  • Port validation for --server-port and -p is duplicate code — both parse with strtol then check range 1-65535.
  • The error-handling pattern (fprintf + exit_code = 1 + goto cleanup) is repeated 12+ times verbatim.

Location

src/client/client_cli.c:65-341

Suggested Fix

  1. Extract argument parsing into a dedicated function.
  2. Factor port validation into a shared helper: static int parse_port(const char* str).
  3. Extract validation into: static int validate_config(const Config* config).
  4. Replace the repetitive error-handling pattern with a helper macro.

Severity

Medium

Category

Quality / Maintainability

## Description The `main()` function in `src/client/client_cli.c` (lines 65–341) is a 277-line god function that mixes at least four distinct responsibilities: 1. **Environment variable parsing** (lines 66–73) 2. **Config creation** (lines 76–82) 3. **Argument parsing** — a 32-branch if-else chain (lines 87–249) 4. **Business-logic validation** — 8 sequential checks for flag incompatibilities (lines 271–319) 5. **TLS initialization and dispatch** (lines 321–335) 6. **Cleanup** (lines 337–341) The McCabe cyclomatic complexity of the argument-parsing loop alone exceeds 30. Additionally: - Port validation for `--server-port` and `-p` is duplicate code — both parse with strtol then check range 1-65535. - The error-handling pattern (fprintf + exit_code = 1 + goto cleanup) is repeated 12+ times verbatim. ## Location `src/client/client_cli.c:65-341` ## Suggested Fix 1. Extract argument parsing into a dedicated function. 2. Factor port validation into a shared helper: `static int parse_port(const char* str)`. 3. Extract validation into: `static int validate_config(const Config* config)`. 4. Replace the repetitive error-handling pattern with a helper macro. ## Severity Medium ## Category Quality / Maintainability
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: TapTap/FastSync#149