DEV Community

Discussion on: Private methods are a code smell

Collapse
 
jessekphillips profile image
Jesse Phillips

Adhering to the SRP just to adhere to it isn't helpful in writing "better" code.

I'm not a huge fan of the access modifiers, I generally find that I want access to most things at some point. But that is still different than a new class needs to be created so we can jump through additional objects to reach the programs logic.

Thread Thread
 
koffeinfrei profile image
Alexis Reigel

Adhering to the SRP just to adhere to it isn't helpful in writing "better" code.

The SRP is in my opinion the single most important guideline in writing clean code. I've never seen code getting worse while adhering to the SRP.

I generally find that I want access to most things at some point.

Exactly, thus everything should be as public as possible.

But that is still different than a new class needs to be created so we can jump through additional objects to reach the programs logic.

If you want to have every piece of functionality publicly available you won't get around to extracting to multiple smaller pieces at some point. When a software system grows you have to split up. And having to search for logic in many specific classes is easier than having to look for it in hidden private methods that logically don't belong where they are.

Thread Thread
 
jessekphillips profile image
Jesse Phillips

And having to search for logic in many specific classes is easier than having to look for it in hidden private methods that logically don't belong where they are.

As nice as that sounds, private methods don't actually hide logic, I just don't see myself doing a search in the way you're describing it happens.

It's weird because I agree and disagree at the same time. I generally don't think class are required for most logic, they should generally be free functions which operate on the class/data. This way your class keeps its single responsibility and you have functions with one responsibility.

Thread Thread
 
koffeinfrei profile image
Alexis Reigel

private methods don't actually hide logic.

They usually do though, which is (in my experience) the main reason for them to be private (which this post details).

I generally don't think class are required for most logic, they should generally be free functions which operate on the class/data. This way your class keeps its single responsibility and you have functions with one responsibility.

Neither do I, it heavily depends on the language you're using. A lot of languages don't require you to use classes to encapsulate logic. Using classes actually enables the use of private logic. Using functional approaches leads to treating each function as a valid public entity, generally leading to less running into the "private code smell".