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?
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:
// Class responsible only for creating valid userclassUser{constructor(username,password){if(username.trim().length<3)thrownewError('Username must be at least 3 characters long')if(!password.match(/[0-9]/))thrownewError('Password must contain at least one digit')this.username=usernamethis.password=password}}// Class responsible only for from handlingclassFormHandler{logs=[]constructor(formElement){this.form=formElementthis.form.addEventListener('submit',this.handleSubmit.bind(this))}handleSubmit(e){e.preventDefault()constusername=e.target.elements.username.valueconstpassword=e.target.elements.password.valuetry{constuser=newUser(username,password)console.log(user)console.log(this.logs)}catch(message){this.logs.push(message)alert(message)}}}constform=document.querySelector('form')newFormHandler(form)
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.