Object-orient or functional programming?

How about object-oriented vs functional programming?

If you guessed object-oriented is better you’re completely wrong :)

And if you guessed functional programming you’re way off base ;)

Debating programming paradigms at this level is like debating art.

There is always more than one way to solve a problem especially in Javascript and it’s great to debate these things but there are no absolutes.

There is going to be a trade-off for every decision that you make.

Let’s start by taking a look at some functional typescript code.

Pure Functions

The most important concept in functional programming is the concept of pure functions. This means that the output of your function should only depend on its inputs. For example:

let num = 123;
function toString(val) {
    return val.toString();
}

Here we have a function called toString() which takes a value as its argument and then returns that value formatted as a string. We can make this an impure function by mutating the number variable directly.

let num = 123;
function toString(val) {
    num = val;
    ...
}

This would be considered a side-effect and functional code should produce no side-effects. In addition they should not rely on any outside value to produce a return value. Pure functions are easier to test and also easier to reason about because you don’t have to think about anything happening outside of the function itself.

#functional-programming #software-development #coding #typescript #object-oriented

Object-Oriented vs Functional Programming with Typescript
1.50 GEEK