Code duplication is the presence of multiple copies of substantially similar implementation logic. Repeated code enlarges programs and makes maintenance risky: a change must be applied consistently to every copy, or a copy may be missed or changed differently. section 2.3.3
Common design symptoms include:
- Parallel subclasses repeat constructors, add() functions, find() functions, and matching logic for each book kind. section 2.3.3
- Related classes repeat a fixed sequence of operations, such as report generation steps, even when only some steps differ. section 8.1.1 section 8.1.2
- Separate clients repeat the same calls into several subsystem classes, making additions require changes in multiple places. section 10.2.2
- Different collection representations force repeated traversal code and tie reporting logic to storage details. section 11.1.2
Book catalogue change leak
A catalogue with Fiction and Cookbook subclasses has overloaded add() and find() functions containing similar logic. Each find() implementation performs runtime type checks and conversions. Adding How-to books increases subclasses, duplicate code, and dynamic_cast operations, exposing a design that does not scale well. section 2.3.3
Repeated report workflow
BaseballReport and VolleyballReport both implement generate_report() as print_header(), acquire_data(), analyze_data(), print_report(), and print_footer(). Their headers and footers are also duplicated, although data acquisition and analysis differ. section 8.1.2
Eliminate duplication by keeping one copy in a separate function or cohesive class. The DRY principle treats repetition as a design warning rather than merely a size problem; patterns such as Template Method, Strategy, Façade, and Iterator address duplication in workflows, algorithms, subsystem calls, and collection traversal. section 2.3.3 section 8.1.1
Listing 8.1 (Program 8.1 Reports): BaseballReport.h (before the design pattern)
#include <string>
#include <vector>
#include "BaseballData.h"
using namespace std;
class BaseballReport
{
public:
BaseballReport() : title("BASEBALL GAME REPORT")
{
singles = doubles = triples = homers = 0;
}
void generate_report()
{
print_header(); #1
acquire_data(); #1
analyze_data(); #1
print_report(); #1
print_footer(); #1
}
private:
string title;
vector<int> hits;
int singles, doubles, triples, homers;
void print_bar(const char ch, const int count) const;
void print_header() const;
void acquire_data();
void analyze_data();
void print_report() const;
void print_footer() const;
};
#1 Invoke the steps to generate a report.
Inspect the duplicated report workflow directly in BaseballReport.h, then compare its fixed sequence with the parallel VolleyballReport implementation. section 8.1.2


