« Lies, damn lies and test coverage | Main | I second the motion! »

July 07, 2004

Scroll much?

Does anyone give two hoots about the lexical ordering of code within a source file anymore?

When I was a mere slip of a lad, only knee-high to a grasshopper, I can remember being taught that the correct sequencing of elements within a source file was vital to the rapid comprehension of the contents of the file when the next poor weenie comes along to debug your mess in future times. Of course, some ripe old languages (i.e., COBOL) positively insist your divisions are toeing the company line, but most other languages are fairly lax with such things.

The general format for the perfectly readable object-oriented source file was something along the lines of (from top to bottom):

  • Public constants
  • Private constants
  • Private attributes (is there any other kind?)
  • Constructors (default, then parameterized)
  • Getters and setters (either grouped by attribute or function)
  • Everything else

Obviously, the above list is customized for an object-oriented language like Java, but you get the general idea.

My point is: when using tools with powerful intra- and inter-class navigation features like IntelliJ and Eclipse, source files cease to be viewed as sequentially accessed beasties and become more like randomly accessed data structures, akin to a hashtable. With the navigation techniques available in the tools above, an experienced developer is never more than a keyboard shortcut away from any element in the current source file and only one step removed from the contents of any related file.

I literally cannot remember the last time I used the scroll bar as an aid to finding something in a source file. Automated support for jumping to method declarations, finding method usages and viewing the outline of a class is, IMHO, as valuable a feature as the more frequently lauded refactoring support provided by these IDEs.

As a result of this, the layout of my class files has somewhat... degraded over time. I thank the Good Lord every day that no-one has invented a Checkstyle rule to impose the sort of ordering I mentioned above!

Posted by Andy Marks at July 7, 2004 06:03 PM

Comments

Use Jalopy to format your code (has a plug-in for eclipse and ideaJ) and it will order everything for you too. :)

Posted by: masukomi at July 7, 2004 10:02 PM

These days, I rarely scroll through code at all.

Between having a rule on method length aimed at being able to fit it all on-screen at a time, and the excellent Outline view in Eclipse for navigation purposes, the scroll bar is somewhat unnecessary. :) I could almost wish I was back in Visual Age where all you could see at a time was one method (though I didn't appreciate it enough then).

That said, I notice that the Eclipse outline gives things in this order (at least when I turn on A-Z ordering):

* Static Variables, A-Z
* Static Methods, A-Z
* Instance Variables, A-Z
* Constructors
* Instance Methods, A-Z

Works reasonably well, for me at least. Oh, that and "Open Declaration". :)

Surprisingly, though, my layouts haven't degraded much; I still put my variables at the top, constructors next, and methods after. Admittedly, part of that is probably because Eclipse puts the variables and constructors there for me.

Posted by: Robert Watkins at July 8, 2004 08:53 PM

Post a comment

Thanks for signing in, . Now you can comment. (sign out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Remember me?