What is functional programming?
Functional Programming is all about deconstructing a problem into small and reusable functions that take an input and return a result. This allows them to operate in a way that does not mutate or change the data that is passed to them.
In functional programming data is not mutated once assigned.
What is the benefit to writing functional components?
Functional programming relies on the principle of immutable data structures to empower it’s functions and components to fulfil highly specialized use cases. There are a lot of benefits to composing functions in this manner. functional programming components tend to be more concise, easier to debug, easier to test, and more scalable. This is because we can make many function calls in parallel and take advantage of multicore CPU’s.
Let’s assume that we have a string that we want to trim and wrap inside a div element. If we were hard coding it we would do something like you see below.
As you can see this is not an immutable way to accomplish this task because input was mutated when we declared output. So how can we accomplish the same thing functionally? Well we can quite simply break the problem down into two small steps that accomplish the same result like so…
As you can see, by breaking the problem down into small functional components we were able to accomplish the same exact result in an immutable way. This allows our functions to be called and used on any string that we want to convert in this way. It also allows us to use the default value of input later on in our code if needed.