August 2007
Ten Ways to Write Cleaner Code

Experts share their top 10 tips for writing code that compiles more easily, performs optimally, plays nicely and sidesteps known vulnerabilities.

by Mathew Schwartz

What's not to love about well-written code?

Clean code compiles more easily, performs optimally, plays nicely, requires less time to maintain, and helps sidestep the array of known vulnerabilities that can leave gaping security holes in your applications. In addition, "simple code is faster to write, faster to understand when you return to it later, and faster to debug," notes Jeff Vogel, the primary programmer for Spiderweb Software, which develops shareware games.

So how can you write cleaner, squeakier code? Here's 10 tips:

1. Keep It Simple

"Always strive to write the simplest code possible," says Pete Goodliffe, author of Code Craft: The Practice of Writing Excellent Code (No Starch Press, 2006). "That is not to say the most simplistic, stupid code. Just the most elegant, smallest, correct thing that works." Accordingly, minimize both features and flexibility: "Don't add in tons of features if they're not needed. Don't make your code too flexible if it's not required to be."

2. Prize Elegance

What does well-written code look like? "Simple code is elegant, not a tangled web of spaghetti, and not a baroque software masterpiece," says Goodliffe. "It's just code that's easy to understood, easy to spot flaws in, easy to extend and consequently, easy to maintain."

3. Name Wisely

Start by choosing variable names that are long enough to be clear, but not so long as to be unwieldy. "That saves five minutes of searching through the program looking for clues to what the heck 'incremeter_side_blerfm' is supposed to mean," says Vogel. "Don't use variable names that will mock you."

4. Comment Thoroughly

When building your code, "comment like a smart person," in Vogel's words. Without well-written comments, you - or whoever is charged with maintaining the code later on - will waste time trying to pick up where you left off. For each chunk of code, Vogel recommends writing "a description at the beginning and a few signposts inside, explaining the road taken."

5. Kill Unnecessary Code

Don't hoard old code inside comments. "It's quite common to find code that's commented out, but still hanging around," says Lars Marius Garshol, development manager for Oslo-based Ontopia, a developer of mapping solutions. "People seem to do this because they want to have the possibility to bring the code back." Yet excess code leads to bloated applications.

In addition, commented-out code is dangerous: Although the application may evolve, the commented code does not, "so that by the time you find you want it again, it will no longer work," Garshol says. Accordingly, trust in version control: You can always restore deleted code later.

6. Test Constantly

No code is ever perfect as first written, so test yours mercilessly. "Programmers who aren't into this discipline think that it's a load of extra, unnecessary work, but it's not," says Goodliffe. "I cannot stress what a valuable development technique it is. It's one of the central tenets of most agile processes, and it really helps you to maintain a correct code base that can be modified, sometimes quite aggressively, without fear of mucking it up." Well-tested code also debugs much more quickly.

A corollary: Test often, but avoid any optimization until later in the code-writing process. "You almost never know what you need to optimize until you actually take your functioning code and test it with a profiler," says Vogel.

7. Know Your Vulnerabilities

When testing applications, remember that an attacker may use a known vulnerability in a desktop-based application, like Apple QuickTime, to introduce a worm that attacks an online application, such as MySpace, and also steals a user's MySpace credentials. (Just such an attack surfaced late last year.)

With that in mind, coding a secure application requires "rigorously testing source code for any and all vulnerabilities, to ensure the application will not compromise, or allow others to compromise, data privacy and integrity," says Ryan Berg, chief scientist for Ounce Labs, which develops security solutions in Waltham, Mass.

8. Manage Time Pressures

"When a project begins to slip, the developers - and the whole team - tend to take shortcuts. This is human nature," says Gary Pollice, a professor at Worcester Polytechnic Institute and author of Software Development for Small Teams: A RUP-Centric Approach (Addison-Wesley, 2004). For example, "instead of using readable names like 'netPay' for variables, we type 'np' since it's shorter and we save valuable time." Except, of course, that we don't, because such shortcuts compromise clarity, and thus will ultimately slow development, testing and QA.

9. Mind Your Mentors

Writing clean code means knowing how to write code inside of a software factory - working with other developers, reporting to managers and operating on a fixed schedule. In developing these types of skills, never underestimate the power of working with people who've seen it all before. "The kind of stuff I learned quickest when mentored in the 'Real World,' rather than from books or school, was how to actually get useful techie stuff done in the software factory," says Goodliffe. "There's a real difference between knowing how to program in C++, and knowing how to knock out great products in C++ as part of a large software team."

10. Lose the Ego

"The biggest problem that I've come across in the software factory is actually ego," adds Goodliffe. "Sometimes programmers' opinion of themselves, and also their own insecurities, can really get in the way of writing good code."

In short, coding isn't just about knowledge, it's also about teamwork: communication, sharing and actually having fun. "The best programmers I know are humble. They know what they can do well, and they know what they cannot do well," Goodliffe says. "We need more of those kinds of programmers out there."

Keep an Open Mind

Even with these tips in mind, there's no one right way to create great code. "In some ways, clean code - like beauty - is in the eye of the beholder," says Pollice. Regardless, following these will help you create better-performing, easier-to-maintain software applications, and everyone can see the beauty in that.



Mathew Schwartz is a freelance business and technology journalist based in Cambridge, Mass.

Comments on this article? Share your feedback on our discussion forum, Dice Discussions.

*Please note, you must be a registered job seeker in order to submit your question to Dice Discussions.
Search Jobs

Did you know?

Many hiring companies who use Dice search our resume database before posting jobs. That means many of the best jobs are never even posted. Post your resume now, and be sure not to miss any opportunities.
Post Resume Now

More Career Insights

  • Technology Today
  • Cover Letters & Resumes
  • In The Trenches With Dice
  • Local Market Reports
  • Dice Discussions