How to organize class source code in Java?

Currently, my middle class contains about 500 lines of code and about 50 methods. The IDE is an Eclipse where I turned "Save Actions" so that the methods are sorted alphabetically, first with public methods, and then private methods. To find any specific method in the code, I use "Quick Outline". If necessary, the Hierarchy of Open Calls shows a sequence of methods that they call one by one.

This approach provides the following benefits:

  • I can start introducing a new method without thinking where to put it in the code, because after saving it will be automatically placed by Eclipse in the appropriate place.
  • I always find public methods at the top of the code (no need to look for the whole class for them)

However, there are some disadvantages:

When refactoring a large method into smaller ones, I am not very pleased that the new private methods are located in different parts of the code, and therefore it is a little difficult to implement the code concept. To avoid this, I call them in some strange way, to keep them next to each of them, for example: showPageFirst (), showPageSecond () instead of showFirstPage (), showSecondPage ().

Maybe there are some better approaches?

+5
source share
4 answers

Well, name your methods to make them easier to find in your IDE, this is really not very good. Their name should reflect what they do, nothing more.

, , , - . ,

public void largeMethodThatDoesSomething() {
 //do A
 //do B
 //do C
}

, :

public void largeMethodThatDoesSomething() {
 doA();
 doB();
 doC();
}

private void doA() {};
private void doB() {};
private void doC() {};

SomethingDoer, 4 , .

+3

. , :

  • API, , .
  • , .
  • , .

, , . doc, . , , , , .

, , , , , . .

, .

, , , .

  • Accessors
  • toString
  • , ,
+4

, , Ctrl-O , .

, , .

: ,

+3

, , , 99% Java, . , , , , , .

1000 150.

+1

All Articles