Skip to main content
We audited an Express Login demo to identify duplicated code patterns and suggest practical refactors. This lesson digs deeper into exact duplicates, near-duplicates, structural repetition, and repeated data/constants — and gives concrete code you can drop into the repo to reduce duplication and centralize behavior.
A presentation slide titled "Code Duplication Detection" with a large dark curved shape on the right displaying the word "Demo." The slide also shows a small "© Copyright KodeKloud" notice in the bottom-left.
The audit begins by prompting Claude Code for Beginners to scan the repository and produce a structured report. Example of the interactive prompt and initial output:
Claude enumerates files and reads content:
Below is a cleaned, consolidated Code Duplication Analysis Report with key findings, recommended remediation, and copy-paste-ready code examples to centralize shared logic and configuration.

Executive summary

This analysis targets duplication across the small Express.js app. Overall the codebase follows DRY principles, but several low- to medium-effort improvements will pay off as the project grows:
  • Centralize environment-derived configuration (high priority)
  • Consolidate error response formatting (medium)
  • Abstract database error mapping/handling (medium)
  • Extract constants for consistency (low)
Estimated refactor time: ~3–4 hours.

Scope & methodology

Files inspected:
  • server.js
  • routes/auth.js
  • config/database.js
  • package.json
  • schema.sql
  • Additional small files in the repo
Approach:
  • Identify exact and near-duplicates
  • Look for repeated data (env variables, constants)
  • Propose utility modules and constants to centralize logic

Findings at a glance

Detailed findings & remediation

1) Exact duplicates

Severity: 1/10 — none found. Great.

2) Near duplicates — repeated error responses

Location: routes/auth.js (multiple endpoints) Common duplicated pattern:
Remediation: centralize error responses in a small utility so all routes return the same structure and can optionally include details. Suggested implementation:
Effort: Low (≈30 minutes)

3) Database error handling pattern

Location: routes/auth.js (error handlers after DB calls) Pattern: repeated switch/case or if/else blocks mapping DB error codes to responses. Remediation: create a DB error map and a handler function that delegates to createErrorResponse. Example:
Effort: Medium (≈1 hour). Extensible: add additional DB vendor codes or map SQLState codes for more granularity.

4) Structural duplicates

Severity: 2/10 — router and connection setup patterns are repeated but consistent. Remediation: when the codebase grows, consider:
  • Router factory functions to reduce boilerplate across many routes.
  • Database connection factory to manage multiple connection pools.

5) Data duplication — environment variables and config values

Severity: 4/10 — process.env.* scattered in multiple files. Remediation: centralize env and derived configuration into config/index.js. This makes it easy to add validation (e.g., using envalid or joi) and to mock in tests. Suggested file:
Effort: Low–Medium (≈45 minutes) Add the following core files to centralize behavior and make refactors low-risk.
  1. utils/index.js — central error and validation handlers
  1. constants/index.js — central constants
  1. config/index.js — central configuration (already shown above)

Implementation priority

Summary & estimates

  • Overall Duplication Score: 2.5/10 — Very Low
  • Total Estimated Refactoring Effort: ~3–4 hours
  • Files to update: routes/auth.js, config/database.js, server.js
  • New files: utils/index.js, constants/index.js, config/index.js
These changes will make the codebase easier to maintain, reduce future duplication, and provide consistent behavior across routes and error handling.
Centralizing configuration and error handling reduces future duplication and makes updates (like changing response formats or adding DB error mappings) quick and low-risk.
Do not commit secrets to source control. Keep JWT_SECRET, DB passwords, and other sensitive values in environment variables, a secrets manager, or secure CI/CD variables.

After addressing duplication, focus on consistent naming conventions and readability — these help humans (and AI tools) reason about and maintain the code. Links and references

Watch Video