Skip to main content

Code Cleaning

Posted by daniel on June 3, 2005 at 8:10 AM PDT

Whipping inherited code into shape

A couple of weeks ago Alain Ravet posted this summary of what he does when he reviews code to clean it up and clarify it. Though you may items to add or delete - this is a great starting point for approaching a new glob of code. Thank you Alain for letting me repost it here:

When I receive code, and before adding anything new to it, I always
spend a little time (1 hour, 1 day) on "preparing" the code for the
later modifications.
This preparation is made in 2 steps:

  • code cleaning
  • code clarifying

Once I've done that, I understand the code/project better, and I can
start adding tests, and functionalities.

Note: all the actions below are possible thanks to modern tools, and
100% safe refactorings => no need to write tests first.

Code cleaning :

First, I run a quick and fully automated analysis of the code, with just
a few rules:

  • remove dead code (classes, methods).
  • remove unused method parameters, variables
  • remove useless variable initialisation
  • remove useless Casting
  • tighten visibility (public => private)
  • move tests code to a separate code tree
  • modernize (Java1.4 => Java5)

Second: amongst the hundreds of inspections rules offered by IDEA, a few
dozens will spot oddities and probable bugs in the code (like modifying
the contents of a collection passed as parameter, etc..). In those "hot"
locations, I add a "todo: check", or "todo: modernize" tag, for later
review.

Code clarifying :
I examine the code structure, starting with the 2-mile high view of the
project, and going down. Along the way, I would perform :

  • rename : (package, class, method, parameter
  • move : package, classes, methods, ...
  • extract/inline method
  • introduce variable

Note: As this has to be fast, I only "cure" the worst problems.
I tag the others for later reviews: "to optimize", "to betterize",
"to fix", "danger", etc..


In our Featured Articles, Bernhard Angerer and Andreas Erlacher have written
Loosely Coupled Communication and Coordination in Next-Generation Java Middleware. The provide a careful overview of JMS and JavaSpaces and compare their features for supporting a distributed application.


N. Alex Rupp returns with a piece on
JPOX 1.1.0-beta-3 in Maven
in today's Weblogs.
"I've been interested in JDO technology for a good year now, and I've begun reapproaching JPOX, which my friend Dion showed me last year. As far as Open Source JDO implementations go, it's supposed to be great. I did run into a few problems while trying to get it running in maven, though. Here are some notes.

Greg Sporrar is making plans for JavaOne just because Charles is going to be there"
"Meet the experts! On Monday evening at JavaOne there will be a gaggle of Sun folks available to answer your questions. Get the scoop on future products and releases and inside tips you can't find anywhere else."

William Wake continues his look at an acceptance testing framework in
Fit code, part 2
"My strategy today is to chew off the routines that are small and/or simple, then go back and figure out the big routines. I have two things I'm trying to understand: "What happens with nested tables?" and "How do I insert stuff into the middle of a Parse?"


In Also in
Java Today
,
in the second part of an interview with Artima.com's Bill Venners, Design Patterns co-author and JUnit co-creator Erich Gamma discusses the appeal and perils of what he calls frameworkitis: "the disease that a framework wants to do too much for you or it does it in a way that you don't want but you can't change it." In Erich Gamma on Flexibility and Reuse, he discusses the challenges of making code reusable and extensible, the role of XP in reusable design, and the advantages of toolkits.

Many web applications are now using AJAX to provide a richer client-side experience, and to reduce the endless page reloads of purely server-driven web applications. This moves some of the responsibility to client-side code, and that can be a good thing. In Joshua Gitlin's XML.com article, Errors and AJAX, he shows how to use the Java-like try-catch-finally supported by recent versions of JavaScript, and use the "onerror" event handler to send error handling into your code.


In Projects and
Communities
,
the JAXB reference implementation is designed for extensibility, and as Kohsuke Kawaguchi writes in his blog entry, Writing a plug-in for the JAXB RI is really easy: "I recently finished improving this functionality further for 2.0, so today I'm going to explain how to write a plug-in."

Members of the Java Games Community take note: O'Reilly has released Killer Game Programming in Java. This 1008-page compendium covers imaging, animation sound, networking, 2D and 3D graphics. Its examples include a tile game, a side-scroller, and a first-person shooter.


In today's Forums, cowwoc provides Feedback on SystemTray support in b38. He asks "What happens if I set a 16-bit icon when the desktop is in 16-bit mode and then the user changes to 32-bit color depth. I'm guessing my old 16-bit icon will look really ugly. I know other native applications deal with this problem (upgrade to a 32-bit icon on demand) but it doesn't look like our code does this. Should I open up a RFE?"

Prunge asks about having
Integer.parseInt, etc. accept CharSequence

"How about having Integer.parseInt, Long.parseLong, Double.parseDouble and the rest accept a CharSequence as a parameter. This would save on object creation when doing lots of parsing from a StringBuilder or Buffer."

In today's java.net
News Headlines
:

Registered users can submit news items for the href="http://today.java.net/today/news/">java.net News Page using our
news submission
form
. All submissions go through an editorial review before being
posted to the site. You can also subscribe to the href="http://today.java.net/pub/q/news_rss?x-ver=1.0">java.net News RSS
feed.


Current and upcoming Java
Events
:

Registered users can submit event listings for the href="http://www.java.net/events">java.net Events Page using our href="http://today.java.net/cs/user/create/e"> events submission form.
All submissions go through an editorial review before being posted to the
site.


Archives and Subscriptions: This blog is delivered weekdays as
the Java
Today RSS feed
. Also, once this page is no longer featured as the
front page of java.net it will be
archived along with other past issues in the href="http://today.java.net/today/archive/">java.net Archive.

Whipping inherited code into shape