DEV Community

Cover image for Functional vs Object Oriented vs Procedural programming

Functional vs Object Oriented vs Procedural programming

Jakub Jabłoński on August 09, 2021

Intro This is a real life example showing differences of three most common programming paradigms. I will be solving one problem in three...
Collapse
 
efpage profile image
Eckehard

Thank you for the nice writeup!

What about code segmentation in functional code? I know that OO often is a bit verbose, but this often pays back if your project grows.

Collapse
 
jjablonskiit profile image
Jakub Jabłoński

To be honest I'm not sure what you mean by 'code segmentation'.

But yes, OO is very verbose and it's easy to find what you are looking for in code.

Collapse
 
efpage profile image
Eckehard

Sorry for the ambiguous wording. OO can be very helpful to build code segments in larger projects for two reasons:

  1. Classes define a separate a relatively closed environment with own namespace, defined visibility and so on. So, a class or a class hierarchy can be developed without knowing too much about the rest of the task.

  2. Relatively large portions of your code can be isolated, tested and maintainted inside classes. Think of a graphics system, a database adapter, a storage system etc.. This has many advantages for the project:

  3. The code can be reused in other projects

  4. The code is easier to maintain

  5. The class can be used to isolate your application from the physical environment. Example: If a database adapter does all the database handling, you only need to change the adapter to exchang the database system.

Is there any similar mechanism in functional programming?

Thread Thread
 
jjablonskiit profile image
Jakub Jabłoński

You can do the same things as you would in OOP.

Common thing is to just encapsulate logic in separate files. Ex. auth.js exporting functions like login/logout. But not necessarily as a part of any higher object. You can then just import a specific function you need whenever you want without class boilerplate and share and test them as easily as in OOP.

Testing and sharing can also be solved just by keeping related logic within the module folder.

Surely there have to be some ground rules established when working in team. It gives you more freedom but can sometimes fireback because of the amount of ways to structure FP projects.

I think I get your point though, in this simple one file example FP looks like a mess.

Thread Thread
 
efpage profile image
Eckehard

I really appreciate your post, as it is pretty instructive to have a direct comparison. I think it is not helpful to talk about programming paradigms without a task. Here it is clear that it is more a demonstration.

From my experience the three approaches are not mutually exclusive. As classes always are a bit more "expensive" (with respect to code length and brain power), you should use them only where it is necessary or helpful. But to be true, it is never really necessary to use classes, it only may be helpful.

If a class only contains a single function or static data, it´s simply waste of time and memory. OO is no religion, it´s a tool.

So, your code could look like this:

// Class responsible only for creating valid user
class User {
  constructor(username, password) {
    if (username.trim().length < 3)
      throw new Error('Username must be at least 3 characters long')
    if (!password.match(/[0-9]/))
      throw new Error('Password must contain at least one digit')

    this.username = username
    this.password = password
  }
}

// Class responsible only for from handling
class FormHandler {
  logs = []
  constructor(formElement) {
    this.form = formElement
    this.form.addEventListener('submit', this.handleSubmit.bind(this))
  }

  handleSubmit(e) {
    e.preventDefault()
    const username = e.target.elements.username.value
    const password = e.target.elements.password.value

    try {
      const user = new User(username, password)
      console.log(user)
      console.log(this.logs)
    } catch (message) {
        this.logs.push(message)
        alert(message)
    }
  }
}

const form = document.querySelector('form')
new FormHandler(form)
Enter fullscreen mode Exit fullscreen mode

The form handler class might be useful for better reusebility. But here, it only contains one user and one function, so it could easyly substituted by a function, leaving the user as a global definition (which now is encapsulated by the form handler). So, in fact, the user class is the only useful class here.

Pleas don´t misunderstand me, your examples are great as a demonstation. But it also demonstrates a typical misunderstanding: Classes are no value on it´s own. They are only a tool.

By the way: There is no reason not to use functional approaches inside of class functions. And possibly you could write better procedural code, if you try to minimize side effects.

So, from my point of view there are many developers out there, that think they have found the golden calf using one or the other approach. There is nothing like the miracle paradigm that solves all your problems. You will benefit most if you learn to use any approach where it is best suited. If you want to print "Hello world", you even do not need a computer. Use a typewriter instead.

Collapse
 
eljayadobe profile image
Eljay-Adobe

The characteristics I look for in Functional Programming
• first-class functions
• higher-order functions
• code as data
• immutable data
• pure functions (aka referential transparency)
• recursive (specifically: tail recursion)
• manipulation of lists
• lazy evaluation
• pattern matching
• monads (along with currying and partial application)
• separation of behavior from data

For the "What's a monad?" nigh inevitable question: a monad is a function that takes exactly one parameter and returns one result. Using a tuple for the parameter is usually considered cheating. Is is common for a monad that takes one parameter returns another monad, or a monad that returns a result.

A JavaScript example of a monad that returns a monad that returns a monad that returns a result of all the parameters multiplied together:
const cf = (a) => (b) => (c) => (d) => { return a * b * c * d; }

The power of monads is partial application and function composition.

Collapse
 
buondevid profile image
buondevid

Nice summary! Is there an alternative to using classes for OOP?

Collapse
 
alrunner4 profile image
Alexander Carter

ES6 classes are basically just syntax sugar for functions, so you can certainly accomplish the class-centric design with just functions.

Collapse
 
efpage profile image
Eckehard

Yes, but the question is: does this improve your code? If somebody else has to maintain your project, will it help him to understand?

Thread Thread
 
jjablonskiit profile image
Jakub Jabłoński

Both solutions are good imo. Just be consistent with your choice and you will be good

Collapse
 
theschemm profile image
TheSchemm

Yep! Rust can be considered Trait based OOP.

Collapse
 
juanmavargas profile image
Juan Manuel Vargas • Edited

What do you think about using the functional paradigm combined with patterns and modules?


// userEntity
// factory.js
export const createUser = (e) => ({
  username: e.target.elements.username.value,
  password: e.target.elements.password.value,
});

// validators.js
export const validateUserName = (username) =>
  username.trim().length < 3 && "Username must be at least 3 characters long";
export const validatePassword = (password) =>
  !password.match(/[0-9]/) && "Password must contain at least one digit";

// schema.js
export const schema = {
  username: validateUserName,
  password: validatePassword,
};

// validator.js
const validateSchema = (entity, schema) => {
  const errors = Object.entries(schema).reduce((acc, [key, validator]) => {
    const errorMessage = validator(entity[key]);
    if (errorMessage) acc[key] = errorMessage;
  }, {});
  return Object.entries(errors).length && errors;
};

// formHandler.js
const addSubmit = (form, handle) => form.addEventListener("submit", handle);
const createForm = (selector = "form") =>
  document.querySelector(selector || "form");

// logger.js
const logErrors = (errors, storage) => {
  Object.entries(errors).forEach((error) => {
    storage.push(error);
    alert(error);
  });
};

// userForm.js
import userEntity from "@userEntity";
import formHandler from "@formHandler";
import validator from "@validator";
import logger from "@logger";

const createUserForm = (selector) => {
  const form = formHandler.createForm(selector);
  const logs = [];

  formHandler.addSubmit(form, (e) => {
    e.preventDefault();
    const user = userEntity.createUser(e);
    const errors = validator.validateSchema(user, userEntity.schema);
    if (errors) return logger.logErrors(errors, logs);
    return { user, logs };
  });
};

// SomeView.js
const userForm = createUserForm("form");
console.log(userForm);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
keyvan_m_sadeghi profile image
Keyvan M. Sadeghi

Great article!