liveBook · Manning

Code duplication

As explained in Object-Oriented Software Design in C++

Code duplication is the presence of multiple copies of substantially similar implementation logic in a program.

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

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains causes and consequences through several C++ examples, then points to shared functions, cohesive classes, and design patterns.
Examples
Illustrates duplication with book catalogues, sports reports, fundraising clients, and team reports over arrays, vectors, and maps.
What you can do after
Evaluate/understand repeated logic and consolidate it into shared functions, cohesive classes, or appropriate design-pattern structures.
How it connects
Connects duplication with the DRY principle, inheritance, runtime type checking, Template Method, Strategy, Façade, and Iterator patterns.

Also explained in

Compare how 3 books explain code duplication →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.