Originally a lecture from Hidde de Vries was planned for this weekly Nerd, but during that week I got sick. This lecture from Justus Sturkenboom serves as a subsitute.
What are programming paradigms?
In the world of programming languages its important to know about programming paradigms.
A programming paradigm is an approach to solve problem using some programming language - Geeksforgeeks
Most programming languages are strictly tuned to fit a programming paradigm, for example Haskell is a pure functional programming language, where Java is am object oriented programming (OOP) language. There are other paradigms as well, but these two are by far the most popular among developers.
Javascript is a strange programming language if we view it in this way, because it allows you to do both. The problem here is that it is not tuned to be either a OOP- or a functional programming language. That's why lots of people say JS is an impure programming language.
Object oriented VS functional
Object oriented
As Justus explained it:
I see OOP as a landscape of things. Its intention is to make talking about code easier, because it is easier for us to talk about things instead of functions.
Pros
- Reusability - OOP uses objects which can be used over and over again. I's also possible to change objects to better suit your needs using the four major OOP principles: encapsulation, abstraction, inheritance and polymorphism. I won't go into details what these principles are, but you can find out more here.
- Maintainance - All the groundwork of your app is already created in your class definitions. This makes it easy to maintain your code because its all in one place.
- Security - Classes and methods are often private and cannot be accessed by other objects. This built-in feature makes OOP very secure.
- A metaphor to the real world - Objects are reflections of real world things, which makes it easier to shape and discuss your code.
Cons
- Debugging - Because objects are abstract at first and logic can be added later on, it can be difficult to find bugs in your code. OOP is often preferred because the freedom it gives to the developer, but that freedom can become problematic when too much of that freedom is used.
- Complexity - The landscape of things can expand into quit a large scenery over time. This can be hard to plan for and once is has taken its shape it hard to morph the lanscape into something different. To fully utlize OOP you will need an architect that creates an protects the underlying design.
- Performance - The landscape of things is less performant as they are often larger and requires more computational power to run than functional languages. This is not always true as some languages are some of the fastest languages on the market like C++.
Functional programming
As Justus explained it:
I see functional programming as a street where little factories are located. Each time a function is called, these little factories are processing the data until you're at the very end off that street
Pure functional programming
Pure functional programming has some strict guidelines to how code should be written.
- Works with recursion, functions and parameters
- There are no states
- Function do only one thing. Each function should be just one line of code. If it is more. It already does too much
- Functions can be reused and do the same thing every time
- Functions and data are immutable
- Functions never have side effects
- Functions never use something outside of its own scope
- Functions only take 1 argument
- Functions always return one thing
- You can not use any for loops. All functions that use loops must be either .map(), .filter() or .reduce().
This is quite a list of restrictions, but breaking any of these rules is considered impure functional programming.
Immutable objects
Saving objects in a const still allow its properties to be mutable. You can use Object.freeze() to make objects completely immutable.
Pros
- Side effects - Everything happens inside the function scope
- Return value - Every function returns something and the values can be cached
- Debugging - It is easier to identify bugs compared with OOP
- Faster development time - You don't need an architect to design a landscape of things. You can start coding right away.
Cons
- Recursion - Recursive functions utilizes a lot of the call stack
- Terminology - Functional programming uses a lot of abstract terminology that makes it hard to talk about code. It doesn't use metophors as OOP does with objects.
Conclusion
When it comes down to these programming paradigms I have quite some experience in both. I started out as a .NET developer writing object oriented code in C#. The last few years I have slowly transitioned to a more functional programming style. But that's for an obvious reason. OOP is perfect to write secure, scalable web apps, but it also takes more development time than taking a more functional approach. I'm using a more functional style because it is faster to write and generates less bugs. This is simply more suitable when you have courses where you have to develop web apps that need to be completed in just a few weeks or days.