Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Developing Your Code Sense


April 2002: Developing Your Code Sense

"Jeez, this code stinks!" Ever find yourself saying that? Glenn Vanderburg, a principal with Delphi Consultants, and Dave Thomas, a partner in the Pragmatic Programmers consulting firm, think you should pay attention when your senses try to tell you something about code. Their Monday tutorial, "Developing Your Code Sense," encouraged programmers to pay attention to all their senses, not just sight. "It's a metaphor," warned Thomas. "And we're going to stretch it."

The Nose Knows
Source code can have a smell, they explained—sometimes a subtle fragrance; sometimes a stench. The sense of smell is associated with instinct, with primitive and subconscious levels of the mind, and the idea of code having a "smell" maps nicely onto the preverbal idea that something odd is going on. Attendees came up with their own examples of smelly code. One described a porting project wherein he ran across an odd construct—a case statement within a For loop. He followed his sense of smell, examining why that particular idiom had appeared, and found that the whole program was fundamentally, fatally flawed: "I pulled on that thread, and everything just kind of unraveled."

Our other senses can be equally—if metaphorically—valuable, Vanderburg and Thomas said. For example, code can have a sense of balance (blank looks from the audience). "We cannot be wrong because we MADE IT UP!" Thomas thundered.(grins from the audience). "It's very hard to describe; everybody has it, it's just a matter of listening to it." He went on to explain that programmers can usefully apply the idea of balance to avoid stumbling into morasses of ill-thought-out implementation. "If you're writing tons of code for a conceptually simple task, stop!—and think about it from the beginning. Don't settle for complexity when simplicity is possible. One of the most important things for programmer productivity is to slow programmers down."

Work Hard, Make Easy
"Once we have a design, many of us are reluctant to change it," Vanderburg chimed in. "Fight back! Work hard to make things easy."

The idea of balance can be used to assess a project's design, they said. "Imagine models are physical," said Vanderburg. "There will be a small number of classes that turn out to be the system's center of gravity," he claimed, illustrating with a class diagram drawn from the Apache Tomcat servlet engine. "Is that center related to the program's true function? If not, the program's out of balance." Going further, he said, you can think of a software system as having a static center of gravity (the center of a class or collaboration diagram), and a dynamic balance point (the class or subsystem that sees the most changes). "Balance enables change," noted Thomas. "Even huge things can be agile if they're in balance."

Talking and Listening
Then there's hearing: "Code talks, but do we listen to it?" asked Thomas, admitting that "This is getting a bit weird." But the class was with him. "Some write code as if its only purpose was to explain to the computer what it has to do," complained one attendee. "But equally important, it has to communicate with people—the author, maintenance programmers, all the other who will see it down the line."

"That's great!" exclaimed Vanderburg. "Don't bother reminding yourself that your goal is to get working software. What you should remind yourself of is that your goal is to communicate with people."

Reading your source code aloud, or explaining it to someone else, has concrete and documented value, whether in a formal setting of code inspections, peer reviews or just chatting with your cube neighbor, said Thomas. "Have you ever done this? You stare at a piece of code for an hour, debug and debug, and finally, in desperation, you pull your friend in from his cubicle. You start to explain the code and how it works, but then …" (smacking his forehead) "Oh, that's it!"

The audience laughed again, and most of them were nodding ruefully. In fact, Thomas said, you can even resort to "rubber-ducking." "I don't know why it works, but it works. You put the rubber duck on top of your monitor. Then, when you're stuck on something, you start explaining what the code does—aloud—to the duck." And before you've finished the explanation, the solution comes to you. You smack your forehead, and politely thank the duck, he said to general laughter.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.