Back
Close

Functional (Programming) mindset

LeoLanese
11.1K views

Functional (Programming) mindset

OOP vs FP: Don't be an FP programmer, don't be an OOP programmer... BE A BETTER PROGRAMMER. ~ @fernando_cejas

Introduction

A large program is a costly program, and not just because of the time, it takes to build. Size almost always involves complexity, and complexity could confuse programmers. Confused programmers, in turn, tend to introduce mistakes (bugs) into programs. Functional Programming can help to make the code predictable and easy to debug, abstract control flows and operations on data with functions.

Functional Programming is about pulling programs apart and reassembling them from the same parts, abstracted behind function boundaries, but it's not a matter of just applying functions to come up with a result; the goal, rather, is to abstract control flows and operations on data with functions in order to avoid side effects and reduce mutation of state in your application.

Functional Programming it is not new, but becomes more popular these days, because lies at the heart of both of the dominant frameworks out there:

  • React: Using immutability as a principle and avoiding shared mutable DOM is the motivation for its architecture and unidirectional data flow.
  • Angular2+:
    ---- Also have unidirectional data flow that encorage the separation of data and functions. Instead of dealing with getters, setters and classes that reference other classes, we pass in the data we want our functions to work with. The data isn't included in the properties of the class where you have to manage the state of properties.
    --- RxJS is a library of utility operators that act on streams by way of Higher-order-Functions, its used extensively throughout Angular.
    --- Typescript the core language of Angular2+, or just Angular, is TypeScript that follow the Hindley-Milner type system convention.

Angular is being advertised as a framework that gives you everything but teaches us a little to none about FP. Angular API is highly influenced by OOP ideas and less influenced by FP ideas, but according to Alan Kay, the investigator for all modern OOP, the essence of OOP is: Encapsulation and Message Passing*. Then OOP is just another approach to "avoiding sharing mutable state and side-effects".

Nevertheless, if we pick up Angular, @ngrx/store (redux), you will actually encounter key functional concepts at the core of what you learn: "pure functions", "immutability", "state", "mutation", "composition" and "reactive". We like more the idea of triggering "actions" that will invoke a "reducer" (pure function & immutable) and/or "@Effect" (no-pure Class & immutable) in Redux than invoking a method in a Service Class in Angular.

We already have RxJS reactive extensions bundled up in your Angular application. Embracing their power will allow you to use the principles of Redux without having the need to introduce specific terminologies like actions and reducers. Redux the core counts similar principle. Redux (@ngrx) organize the application state into simple objects and update this read-only state by replacing it with a new state (no mutate). Also @ngrx/store is a RxJS stream, is a Redux implementation for Angular. Implements the Redux architectural pattern and principles using the well-known RxJS observables of Angular.

Please, join us. Let’s have an easy introduction to Functional Programming, talk about these ideas, what is this style of writing code is and how it could help us to produce a more predictable and testable code.

Thanks,
Leo Lanese

1* prototypes vs classes was: Re: Sun's HotSpot

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants