Skip to main content
We examined a small Express login demo to identify duplication and suggest pragmatic, low-effort refactors. This walkthrough highlights where duplication exists, provides concrete code to centralize behavior, and estimates implementation effort so you can prioritize improvements quickly.
A presentation slide titled "Code Duplication Detection" with a dark teal curved panel on the right that says "Demo." The bottom-left corner shows a small "© Copyright KodeKloud" notice.
Below is the audit prompt used to drive the duplication analysis. It instructed the auditor to search for exact, near, structural, and data duplication and to produce a structured findings report.

Executive Summary

  • Overall Duplication Score: 2.5 / 10 (Very low)
  • Total Estimated Refactoring Effort: ~3–4 hours
  • Primary files to change:
    • routes/auth.js
    • config/database.js
    • server.js
  • Suggested new modules:
    • utils/index.js
    • constants/index.js
    • config/index.js
This application is already reasonably DRY. The recommendations are small, focused refactors that improve consistency and help the codebase scale with minimal effort.

Findings (detailed)

1. EXACT DUPLICATES

Severity: 0/10 — None found No exact copy-pasted blocks were identified.

2. NEAR DUPLICATES

Severity: 3/10 — Minor issues 2.1 Similar Error Response Patterns
  • Location: routes/auth.js (multiple handlers)
  • Duplication: ~60% similar JSON error responses repeated.
Common pattern:
Remediation: centralize error responses into a utility function and reuse it across routes. Suggested implementation (placed under utils):
Usage:
Effort: Low (≈30 minutes) 2.2 Database Error Handling Pattern
  • Location: routes/* (switch-case handling of DB errors)
  • Duplication: similar switch/case or if/else blocks mapping DB error codes to responses.
Example repeated pattern:
Remediation: map error codes to response metadata in a utility. Suggested implementation:
Effort: Medium (≈1 hour)

3. STRUCTURAL DUPLICATES

Severity: 2/10 — Patterns are good
  • Express router usage and database event handling are consistent.
  • If the app scales (multiple routers or DBs), extract a router factory or a DB connection factory to avoid boilerplate.
Suggested future extraction: connection factory that takes connection options and registers lifecycle event handlers in one place.

4. DATA DUPLICATION

Severity: 4/10 — Moderate 4.1 Environment Variable References — scattered across multiple files
Examples:
Remediation: centralize configuration in config/index.js and import where needed. Suggested implementation:
Effort: Low (≈1 hour) 4.2 Magic Numbers / Strings (Constants)
  • Repeated HTTP statuses and literal strings make refactors error-prone.
Remediation: create constants/index.js to centralize HTTP status codes and common messages. Suggested implementation:
Effort: Low (≈45 minutes)
Create a reusable utilities module to centralize error handling, DB error mapping, and validation handling. Place this at utils/index.js and import in routes as needed.
Place constants in constants/index.js and config in config/index.js (examples shown earlier). Then update route handlers to:
  • Use createErrorResponse instead of manually building response objects.
  • Use handleDatabaseError where DB errors are handled.
  • Use centralized config values instead of process.env scattered throughout the code.

Implementation Priority & Estimated Effort


Files to Create / Modify

  • Add utils/index.js (utilities shown above)
  • Add config/index.js (centralized configuration)
  • Add constants/index.js (HTTP statuses and shared strings)
  • Update routes/auth.js to use utilities
  • Update config/database.js to import values from config/index.js
  • Update server.js to import config.server.port instead of process.env.PORT directly

Do not commit secrets (like JWT secret or DB passwords) to source control. Use environment variables or a secrets manager and ensure .env is excluded from version control.


Final Remarks

The codebase already follows good DRY and SOLID practices. The recommended changes are incremental, low-risk refactors that yield consistent error responses, centralized configuration, and fewer duplicated patterns—helping maintenance and future scaling.
Generated by Claude Code Duplication Analyzer v1.0

Watch Video