johankronholm

me

The Fundamentals of Functional Programming

An overview of the definition of functional programming, its user-cases and comparisons of several programming paradigms.

Table of Contents

  1. Definition of Functional Programming
    1. Immutability
    2. Pure Functions
    3. Higher-order Functions
  2. Comparisons
    1. Functional Programming
    2. Imperative Programming
    3. Object Oriented Programming
  3. Sources & References

Definition of Functional Programming

- Breaking It Down: What, When, How

In order to grasp the fundamentals of functional programming, it is essential to understand the context in which it may be applied. As development projects become larger, code may lead to more complexity in which understanding how the code operates comes in the way of understanding of what it accomplishes. Code accumulation in large-scale projects may additionally result in a greater increase of lines of code if the project operates on an imperative approach only — resulting in a greater exposure to errors.

Example of functional programming with Java streams
Figure 1: Illustration of the .forEach method of the Stream class operating on a Java array

By applying techniques of functional programming (as displayed in Figure 1), developers are able to write higher-level code with predefined functions provided by libraries. It enables abstraction by eliminating code details where the developer may instead focus on code accomplishment and work in a more productive and concise manner.

Imutability

- A prevention of Side Effects

Immutability, by definition, refers to an object that cannot be modified. In functional programming, it plays a crucial role in preventing errors and unintended side effects. Since functional methods are generally designed not to alter data, developers can be confident that data states remain unchanged and can retrace any callbacks that may have caused errors.

Higher-order functions

- Improved Modularity and Code Reusage

Higher-order functions are functions that can take other functions as arguments or return them as results. The term 'higher-order' refers to the fact that the order in which these functions are applied affects the final output. They are widely used in functional programming as they enable function reuse and make it easier to trace and correct errors.

Sources & References

  • Deitel, Paul J., and Harvey M. Deitel. How to Program, Early Objects. 11th ed., Pearson, 2018.
  • https://www.javaranch.com/journal/2003/04/immutable.html
  • https://hackernoon.com/9-functional-programming-concepts-everyone-should-know-uy503u21
  • https://www.geeksforgeeks.org/javascript/javascript-higher-order-functions/