Please enable JavaScript.
Coggle requires JavaScript to display documents.
Functional Core / Imperative Shell Pattern - Coggle Diagram
Functional Core / Imperative Shell Pattern
References
The Functional Core, Imperative Shell Pattern
Functional Core Imperative Shell in Javascript
Functional Core Imperative Shell
Functional Programming
Pure Function
The function always returns the same result given the same arguments
The function does not have any side-effects
Declarative Programming
Sets of declarations, or declarative statements, each of which has meaning (often in the problem domain) and may be understood independently and in isolation.
Declarative Statement
Declarative sentences simply make a statement (or a declaration).
They pass on information.
A declarative sentence always ends with a period (full stop).
subject-verb–object–place-time.
The usual word order for a simple declarative sentence
Examples
Rachel is Irish.
This is a declarative sentence.
The word order is subject-verb.
A declarative sentence expresses an active state of being in the present tense
A declarative sentence should always express using the present tense and it should be in an active state. The subject of a declarative sentence should precede the verb.
A declarative sentence is a type of sentence that states a fact, information, or an argument.
Benefits
A pure function always does the same thing
One of the goals of “functional core, imperative shell” is to avoid mocking all together
Key Architectural Attributes
Core
Functional programming
Immutable Data
No Side Effects
Business Decisions - Business Logic
Written in Pure Functions
Shell
Handles interactions with the outside world, such as persisting data in databases or providing UIs to interact with end users.
Imperative programming happens here. It is where all the messy, mutable, side-effects happen that most applications need to do.
Calls the core.
Dependency Rule
Core cannot call the shell.
Core is unaware of the existence of the shell
Key Objective
separate functional code and imperative code
minimize the imperative code, so when in doubt whether a piece of functionality belongs in the core or shell, then make it functional and put it in the core.
Cons
This design has many nice side effects. For example, testing the functional pieces is very easy, and it often naturally allows isolated testing with no test doubles. It also leads to an imperative shell with few conditionals, making reasoning about the program's state over time much easier.
Angular Functional Core
Organize Angular Project
File Naming Guideline
Do follow a pattern that describes the symbol's feature then its type. The recommended pattern is feature.type.ts
General Naming Guidelines
Do use conventional type names including .service, .component, .pipe, .module, and .directive. Invent additional type names if you must but take care not to create too many.
Style
Name core functions using conventional type name of core. (ex. filename.core.ts) Type name of core provides a consistent way to quickly identify core functions.
Procedural Programming
Imperative Programming
Sequences of commands, each of which perform some action; but which may or may not have meaning in the problem domain.
Imperative Sentence
Is a command or a polite request.
examples
Fetch my umbrella!
Please bring my umbrella.
In Practice
Core will be an instance variable in the shell
The shell will call the core
with a value and get a new value returned
Then do side-effectful, imperative stuff, like displaying it to a user or storing it in a database, with value returned from the core.