Make bad code good
Refactor broken Java code for fun and profit
By Dr. John Farrell, JavaWorld.com, 03/23/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
So, you've inherited some code. It's 50,000 lines of the oldest code on the project. The authors have left the company and
won't return your calls. It's undocumented, badly designed, and buggy; yet the rest of the team uses it all the time. There's
absolutely no option but to make it work, and that's your job. What do you do?
Don't stress. This article will make some concrete suggestions as to the sorts of things you can do, from the simple and mindless
to the complex and dangerous. The process can be mostly evolutionary and absolutely safe. In a couple of months, colleagues
will stop complaining about your section of the code, and a couple of months after that, your revamped code will be the pride
of the project.
Some of the suggestions that follow are nothing new. Indeed, concepts such as documenting code and following coding standards
have been drummed into programmers since they went to school. However, the usual reasons given are motherhood statements --
things you wouldn't say the opposite of (be a good boy, write understandable code, for example). I've tried to explain why
you would want to do those things, and what their positive effects are.
So, let's get to it.
Start with the easy things
When making bad code good, it's best to start with easy changes, to make sure you don't break anything. A large lump of bad
code breaks easily, and nobody can be expected to fix bugs in it without some preparation.
Fix the Javadoc comments
You never realize a comment's importance until you need to read it. Even if nobody else reads it (and usually, they don't),
Javadoc comments are important for the code authors to help them remember what the code/class/parameter should do. Pieces
of information particularly important to include are:
- Whether an object parameter may be null, and what it means if it is
- Whether a parameter needs to be mutable or not
- Whether a return value is mutable
- Whether a return value may be null
- Whether changes to the return value affect the returner's internal state
Fixing the Javadoc comments doesn't mean just adding them where there were none before. You should also delete misleading
and trivial comments (such as documenting setters and getters) as they engender a false sense of security. Important information
exists for inclusion in Javadoc comments, so there's no point wasting the time of potential readers.
When you write new methods and classes, try to include some Javadoc comments (just a few) to explain what they do. Even if
it's obvious to you, the next person to get the code might thank you for the effort. If you don't know what to write in the
Javadoc comments, you have no business writing the code.
Javadoc comments can also serve as a record of what you learned about the code. When you figure out what a particularly tricky,
clever, or ugly method does (especially a private or protected method with no implied contract), write a comment to record it for posterity. This expands the amount of the code under your
control and helps you later when you're trying to decipher something related.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Nested exceptions are such a good idea that Sun is considering adding them to the next release of the JDK. Read "Java Tip
91Use Nested Exceptions in a Multitiered Environment" Terren Suydam (JavaWorld)
http://www.javaworld.com/javaworld/javatips/jw-javatip91.html
- For more JUnit tips and tricks, read "JUnit Best Practices" Andy Schneider (JavaWorld, December 21, 2000)
http://www.javaworld.com/jw-12-2000/jw-1221-junit.html
- Java coding standards differ from project to project, but one widely accepted set is Sun's own "Code Conventions for the Java
Programming Language"
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
- The Law of Demeter is an object-oriented design principle of debatable value http://www.c2.com/cgi/wiki?LawOfDemeter
- Jikes, IBM's natively compiled Java compiler, is an order of magnitude faster than javac
http://www10.software.ibm.com/developerworks/opensource/jikes
- JUnit is an automated test framework derived from a similar Smalltalk tool
http://www.c2.com/cgi/wiki?JavaUnit
- Refactoring is one of the fundamental practices of Extreme Programming and is absolutely essential to understand if you are
working with existing code. Read RefactoringImproving the Design of Existing Code by Martin Fowler (Addison-Wesley, 1999)
http://www.amazon.com/exec/obidos/ASIN/0201485672/javaworld
- "Test InfectedProgrammers Love Writing Tests" -- Kent Beck and Erich Gamma's inspiring article will make you enthusiastic
to start writing automated unit tests, if I haven't succeeded
http://members.pingnet.ch/gamma/junit.htm
- The WikiWiki Web is the Web's home of design patterns, extreme programming, and object-oriented design
http://www.c2.com/cgi/wiki?WikiWikiWeb
- Extreme Programming is a modern software construction methodology low on ritual and high on delivery
http://www.xProgramming.com
Exception handlingBy Koos Gadellaa on July 20, 2009, 9:17 amAlthough it may not be nice to have a lot of catch() clauses (each wrapping to it's own FooException), it is somewhat deemend appropriate. catch(Exception) also...
Reply | Read entire comment
how i can...By Anonymous on July 6, 2009, 12:45 amhow i can make a bad code and can you give any kind of code.???!!!
Reply | Read entire comment
View all comments