Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you organize class source code in Java?

Tags:

java

By now my average class contains about 500 lines of code and about 50 methods. IDE is Eclipse, where I turned “Save Actions” so that methods are sorted in alphabetical order, first public methods, and then private methods. To find any specific method in the code I use “Quick Outline”. If needed, “Open Call Hierarchy” shows the sequence of methods as they called one by one.

This approach gives following advantages:

  • I can start typing new method without thinking where to place it in the code, because after save it will be placed by Eclipse to appropriate place automatically.
  • I always find public methods in the upper part of the code (don’t have to search the whole class for them)

However there are some disadvantages:

When refactoring large method into smaller ones I’m not very satisfied that new private methods are placed in different parts of code and therefore it’s little bit hard to follow the code concept. To avoid that, I name them in some weird way to keep them near each one, for example: showPageFirst(), showPageSecond() instead of showFirstPage(), showSecondPage().

May be there are some better approaches?

like image 956
Aleksey Avatar asked Aug 23 '12 08:08

Aleksey


3 Answers

Organize your code for its audiences. For example, a class in a library might have these audiences:

  1. An API client who wants more detail on how a public method works.
  2. A maintainer who wants to find the relevant method to make a small change.
  3. A more serious maintainer who wants to do a significant refactoring or add functionality.

For clients perusing the source code, you want to introduce core concepts. First we have a class doc comment that includes a glossary of important terms and usage examples. Then we have the code related to one term, then those related to another, then those related to a third.

For maintainers, any pair of methods that are likely to have to change together should be close by. A public method and its private helper and any constants related to it only should show up together.

Both of these groups of users are aided by grouping class members into logical sections which are separately documented.

For example, a collection class might have several mostly orthogonal concerns that can't easily be broken out into separate classes but which can be broken into separate sections.

  1. Mutators
  2. Accessors
  3. Iteration
  4. Serializing and toString
  5. Equality, comparability, hashing
like image 169
Mike Samuel Avatar answered Sep 27 '22 23:09

Mike Samuel


Well, naming your methods so that they'll be easier to spot in your IDE is really not good. Their name should reflect what they do, nothing more.

As an answer to your question, probably the best thing to do is to split you class into multiple classes and isolate groups of methods that have something in common in each of such classes. For example , if you have

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

which then you've refactored such that:

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

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

you can make a class called SomethingDoer where you place all these 4 metods and then use an instance of that class in your original class.

like image 32
Shivan Dragon Avatar answered Sep 28 '22 00:09

Shivan Dragon


Don't worry about physically ordering your methods inside the class, if you can't see it just use Ctrl-O and start typing the method name and you will jump straight to it.

Having self-describing method names results in more maintainable code than artificially naming them to keep them in alphabetical order.

Hint: learn your shortcut keys and you will improve your productivity

like image 43
shonky linux user Avatar answered Sep 27 '22 23:09

shonky linux user