liveBook · Manning

Principle of least power

As explained in Functional Programming in Scala, Second Edition

The principle of least power is the design principle of exposing only the minimal functionality required by a program.

The principle of least power designs a program with only the functionality required to accomplish its job. Restricting available effects narrows what the program can do, improving local reasoning and reuse. section 13.6

Restricting effect capabilities

A Free program over Console can interact with the console but cannot read files. Adding Files permits file access while still excluding database and network operations. The type records these capabilities explicitly. section 13.6

trait Console[F[_]]:
  def readLn: F[Option[String]]
  def printLn(line: String): F[Unit]

A Console capability exposes only reading and printing, allowing programs such as greet to depend on console operations without knowing the target monad. section 13.6

Capability traits offer a direct alternative to constructing Free programs and interpreting them later: each trait names the permitted operations, while instances supply their implementation for a target monad. section 13.6

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Demonstrates the principle through Free types and capability traits, while noting encoding boilerplate and interpretation indirection.
Examples
Uses Console-only programs, Files-plus-Console programs, greet, and cat to show explicit capability boundaries.
What it assumes
Assumes familiarity with effectful programs, monads, Free, and type-level unions; capability traits provide the alternative formulation.
What you can do after
Apply it via Free types or capability traits to restrict effectful programs and make their permitted operations explicit.
How it connects
Connects effect algebras, Free, interpreters, capability traits, target monads, and local reasoning.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.