Functional refactoring

April 24, 2016

Refactoring is one of the important processes in software development.

This is an exploration of of code written in familiar imperative programming patterns, and refactored using a counterpart functional programming pattern.

The goal is to build up a handy bidirectional map of corresponding imperative and functional patterns.

These patterns use Scala to take advantage of its hybrid object-oriented and functional design, which caters to both imperative and functional patterns.

Definitions

Functional

adjective

  1. referentially transparent.

Referentially transparent

We use RĂșnar Bjarnason's definition of referential transparency.

adjective

  1. an expression e is referentially transparent if for all programs p, every occurrence of e in p can be replaced with the result of evaluating e without changing the result of evaluating p.

Refactoring

We use Martin Fowler's definitions of refactoring.

noun

  1. a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.

verb

  1. to restructure software by applying a series of refactorings without changing its observable behavior.

Table of contents

  1. Replace mutator method with deep copy
  2. Replace mutable variables with the state monad
  3. Replace loops with folds
  4. Replace exceptions with sum types
  5. Replace dependency injection with the reader monad

References