DEV Community

Discussion on: Explain functional programming to me like I'm five

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

I agree with what the others said about pure functions and immutability and will extend it a bit.

If you want to bring functional programming to the real world,
there is a great pattern you should be aware of :
Functional Core, Imperative Shell
where you put your business logic in easily testable pure functions and have a thin imperative layer that is responsible for doing the side effects.

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 themselves. But this makes many real-world applications difficult: how do you write to a database, or to the screen?
In this screencast we look at one method for crossing this divide. We review a Twitter client whose core is functional: managing tweets, syncing timelines to incoming Twitter API data, remembering cursor positions within the tweet list, and rendering tweets to text for display. This functional core 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 core.
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.

Do watch the screencast => destroyallsoftware.com/screencasts...