When to use private methods?

I understand what public / protected / private accessors mean in Java or PHP. However, when you decide whether to make a private method?

Imagine that I have a class that processes configuration lines — they must match a particular regular expression, and if so, further logic is executed to make sure the lines are valid.

This code is currently used in a private method in the configuration class. This class accepts configuration lines and then returns values ​​to the client code after checking the lines.

However, I want unit test verification code, so maybe it should be in another class. I usually do not do this, although I know that the code will be reused. If it will be used by only one class, as in this case, I usually just make it private.

So my question is: what design rules should inform a programmer that a particular method should be private compared to being moved to its own class?

+3
source share
6 answers

- , . , (, , ). , , , , , , ​​, , , , .

+3

, , , , , , .

/ , .

+1

, . , , . . refactor

+1

strategy, . , unit test , , .

, Validator, IValidator. Configuration Validator , .

+1

unit test ( ++, - Java, unit test ).

0

SOLID SOLID

0
source

Source: https://habr.com/ru/post/1776879/


All Articles