Home > Articles > Programming > Java

The Bad Code Spotter's Guide

Diomidis Spinellis
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Old maps were marked with the phrase "Here be Dragons" to help seafarers steer away from dangerous places; in programming the best way to avoid dealing with bad code is to avoid writing it. Diomidis Spinellis points out 10 giveaways to spot bad code that you (or others) may have written.

The ability to spot substandard code is crucial both for programmers and software development managers. As programmers, when we encounter bad code we know we have to take a defensive stance: plan for more development time, expect bugs to crop out of nowhere, and anticipate that small changes may require significant rework. Ideally, we should also arrange for an opportunity to give the code a facelift, refactoring the worse-designed elements and correcting style problems. As managers (for others or of our own work), when we find bad code we need to take a step back and evaluate the situation. Why am I or my group writing this trash? Are there schedule pressures or insufficient incentives for writing brilliant code; is there a need for more training, better design, different development practices?

Here’s a list of 10 giveaways I typically use to spot bad code that I or others have written.

Poor Commenting

Writing good comments is more difficult than writing good code, and therefore identifying code whose commenting is poor is a child’s play. If you see nontrivial functions or methods lacking a comment at their beginning explaining what they do, you know you’re in trouble. The same goes for global variables, class and structure fields, and code blocks implementing a complex algorithm: all should be accompanied by a comment. Note that I don’t expect to see everything adorned with a comment: getter and setter methods, straightforward code, and many local variables are better left to explain themselves.

There’s also the problem of useless comments that simply repeat what is obvious from the name of the entity they explain. A particular thorn here are the elaborate javadoc comments that some sophisticated editors create automatically and some unsophisticated programmers never fill in. Such comments take up valuable screen real estate and distract the code’s reader without contributing something to the program’s understanding.

Finally, there’s also the (relatively less common) problem of excessive commenting. This tends to cause difficulties, because as we programmers maintain the code we often forget to update the comments (especially when there are many detailed comments and we’re unsure about what we’re doing) and this causes the comments to diverge from reality. So, unless you’re programming in assembly language, don’t feel you’ve got to comment every line of code you write.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer Bortel on February 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin Sullivan on February 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

David ChisnallCocoa Tip of the Day, 1/29/10
By David Chisnall on January 29, 2010 No Comments

Don't ignore old versions of OS X.

See All Related Blogs

Informit Network