Functional core, imperative envelope (2012)

← Testing isolation without mocking reducing services to values ​​→

Purely functional code makes some things easier to understand: because values ​​don't change, you can call functions and know that only their return value matters; they don't change anything outside of themselves. But this makes many real-world applications difficult: how do I write to a database or to the screen?

In this screencast, we look at a method to bridge that gap. We review a Twitter client whose core is functional: managing tweets, syncing timelines with incoming data from the Twitter API, remembering cursor positions in the tweet list, and rendering tweets to text for display . This functional kernel is surrounded by a shell of imperative code: it manipulates stdin, stdout, the database and the network, all based on values ​​produced by the functional kernel.

This design has many pleasant side effects. For example, testing functional parts is very easy, and this often naturally allows for isolated testing without duplicate testing. It also leads to an imperative shell with few conditions, which makes it much easier to reason about the state of the program over time.

Run the program

Looking for something more interactive? Try Execute Program, an interactive learning platform from Destroy All Software LLC! It offers courses on TypeScript, SQL, regular expressions, JavaScript concurrency, and more. All Destroy All Software subscriptions include full access to the Execute program, or you can subscribe directly to the Execute program.

← Testing isolation without mocking reducing services to values ​​→

Purely functional code makes some things easier to understand: because values ​​don't change, you can call functions and know that only their return value matters; they don't change anything outside of themselves. But this makes many real-world applications difficult: how do I write to a database or to the screen?

In this screencast, we look at a method to bridge that gap. We review a Twitter client whose core is functional: managing tweets, syncing timelines with incoming data from the Twitter API, remembering cursor positions in the tweet list, and rendering tweets to text for display . This functional kernel is surrounded by a shell of imperative code: it manipulates stdin, stdout, the database and the network, all based on values ​​produced by the functional kernel.

This design has many pleasant side effects. For example, testing functional parts is very easy, and this often naturally allows for isolated testing without duplicate testing. It also leads to an imperative shell with few conditions, which makes it much easier to reason about the state of the program over time.

Run the program

Looking for something more interactive? Try Execute Program, an interactive learning platform from Destroy All Software LLC! It offers courses on TypeScript, SQL, regular expressions, JavaScript concurrency, and more. All Destroy All Software subscriptions include full access to the Execute program, or you can subscribe directly to the Execute program.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow