Is it private necessary for a standalone Java application?

I read a bunch of best practices on the Internet for JUnit and Java in general, and the great thing people would like to point out is that fields and methods should be private if you really don't need to allow users to access them. Class variables should be private using getters and setters, and the only methods you should expose should be those that users will call directly.

My question is: how strictly necessary are these rules when you have things like stand-alone applications that don't have users? I am currently working on what will run on the server, perhaps once a month. There are configuration files that the application uses that can be changed, but otherwise there is no real user interaction after starting it. I mostly followed best practices, but ran into problems with unit testing. It’s a lot of time when it seems to me that I’m just jumping through hoops, and my unit testing gives everything in order, and it would be much easier if this method or something was public or even protected.

I understand that encapsulation will make it easy to make changes behind the scenes without having to change the code all the way through, but without impacting users it seems a little more flimsy. I just make my current job more complicated, even though it will save me later. I also saw all the answers on this site saying that if you need a unit test private method, you are doing something wrong. But this is based on the idea that these methods should always be private, and this is what I am interrogating.

If I know that no one will use the application (by calling its methods from the jar or API or something else), is there something wrong that everything is protected? Or even publicly? How about saving private fields, but every way to publish? Where is the balance between the “correct” accessibility to pieces of code and ease of use?

+4
3

"", "" .

, . - , . , , . , , , , , , , ( "" ).

, , " ", . , , , .

, , "", , . . , .

, . API , , , API . A A, B, API. , A, , A API, . , , , A - .

, ? , . (Perl, AWK ..), Java , "" , , .

+2

, - , .

" " , , , /. private ; / , , , .

, , -, getter/setter, : , , , , , / / , , / .

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

+1

, . , / " "; , ; "" :

  • , - . , . , : - ... , . . , ... : ; , , .
  • . : , " ", , . , " "; ; . " "; // used for unit testing. , . : . .
  • : , . , , , , - . ; . - , . ; . : . , , . , ; ) /. (. " " , 6 " " )
0
source

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


All Articles