DEV Community

John Peters
John Peters

Posted on • Updated on

Typescript and SOLID

JavaScript people often reject the concepts of OOP the way OOP people rejected JavaScript in the early days. Think about that a little longer, if you will.

Alas JavaScript and it's big brother Typescript are here to stay. Much to the chagrin of many OOP-ers. Yes JavaScript is king like it or not. What did the OOP-ers learn in their 20+ year run leading up to JavaScript's dominance?

The major takeaways were 1)Favor composition over inheritance and 2) The SOLID principal. The S in Solid is the Single Responsibility Principle (SRP), which says each class or function or method should only be responsible for one thing. Think hard on that one, commit it to memory and live by it. Following the SRP rule means frequent refactoring. Anytime a new concern is introduced, it is split into a new function, method, class, object etc. This then, results in code full of functions, methods, classes, objects that only do one thing. This is exactly what JavaScript people are teaching when mentioning Pure-functions.

All good OOP winds up at functional-styles anyway. So what's the problem with OOP again?

Any questions?

Top comments (8)

Collapse
 
seanmclem profile image
Seanmclem

I am surprised sometimes to see the length people will go to avoid OOP in js. Like, passing a string to an external function, just to have that function use a switch statement to figure out which other function to run based on the string... Why?

Seriously asking.

Collapse
 
jwp profile image
John Peters

Or to define object structures that are never reused, only to find they are different in two or more places. Try debugging that one.

Collapse
 
stereobooster profile image
stereobooster • Edited

pure function != single responsibility

Collapse
 
jwp profile image
John Peters

Ok semantics aside, any function not doing just one thing is violation of single responsibility pattern. It is immediate technical debt. It's a code smell/anti pattern. The solution: Chaining pure functions each doing their own thing.

Collapse
 
stereobooster profile image
stereobooster

It is immediate technical debt. It's a code smell/anti patter

This is just your opinion. Other people may have another

Ok semantics aside

Semantics is meaning. If we are not talking about meaning, what are we talking about?

Pure function can do one thing, but as well can do many things, it will not change it's pureness.

This is core idea of pure functions, you can combine them and as the result you get new pure function (which is not doing "one thing" anymore). Pureness is about absence of side effects.

On the other function method in object can have single responsibility but the same time it can do some side effect, e.g. it will not seamlessly compose.

Thread Thread
 
jwp profile image
John Peters

..this is the core idea of pure functions... yes composition at it's best.

Yes my opinions are strong on single responsibility, indeed and I stick by them. Notice I never made mention of side effects, that's a whole different topic.

Collapse
 
seanmclem profile image
Seanmclem

Single responsibility principle, like pure functions, are highly suggested. It is understood and usually suggested that you only stick to the principles when it makes sense. Which is not every scenario.

Thread Thread
 
jwp profile image
John Peters

True..