liveBook · Manning

Code duplication

As explained in Test Driven: Practical TDD and Acceptance TDD for Java Developers

Code duplication is the repetition of the same or similar code in multiple places in a software system.

Code duplication is repeated or similar code—or repeated responsibilities—in multiple places. It makes software harder to change: when logic is duplicated, a fix can be missed in one place and defects can follow. section 1.1.1 section 1.3.3

Look for duplication in tests as well as production code:

  • Tests may repeat setup or cover the same behavior. Share common setup and remove redundant tests while preserving the specific behavior each test needs to check. section 2.4.2 section 2.4
  • When tests repeat creating and parsing a template, move that work into a helper method so the tests focus on their assertions. section 3.2.1
Listing 2.13. Test code after removing a redundant test and unifying the fixture

See how redundant template-engine tests are removed and the remaining tests use a common template and setup. section 2.4.2

Refactor in small steps, and keep behavior unchanged. A useful check is whether a proposed cleanup removes repetition while leaving the tests focused on the functionality they verify. section 1.3.3 section 3.2.1

How Test Driven: Practical TDD and Acceptance TDD for Java Developers covers it

How deep it goes
Explains maintenance risks and demonstrates removing duplication from tests with shared setup and a helper method.
Examples
Uses repeated template-engine tests, shared fixtures, and a parser helper to illustrate removing redundancy.
What you can do after
Apply it by extracting common test setup while preserving test intent and behavior.
How it connects
Connects duplication to refactoring, maintainability, defects, and test code.

Also explained in

Compare how 5 books explain code duplication →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Test Driven: Practical TDD and Acceptance TDD for Java Developers. The section links point to the parts of the book it draws on.