Apr 29, 2011

"Clean Code" Method Sorter for Eclipse?

Have you ever chased your tail through a class, hopping from one function to the next, scrolling up and down the source file, trying to divine how the functions relate and operate, only to get lost in a rat’s nest of confusion? Have you ever hunted up the chain of inheritance for the definition of a variable or function? This is frustrating because you are trying to understand what the system does, but you are spending your time and mental energy on trying to locate and remember where the pieces are.

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:

  1. Extracting the caller-callee relationships from code,
  2. Sorting these relations using some topological sorting algorithm,
  3. Dealing with some pat/remi situations where several elements may have the same ordering, and finally
  4. A "physically"reordering these methods in your source file.
That's all. At least in theory. It's well known that "in theory, there is no difference between theory and practice", and thus, it should be straight-forward to implement :-P

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!

Track progress of this feature via Bugzilla.

Stay in touch with Code Recommenders via Twitter.


  1. That is a great (and simple) idea, I can't live without it already! :)

  2. her türlü temizlik işinizde yanınızdayız...:))

  3. This is cool stuff, I've found myself wishing for this kind of features before. Is there a bug to follow?

  4. I created https://bugs.eclipse.org/bugs/show_bug.cgi?id=344394 for tracking purpose.