DEV Community

Discussion on: DRY & the Wrong Abstraction

Collapse
 
jwp profile image
JWP

My guideline is once, sometimes twice but never three times.

Single Responsibility principal is king and applies to functional programing too.

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

I was thinking about having maybe a getUserById and then a getUserByEmail, if you gonna DRY you can join both together in a getUserBySomething so you avoid repeating most of the query, but then you will have a conditional inside your function that makes the function more complex and it also breaks the Single Responsibility principle and it's worse IMHO.

Collapse
 
jwp profile image
JWP • Edited

Single Responsibility does not mean all functions must only do one thing.

For example if we need new behavior, we simply write a new method whose name indicates that single responsibility. It then allows for parameters to change behaviors. It can contain calls to many other functions but strictly only does what the name implies.

I call this a decorator pattern because it adds functionality by use of optional parameters. Allows parts to be added at will, but never departs from what the name implies. It only does that one thing.

The decorated function prefers use of reusable SRP functions because that code is already done and bullet proof. They are closed for modification.

The Concept is so simple but often confuses pepple.

Doing more than one thing in a function composed of SRP Functions is only more fragile if tests prove it.