Intro
This is a real life example showing differences of three most common programming paradigms. I will be solving one problem in three...
For further actions, you may consider blocking this person and/or reporting abuse
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.
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.
Sorry for the ambiguous wording. OO can be very helpful to build code segments in larger projects for two reasons:
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.
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:
The code can be reused in other projects
The code is easier to maintain
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?
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.
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:
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.
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.
Nice summary! Is there an alternative to using classes for OOP?
ES6 classes are basically just syntax sugar for functions, so you can certainly accomplish the class-centric design with just functions.
Yes, but the question is: does this improve your code? If somebody else has to maintain your project, will it help him to understand?
Both solutions are good imo. Just be consistent with your choice and you will be good
Yep! Rust can be considered Trait based OOP.
What do you think about using the functional paradigm combined with patterns and modules?
Great article!