liveBook · Manning

Code duplication

As explained in High Performance Unity Game Development

The practice of implementing the same or similar code in multiple places instead of sharing one implementation.

Duplicating code can make different features easier to understand and debug. For example, keeping local and server save/load paths separate makes it clearer where a source-specific bug lives and helps prevent loading data from the wrong place. section 7.1.8 section 7.3

Choose whether to duplicate or share code based on what the code does:

  • Keep implementations separate when sharing them could make a change to one feature break another. section 7.1.8
  • Share code when it performs one action: take data in, transform it, and output data. Split it into smaller functions if it grows to do more. section 7.3
Figure 7.6 Saving and loading code should be separated by source, but any common code can be moved to a shared static function.

Shows separate save/load paths by source, with common code suitable for a shared static function. section 7.1.8

How High Performance Unity Game Development covers it

How deep it goes
Explains risks of shared code and limits sharing to single-action transformations; it does not prescribe blanket deduplication.
Examples
Uses local and server save/load paths, including bugs caused by loading from an unexpected source.
What you can do after
Evaluate whether to duplicate source-specific behavior or share a single-action data transformation.
How it connects
Connects code duplication with data-oriented design, static functions, and local-versus-server data loading.

Also explained in

Compare how 6 books explain code duplication →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.