Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

functional core
imperative shell
elixir
communitycreator

What are functional core and imperative shell in Elixir?

Muhammad Ali Shahid

Designing an application with a functional core and an imperative shell has several interesting side effects while it makes testing easier. When it comes to testing, all we have to do is check the return value on functional elements, which are often isolated naturally. This enables testing in isolation without the use of test doubles. A functional core also leads to an imperative shell with few conditionals, making it easier to reason about the application’s state.

Let’s begin with some definitions to make sure we’re all on the same page.

Functional programming

What is the functional core?

Functional programming offers practical methods for developing maintainable and scalable software that solves present-day programming challenges. In Elixir, functions are first-class citizens as it is a functional language. One important thing to remember is that in functional programming, data is immutable, which means that it cannot be changed.

In functional programming, there are two types of functions: pure and impure. Pure functions must be predictable and without side effects. If either of these conditions isn’t satisfied, then we have an impure function.

What is an imperative shell?

We can imagine the imperative shell as a blanket that protects the functional core from the outside. Passing data into the functional core, manipulating stdin and stdout, manipulating the database, dealing with the network, and so on, are all handled by the imperative shell. All of this is reliant on values returned by the functional core.

The imperative shell has a small number of pathways but a large number of dependents. The imperative shell, unlike the functional core, is not isolated.

Functional core vs imperative shell

Functional programming is the domain of pure functions while imperative programming deals with side effects and conditional logic. A program composed entirely of pure functions may be extremely clean and predictable, but it will quickly become dull if it never interacts with IO or persistence.

Although side effects add complexity and unpredictability to our code, they also allow the program to be more helpful. Combining the two in a functional core and an imperative shell is an attempt to combine the best of both worlds, with as much of the project written in pure functions as possible while being wrapped in an imperative layer that handles side effects and logic.

RELATED TAGS

functional core
imperative shell
elixir
communitycreator

CONTRIBUTOR

Muhammad Ali Shahid
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring