1 Research - kylebot0/functional-programming GitHub Wiki

Functional programming research

What is functional programming?

According to medium.com:

Functional programming (often abbreviated FP) is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. Functional programming is declarative rather than imperative, and application state flows through pure functions. Contrast with object oriented programming, where application state is usually shared and colocated with methods in objects.

But in my own experience, functional programming is working with clear pure functions that briefly describe what they are and what they do, within the original function other functions will be called. That way the code looks a lot cleaner and more readable. Besides that everything flows through parameters and the functions and no longer within objects. Personally i think it looks clean and declarative, but it can be quite tricky to see what the state of an application is. With object oriënted programming you can just set a state and see what lies within, but you can't really do that with functional programming.

Programming paradigms

Functional programming is actually part of a programming paradigm. Meaning that it is a way of thinking about code construction based on some fundamental, defining principles. Other examples of programming paradigms are object oriënted programming and procedural programming. We already learned object oriënted programming.

Functional vs object-oriënted programming

Object oriënted programming and functional programming contain a lot of differences. The advantages of using object oriënted:

  1. Contains a shared state between objects
  2. Easy manipulation of state
  3. Uses object notation

And the advantages of using functional programming:

  1. There is a complete separation between the data of a program, and the behaviors of a program
  2. All objects created in functional programming are immutable
  3. There is no shared state
  4. Uses pure functions

Pure functions

A pure function is a function where the return value is only determined by its input values, without observable side effects. This is how functions in math work: Math.cos(x) will, for the same value of x, always return the same result. Computing it does not change x. It does not write to log files, do network requests, ask for user input, or change program state. It’s a coffee grinder: beans go in, powder comes out, end of story.

When a function performs any other “action”, apart from calculating its return value, the function is impure. It follows that a function which calls an impure function is impure as well.

So what is better?

In my opinion each paradigm has it's own benefits, however if you want to create programs for other people or if you're working in a large team. I think functional programming can be better option for you. That is because if you can reuse functions it can be useful for your team, also the more declarative method of writing code can be more readable for a team. But object oriënted might be easier to learn, so i you're a beginner in learning javascript or any kind of language in particular i would suggest object oriënted. Although it all comes down to personal preference.