I have been experimenting with various ways of ordering the variables and methods in
classes. You want a consistent scheme to help you:
The third consideration is less important these days with quasi-SCID tools that let you find the definition for
something at a mouse click.
- Understand how the class works
- Have onscreen all at once everything that is relevant to the current task.
- Find the little piece are looking for.
You can order for the convience of either someone using your class as a black box,
or for someone who is trying to understand how it works inside, or for the convience
of the maintenance programmer thoroughly familiar with it.
There are tools such as VAG and Eclipse that will reorder methods and declarations
for you. It does not matter to the computer, unless initialisation code contains forward reference.
Here are some possible schemes.
- Top down. But the highest level methods at the top and the fine detail at the
bottom. This way someone can understand your class from the top down or the bottom
- Public first, the protected, then private. This puts the stuff of most interest
to your clients at the top. They can ignore the rest.
- Put varibles just above or just below the methods that use them most. Then they
will likely be conviently onscreen for reference automatically when you are viewing
the code that uses them.
- Put all the variables at the beginning or the end of the class. So it is easy
to see just what data a class holds, and thus what general potential it has for
methods without even looking at the methods. Within that you can possibly group the
constants public static final, the class variables and
the instance variables.
- Sort alphabetically within category. If you name your methods and variables
appropriately this will group related variables together. For a start all the
getXXX and setXXX will
For keywords order preferably like this: public, protected,
private, abstract, static, final, transient, volatile, synchronized, native,
I’d interested in discovering links to tools that will automatically