Organize your code for its audiences. For example, a class in a library might have these audiences:
- An API client who wants more detail on how a public method works.
- A maintainer who wants to find the relevant method to make a small change.
- 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.
- Mutators
- Accessors
- Iteration
- Serializing and toString
- Equality, comparability, hashing