liveBook · Manning

Implicit output

As explained in Unit Testing Principles, Practices, and Patterns

A hidden output is an effect of a computation, such as a side effect or exception, that is not represented by its return value.

An implicit output is an effect of a computation that its return value does not represent. Side effects and exceptions are hidden outputs; changing internal or external state is a common example. section 6.3 section 6.6

Recognize implicit outputs by checking whether:

  • Replacing a method call with its return value changes program behavior, indicating a lack of referential transparency. section 6.3
  • The method changes internal state, as AddComment does when it adds a comment to a collection. section 6.3
  • The computation raises an exception or otherwise produces an effect not expressed in its signature. section 6.6

State-changing method

AddComment(string text) returns a Comment, but also appends it to _comments. The returned object therefore does not describe the method's complete output. section 6.3

public decimal CalculateDiscount(Product[] products)
{
    decimal discount = products.Length * 0.01m;
    return Math.Min(discount, 0.2m);
}

Contrast this explicit calculation with state-changing methods: its input and decimal result are fully represented by the signature. section 6.3

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines hidden outputs, identifies them through referential transparency, and contrasts them with explicit method outputs.
Examples
Uses Increment, AddComment, CalculateDiscount, side effects, and exceptions as concrete comparisons.
What you can do after
Diagnose hidden behavior by comparing a method call with its return value and inspecting state changes or exceptions.
How it connects
Connects implicit output to referential transparency, functional programming, functional architecture, and hexagonal architecture.

Also explained in

Compare how 2 books explain implicit output →

Related concepts

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.