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


Pete is a consultant specializing in library design and implementation. He has been a member of the C++ Standards Committee since its inception, and is Project Editor for the C++ Standard. He is writing a book on the newly approved Technical Report on C++ Library Extensions to be published by Addison-Wesley. Pete can be contacted at [email protected].


Overloading has been a theme in my life for the past year. Those of you who remember my first column for Dr. Dobb's, back in the April issue, have heard it before: Writing a book, moving from Massachusetts to Indiana, buying a house, selling a house, getting married, changing job relationships, going to Standards Committee meetings, editing the draft C++ Standard, writing this column. But I think the worst is nearly over. A month ago I was in Berlin for a C++ Standards Committee meeting; the next one is now a long five months away. Two weeks ago I sold my house. Yesterday I sent the penultimate draft of the book off to the publisher [1]. Later today I leave for Boston—the wedding is the day after tomorrow. How did I get through it all, you ask. It was easy: I'm lazy.

The fundamental rule of laziness is: Don't do it unless you have to. But that's not a justification for your teenagers refusing to clean up their rooms until you yell at them. There's a more principled meaning to "have to" in that rule—it means setting priorities with an eye toward long-term goals. One of the hardest things to learn, in programming and in life, is that some things are less important than others. Do the things that are most important, and let the things that are less important go. Be lazy.

There was a discussion recently on comp.std.c++ about the best way to write code that loops through the contents of a vector using an index. The natural way to write that loop is:

for (int i = 0; i < vect.size(); ++i)
  // do whatever you need to do with i

But vect.size() might be a little slow, and if you change to a different kind of container, it might not even be constant time. So some people do this:


const int limit = vect.size();
for (int i = 0; i < limit; ++i)
  // do whatever you need to do with i


But that makes limit visible to the rest of the code, and someone later on might misuse it. So try this:

{
const int limit = vect.size();
for (int i = 0; i < limit; ++i)
  // do whatever you need to do with i
}

Now limit goes away at the end of the block, and there's no need to worry about excessive scope.

There were several other variants, all dealing with equally unimportant issues. For a vector, size() is trivial. And it's an inline function. Just write the code and get on with the important part, which is making the application work.


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.