Skip to content

leiden/partition.ts: makePartition exceeds cyclomatic threshold (cyc=10) #1871

Description

@carlos-alm

What

`codegraph complexity --file src/graph/algorithms/leiden/partition.ts --health -T --json` flags `makePartition` (the factory function that wires `PartitionState` to the public `Partition` interface) as exceeding the cyclomatic complexity threshold:

```
cyclomatic: 10 (threshold: warn >= 10, see DEFAULTS.complexity in src/infrastructure/config.ts)
cognitive: 13
maintainabilityIndex: 32.5
exceeds: ["cyclomatic"]
```

Found incidentally while working on #1755 (a sibling complexity fix for `computeDeltaModularityDirected` in the same file) — out of scope for that issue, filing separately per repo scope-discipline convention.

Likely source of complexity

`makePartition` builds an object literal with many getter methods, several of which have their own bounds-check ternaries inline (e.g. `getCommunityTotalSize`, `getCommunityNodeCount`, `getNeighborEdgeWeightToCommunity` and friends with `|| 0` fallbacks). Each inline ternary/`||` in the returned object literal adds to the enclosing function's cyclomatic count even though they're trivial one-liners.

Suggested approach

Decompose by extracting the getter closures (or groups of them) into small named functions that close over `s: PartitionState`, similar to how `initAggregates`, `accumulateNeighborWeights`, `moveNode`, and `compactIds` are already extracted and merely referenced in the returned object literal. This should pull the branchy one-liners out of `makePartition` itself.

Note: while decomposing, keep in mind #1755's finding that the `|| 0` fallback after `fget`/bounds-check reads in this file is currently a no-op given Float64Array invariants (dense, zero-initialized, NaN-scrubbed at adapter.ts ingestion) — a shared `fgetOrZero` helper was added to `typed-array-helpers.ts` in that fix and could be reused here instead of hand-rolling the same ternary+`||` pattern again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions