liveBook · Manning

Trivial code

As explained in Unit Testing Principles, Practices, and Patterns

Trivial code is code with little complexity or domain significance and few or no collaborators.

Trivial code has low complexity, little domain significance, and few or no collaborators. Parameter-less constructors and one-line properties are typical examples. section 7.1

Classify code by complexity or domain significance and collaborator count:

  • Treat domain models and algorithms as the strongest candidates for unit tests because they combine high value with low maintenance cost. section 7.1
  • Skip tests for trivial code because their value is close to zero. section 7.1 section 7.6
  • Test controllers briefly through integration tests, and split overcomplicated code into controllers and complex logic. section 7.6

Typical trivial code

A C# parameter-less constructor or one-line property usually belongs in the trivial-code category when it has few or no collaborators and little domain significance. section 7.1

Figure 7.1. The four types of code, categorized by code complexity and domain significance (the vertical axis) and the number of collaborators (the horizontal axis).

Use the four-quadrant classification to compare complexity or domain significance against collaborator count. section 7.1

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines trivial code through complexity, domain significance, and collaborators, then stops at testing prioritization rather than implementation internals.
Examples
Uses C# parameter-less constructors and one-line properties as examples of trivial code.
What it assumes
Assumes familiarity with unit and integration testing, while defining the classification dimensions used to prioritize them.
What you can do after
Evaluate code for testing return by weighing complexity, domain significance, and collaborator count.
How it connects
Connects trivial code to domain models, algorithms, controllers, overcomplicated code, unit testing, and integration testing.

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.