Skip to content

Duplicate Code: Repeated region resolution logic in ResolutionSelector and element-wise coord utilities #127

@github-actions

Description

@github-actions

Analysis of commit 846ffc9

Assignee: @copilot

Summary

Two distinct structural duplication patterns were found in the source files:

  1. selectResolution and select2DResolution share nearly identical region-computation blocks in both their inner loop and fallback paths — 4 occurrences of the same ~14-line block.
  2. roundPixelCoord / floorPixelCoord / ceilPixelCoord are structurally identical element-wise mapping functions differing only in the Math.* operation applied.

Pattern 1 — Duplicated region-computation block in ResolutionSelector.ts

  • Severity: Medium
  • Occurrences: 4 (2 inside each function's loop, 2 in fallback paths)
  • Locations:
    • fidnii/src/ResolutionSelector.ts lines 47–59 (selectResolution loop body)
    • fidnii/src/ResolutionSelector.ts lines 74–88 (selectResolution fallback)
    • fidnii/src/ResolutionSelector.ts lines 282–296 (select2DResolution loop body)
    • fidnii/src/ResolutionSelector.ts lines 316–330 (select2DResolution fallback)

Duplicated block (~14 lines, repeated 4×):

const region = clipPlanesToPixelRegion(
  clipPlanes,
  volumeBounds,
  image,           // or lowestImage in fallback
  viewportBounds,
)
const alignedRegion = alignRegionToChunks(region, image)

const dimensions: [number, number, number] = [
  alignedRegion.end[0] - alignedRegion.start[0],
  alignedRegion.end[1] - alignedRegion.start[1],
  alignedRegion.end[2] - alignedRegion.start[2],
]

Root cause: select2DResolution is a near-copy of selectResolution with only the pixel-budget calculation differing (3D voxel count vs. in-plane × slab depth).

Refactoring Recommendation

Extract a shared helper that computes the aligned dimensions for a given resolution image:

function computeAlignedDimensions(
  image: MultiscaleImage,
  clipPlanes: ClipPlanes,
  volumeBounds: VolumeBounds,
  viewportBounds?: VolumeBounds,
): [number, number, number] {
  const region = clipPlanesToPixelRegion(clipPlanes, volumeBounds, image, viewportBounds)
  const aligned = alignRegionToChunks(region, image)
  return [
    aligned.end[0] - aligned.start[0],
    aligned.end[1] - aligned.start[1],
    aligned.end[2] - aligned.start[2],
  ]
}

Then accept a pixelCountFn: (dims: [number, number, number], image: MultiscaleImage) => number callback in a unified selectResolutionWithBudget helper, or simply call computeAlignedDimensions from both functions.


Pattern 2 — Structurally identical element-wise pixel-coordinate functions in coordinates.ts

  • Severity: Low
  • Occurrences: 3
  • Locations:
    • fidnii/src/utils/coordinates.ts lines 232–240 (roundPixelCoord)
    • fidnii/src/utils/coordinates.ts lines 248–256 (floorPixelCoord)
    • fidnii/src/utils/coordinates.ts lines 264–272 (ceilPixelCoord)

Duplicated structure (9 lines each):

export function (name)PixelCoord(
  pixelCoord: [number, number, number],
): [number, number, number] {
  return [
    Math.(op)(pixelCoord[0]),
    Math.(op)(pixelCoord[1]),
    Math.(op)(pixelCoord[2]),
  ]
}

Refactoring Recommendation

Introduce a private helper and re-implement:

function mapPixelCoord(
  fn: (v: number) => number,
  coord: [number, number, number],
): [number, number, number] {
  return [fn(coord[0]), fn(coord[1]), fn(coord[2])]
}

export const roundPixelCoord = (c: [number, number, number]) => mapPixelCoord(Math.round, c)
export const floorPixelCoord = (c: [number, number, number]) => mapPixelCoord(Math.floor, c)
export const ceilPixelCoord  = (c: [number, number, number]) => mapPixelCoord(Math.ceil,  c)

This keeps the public API identical while eliminating the structural repetition.


Impact Analysis

  • Maintainability: Any change to the region-computation logic in selectResolution must be mirrored manually in select2DResolution, and any change to the pixel-budget block must be replicated across both fallback paths.
  • Bug Risk: A bug fix applied to one copy will silently leave the other copies broken.
  • Code Bloat: ~56 lines of duplicated region-computation code; ~18 lines of duplicated element-wise mapping boilerplate.

Implementation Checklist

  • Extract computeAlignedDimensions helper in ResolutionSelector.ts
  • Refactor selectResolution and select2DResolution to use it
  • Add mapPixelCoord helper in coordinates.ts and simplify the three public wrappers
  • Run bunx tsc --noEmit to verify no type errors
  • Run bun run check to verify lint/format
  • Verify no test regressions with bun run test

Analysis Metadata

  • Analyzed Files: 14 source files in fidnii/src/ (excluding test files and workflow files)
  • Detection Method: Serena semantic code analysis + symbol body inspection
  • Commit: 846ffc9
  • Analysis Date: 2026-04-15

Generated by Duplicate Code Detector

To install this agentic workflow, run

gh aw add github/gh-aw/.github/workflows/duplicate-code-detector.md@33cd6c7f1fee588654ef19def2e6a4174be66197

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions