Aug 26, 2011

Eclipse Clean Code Method Sorter: Try #1


A few months ago, a colleague and I had a hot discussion on good styles of sorting methods in source code, and (of course) we also discussed the advantages of the newspaper-style as proposed by Robert C. Martin:
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

Unfortunately, Eclipse doesn't support sorting members in "clean-code style". Thus, we decided to give away this idea to a pair of students as an Eclipse hand's-on training. This post is a follow-up of my post in May.

To get you started, here is a quick refresh of the problem:

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?
It's amazingly complex :) We had to learn that an optimal solution would be APX-hard to find (did you no that complexity class before? I didn't.) Anyways, we had quite tough discussions with our students and sometimes even bar room fights what might be the best solution. To make that part of the story short,  Mateusz and Fabian came up with two solutions. One solution is already available for testing.

If you like the newspaper-style as described by Robert C. Martin, you should give it a try! Please check this wiki page for further information about the plug-in and installation instructions (it's not an official part of Code Recommenders yet).


To track the progress of this feature, add yourself to this bugzilla report.


Best,
Marcel



Stay in touch with Code Recommenders via Twitter.

5 comments:

  1. Isn't it a plain topological sort? Did I miss something?

    ReplyDelete
  2. If you assume alphabetical order for nodes with no fan-in, and doing a depth-first traversal of the call relations: yes, that's easy :)

    It get's more complicated if you would like to minimize the *total* number of jumps to make (maybe by relaxing the exact news-paper style), if you try to put getters close to their setters and several other features :) Can you imagine other criteria name alphabetic and call-relations?

    ReplyDelete
  3. I like this, but I do have a couple of issues.

    I would like to see getters and setters completely excluded from the invocation ordering strategy. I want the getters and setters to appear last in the class, alphabetically ordered - but considered as outcasts compared to the more important methods!

    As I am coding, I have the occasional public method that isn't invoked within the same class - these are ordered alphabetically within the lowly getters and setters too.

    The functionality is only exposed through right click on an editor > Source. It doesn't appear on the Alt+Shift+S shortcut, or on the source menu.

    Whoever worked on this should be very proud of themselves!

    ReplyDelete
  4. Did this awesome project die already? I just noticed that awkward custom of mine to arrange methods topologically by their call order when I had to debug an awfully large class of 2k+ lines recently and had to jump through the code quite a lot. Now I started googling with that idea in my mind: Why not check if there is some tool that would allow me to
    1) order most method calls topologically (as Robert said, the "more important" methods)
    2) Getters/setters below, alphabetically or according to the order of the member declarations (public getXXX/setXXXX)
    3) event handler methods (public onXXXX)

    And now I see there appear to be no more updates on this genuine project? What a pity! If this is true, is this project open source so that it could be extended by other developers?

    Best,
    Michael Jess

    ReplyDelete
  5. Hi Michael,

    this project was developed by a student during a hands-on but not continued after the final submission. The code should be somewhere around. If you are interested to continue working on it or you know a student that might work on it as part of this year's Google Summer of Code, I can see whether I can get the code.

    ReplyDelete