Suppose the work is done in an additional stream; when the application is minimized and the application is deployed, the flow is killed and a new one is created. (since the stream does not immediately stop, as I understand it, it is recommended to create a new stream). If variables are declared in the stream, they will be lost when creating a new instance of the stream. If you display a variable declaration in the Main class, it is piled up. I’m wondering if declaring a large number of variables in one class affects performance. This time. For visual beauty, a separate class can be created for storing variables, is it right to do so? I want to know, considering all the subtleties, which approach is used by good programmers. Maybe the option that I described is normal and nothing else needs to be invented.

  • Some kind of confusion. And OOP and threads and variables ... And so it all depends on the context of the problem. If this is an academic task for the course, then there will be one approach. If this is a production code, then another. - newman
  • I just write my first application in Java, before this year I was hard at work on qbasic, so my education is not programmer, so this kind of questions arise. Do not put the question in the framework of a specific task. And I'm always interested in How to make performance even higher (although it is no longer required, I love ideal algorithms) and the question How do good programmers do that take into account all aspects of the question (they looked at whether this performance needed, how important is visual beauty and did not forget to take into account the rules of good tones). - Turalllb
  • 2
    Abstraction from a specific task is akin to finding the meaning of life. Occupation fascinating, but useless. - newman
  • Okay, I myself can not specify the question anymore and it seems that the multiple declaration of variables will not bring any problems. - Turalllb

2 answers 2

My answer will be very ambiguous and subjective.

If a class has many variables, then this is no longer the correct class. And the wrong one is most likely due to the wrong architecture. Keeping everything in Main is bad.

A large number of variables in a class affects performance if you often work with objects of this class. That is, create, delete, clone and the more variables the more difficult it is to do.

What do you call

variable storage can be created a separate class

this is called a pattern (you have already been given a link to them) Value Object .

Refactor architecture, in the right you should not have such problems.

@Turalllb MVC is not a pattern, but it does not have a concept and attitude to this issue. You are not using threads properly. A thread in a program should not perform any other functions of the program, but should asynchronously perform the functions of the main program.

  • that's what I needed to know if it affects performance. I realized that this pattern is for a case with a large number of variables, but, as I considered, the MVC pattern is more appropriate. My variables are actually not very much, I asked the question for the future, if you suddenly need to work with a large number of variables. Until I looked at the Value Object, could you tell me where to store the variables that need to be declared outside the additional streams. - Turalllb
  • If you create a separate class where variables are only declared and run this class from the Main class in the main thread, the performance situation will of course not change, but visually everything will be more readable. But to get these variables you have to declare them as static. What other ways are there to get these variables from the extra streams? - Turalllb
  • @Turalllb cannot be accessed from another thread for non-static (unless you create another class in which to store such variables and get a pointer to this class via static). - Ilya Chizhanov

Steve McConnell in his 1993 book Code Complete recommended using about 7 methods per class. In this case, the number of lines in each method must be such that they fit on the screen without using scrolling.
Source: https://stackoverflow.com/a/312650/872294

Subsequently, these and other recommendations were included in "linters" (linters) - programs that check the quality of the code. Linters are often used as part of integrated development tools (IDE), which simply prevent the programmer from writing too bad code.

Therefore, advice from Linus Torvalds is more relevant now: to distinguish a bad programmer from a good programmer, ask him what he considers more important: the style of the code or data structure
Source: https://softwareengineering.stackexchange.com/questions/163185/torvalds-quote-about-good-programmer