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

C/C++

In Defense of Laziness


Incremental Development

That wasn't a particularly popular position. It drew some rather condescending comments from people who think that the goal of programming is perfection, and who are willing to spend inordinate amounts of time debating how best to achieve it. If you write code for a living, you can't afford to do that. You have to decide what's important and what isn't, and focus on what's important. Don't try to do everything. Only do the things that matter. Be lazy [2].

One of the best aids to laziness that I've come across is incremental development. Instead of trying to grasp the full complexity of a project, start with a simple part. Begin by writing test code for it, then implement it. Run the tests. And as you add complexity, keep running the tests. As you learn more about the intricacies of your project, you'll probably have to go back and rewrite the code several times. Running the tests helps assure you that you haven't broken anything. Or, if you're less fortunate, it helps you find the things you broke.

Incremental development means not writing more code than you have to. For example, one of the biggest problems that intermediate programmers run into is deciphering the error messages that compilers give them when they write template code [3]. The best way to write template code is to not do it until you have to. That way you don't spend time parsing obscure error messages before you know what you're trying to do. Get the code working with one simple type, so you know that you've got the fundamentals down. Then, if necessary, turn it into a template. Separating development of the computation itself from its expression as a template simplifies your job. Especially if it turns out that you don't need a template, after all.

Don't give in to the temptation to write templates. I know, templates are exciting. They're the hot new technology, and you're nobody if you're not writing templates. But the next time you're tempted to write a template, ask yourself how you're going to use it in your current project. If you're only going to instantiate it for one type, don't make it a template. Resist the temptation to generalize. You can do that later, if you need to. One type plus a suspicion that you might use it with another type later should not turn your code into a template [4]. Be lazy.


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.