Aleksey
Aleksey

Reputation: 487

How do you organize class source code in 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:

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?

Upvotes: 5

Views: 4116

Answers (4)

Mike Samuel
Mike Samuel

Reputation: 120586

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

Upvotes: 4

shonky linux user
shonky linux user

Reputation: 6428

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

Upvotes: 3

Shivan Dragon
Shivan Dragon

Reputation: 15229

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.

Upvotes: 3

Dan D.
Dan D.

Reputation: 32391

Organizing the way you described sounds better than 99% of the Java code I have seen so far. However, on the other side, please make sure your classes don't grow too much and methods are not huge.

Classes should usually be less than 1000 lines and methods less than 150.

Upvotes: 1

Related Questions