Concepts that are closely related should be kept vertically close to each other. We want to avoid forcing our readers to hop around through our source files and classes.
Robert C. Martin "Clean Code" pp. 80
If you have to read other people's code quite often you probably know this, right?
Typically, there are two types of code ordering. Alphabetically (+ modifiers) or completely hand-crafted (which in many cases means "without any particular order"). But is it that complicated to let the IDE sort a class' members for you? Reducing the need to hop from top to the bottom of the file and thus minimizing scrolling and confusion?
The slide below gives you an example of how a well-sorted piece of code might look like. As you see the number of hops required to read the whole code is minimal since there is a clear ordering between the code:
How complicated can it be?
In the end, there are just a few things to do:
- Extracting the caller-callee relationships from code,
- Sorting these relations using some topological sorting algorithm,
- Dealing with some pat/remi situations where several elements may have the same ordering, and finally
- A "physically"reordering these methods in your source file.
I'm glad that we found two students who will implement this nice Clean Code Method Sorter during their Eclipse hands-on this semester. I'm really looking forward to their clean code!
Good Luck, Mateusz and Fabian!
Stay in touch with Code Recommenders via Twitter.