Violation of the functions of public users in many private member functions

When I write an open member function of a class that performs several actions, for example ..

void Level::RunLogic(...);

In this function, I found that I am separating it into several private member functions. It makes no sense to break the function of a public participant into several functions, because you would not do anything without the other, and I do not want the user to worry about what in which order, etc. Rather, the RunLogic () function, look something like this ...

void Level::RunLogic(...) {
 DoFirstThing();
 DoSecondThing();
 DoThirdThing();
}

With DoThing functions being private member functions. In Code Complete, Steve McConnell recommends reducing the number of functions you have in the class, but I would prefer not just to translate all this code into one function. My guess about its true meaning is that the class should not have too much functionality, but I'm just wondering what other programmers think about this.

In addition, I moved to see less and less implementation details in my public member functions, and transferred most of the work to small private member functions. Obviously, this creates more functions ... but where the question lies.

+3
source share
7 answers

, public .

McConnell , , .

. , , . , , .

, , .

+2

, . ! :

double average(double[] numbers) {
  double sum = 0;
  for (double n : numbers) {
    sum += n;
  }
  return sum / numbers.length;
}

double sum(double[] numbers) {
  double sum = 0;
  for (double n : numbers) sum += n;
  return sum;
}

double average(double[] numbers) {
    return sum(numbers) / numbers.length;
}

, , .

, , unit test , RunLogic.

+1

.

, , , , , . .

- , , , , . , , - , ; .

+1

, , - . , (, ) -, .

, , - . -, , IDE, . - .

, , , , . , , - (, - , ..). - API.

0

ff:

A class should have only one reason to change / Single responsibility principle

. , , , - change

0

, , ( ). , , , , , , .

, . , .

0

A ' ', , , , . , , " " . ( ) . , (!), , . , , .

0

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


All Articles