The Elm Architecture

The Elm Architecture, or TEA is a pattern for architecting interactive programs.

Pattern

The basic pattern for TEA conforms to three distinct parts:

  • Model, the state of the application.
  • View, a way to turn the state to something visible.
  • Update, a way to change the state based on messages.

The Elm Architecture shares some similarities with the Model-View-Controller (MVC) pattern, but there are distinct differences in their structure and approach. Understanding these differences is key to appreciating how each pattern suits different programming environments and requirements.

Similarities:

Separation of Concerns: Both TEA and MVC aim to separate concerns, making
the code more manageable, understandable, and scalable. Model: In both
architectures, the model represents the data or the state of the
application. View: Both have a view layer responsible for presenting the
application's state to the user.

Differences:

Control Flow: MVC: In MVC, the controller updates the model, and the view
is updated to reflect changes in the model. The controller plays an active
role in processing user inputs and updating the model. TEA: In TEA, the
update function (analogous to the controller in MVC) is a pure function
that takes the current state and a message to produce a new state. It
doesn't directly cause side effects but instead returns commands when side
effects are needed.

Update/Controller Logic: MVC: Controllers in MVC can become complex as they
handle user input, update models, and sometimes even manage view state.
TEA: The update function in TEA handles messages (events) in a more
predictable way. It's a pure function, ensuring that given the same inputs
(current state and message), it will always produce the same output (new
state).

State Management: MVC: In traditional MVC, the model can be mutated from
multiple points, which can lead to less predictable states. TEA: The state
(model) in TEA is immutable. State changes are managed through the update
function, and the current state is replaced with a new state.

Functional Programming vs Object-Oriented: MVC is commonly used in
object-oriented programming languages and environments. TEA is deeply
rooted in functional programming principles, like immutability and pure
functions, which are core concepts in Elm.

Unidirectional Data Flow: TEA strictly follows unidirectional data flow,
which makes the data flow through the app more predictable and easier to
debug. MVC doesn't inherently enforce unidirectional data flow, and data
can flow in multiple directions.

Side Effects: MVC doesn't provide a structured way to handle side effects.
TEA handles side effects using commands and subscriptions in a more
controlled and predictable manner.

While TEA and MVC share the goal of organizing code through separation of concerns, their methodologies and underlying principles differ significantly. TEA’s approach is more functional and immutable with a strong emphasis on predictability and simplicity, especially in state management and handling side effects. MVC, on the other hand, is more flexible and widely used in various programming paradigms, particularly object-oriented programming.