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

Conversations: So Who's the Portable Coder? [1]


September 2000 C++ Experts Forum/Conversations


"Hi, buster." Jeannine came over and sat down across from me in the mess. "Hear any more rumors?"

I glanced at the time just to make sure, but I wasn't due to go on for another half hour. "Hi, busted. No, not yet. They're keeping a lid on it, whatever it is. You're off watch early?"

"Brzenkowski couldn't sleep, so he relieved me. I owe him one."

"Thoughtful of him," I agreed around another mouthful of lunch. "I really like him. He always does good work, makes sure it won't affect other people, understands how he fits into the big picture. I wish we could keep working with him after planetfall next month. Everyone should be that professional."

"Most people are," Jeannine smiled. "You're no slouch yourself."

"Actually," I chuckled, "it reminds me of Bob. You know, I've told you about him before, the other weird from my first job. Man, sometimes he'd do things that'd just make you fry a lobe. It got to the point where if someone did something dumb we'd say he 'pulled a Bob.' "

I proceeded to explain…


A voice beside me said: "Hey, Junior, you broke the build."

I sighed silently and closed my eyes. I'd been on the job nearly two months and already decided I hated the Guru's calling me "my apprentice" less than anything this rotten programmer, Bob, might call me -- especially "Junior." I counted to ten, then answered without turning: "What are you talking about, Bob?"

"Your latest code checkin," Bob responded amiably, and sipped at his latté. "My compiler barfs on it. It complains that an iterator is already defined. You broke the build, man. Better fix it before Her Holiness finds out." I winced; as inexperienced as I was, I knew that breaking the build was A Bad Thing. I could just imagine the Guru's reaction: Remember, my young apprentice, the second commandment of team membership: Thou shalt not break the build.

"Okay, fine," I sighed again, audibly this time. "What part of the code are you talking about?"

Bob leaned over, hit a few keys, and pulled up a function that looked something like this:

// OBJMAP is a typedef for a standard container
void f( OBJMAP &theMap )
{
    for( OBJMAP::iterator iter = theMap.begin();
         iter != theMap.end();
         ++iter ) {
        // do something
   }
   // other code in here
   for( OBJMAP::iterator iter = theMap.begin();
        iter != theMap.end();
        ++iter ) {
       // do something else
   }
}

"What's wrong with that?" I asked. "I checked it out in Stroustrup, and the iterator should go out of scope at the end of each loop [2]."

"Yeah, well, not with my compiler." Bob replied, picking at his teeth. "It complains that iter is already defined. Easy fix, though… just get rid of the second declaration of iter. In fact, that was how my code originally read when I checked it in. Then you went and broke the build."

Then I understood. I had to be diplomatic because I was still on probation, though. "Bob," I said reasonably, "this has to compile on all the platforms we're targeting. I put the second declaration in because my compiler complained about the second iterator being undefined, and that's how it's supposed to work, according to Stroustrup."

Bob seemed a little put off. "I don't care about your newfangled compiler. I've been using mine for years and it's never let me down. Your code doesn't work."

"Bob. The code's right. It compiles fine for me."

"So you got a buggy compiler. Not my fault, I'm just doing my work, I don't care about other compilers," Bob shrugged again, more defensively.

A quiet voice behind us said: "I sense much fear in you."

I jumped a little; Bob jumped more, sloshing his latté. The Guru was far too good at gliding up silently behind us, a tome in one hand as she always had.

The Guru shook her greying head slowly, frowning. "Fear leads to anger," she continued.

"Aw, cut it out. I'm not afraid of other compilers!" Bob said hotly, sucking the spilled latté from his wrist. "I don't care about them at all."

"Anger leads to hate. Hate… leads to suffering."

"I'm suffering, all right," I muttered under my breath. "I'm just trying to write portable code. — All right, Bob," I capitulated, anxious to be rid of him, "I'll fix it so it works. Okay?"

"Like I said, Junior, just get rid of the second declaration." He glared once more, then turned and left.

Wendy gophered up from the next cubicle, watching him go. "So what's Bahb complaining about this time?" she asked us. While the Guru stood nearby, having resumed reading in her tome, I showed Wendy the code and explained what Bob wanted to do. "Typical," Wendy snorted. "Give him a choice between implementing a portable solution, and a solution geared towards his compiler, he'll unerringly choose the latter. His code is not portable, just like you said. Y'know, in this case, you don't really need to declare a new iterator for each loop anyway. Just declare it once, before the first loop, and it'll work on both compilers." She made some quick changes to the code:

// OBJMAP is a typedef for a standard container
void f(OBJMAP &theMap)
{
    OBJMAP::iterator iter;
    for( iter = theMap.begin();
         iter != theMap.end();
         ++iter ) {
        // do something
    }
    // other code in here
    for( iter = theMap.begin();
         iter != theMap.end();
         ++iter ) {
    // do something else
    }
}

The Guru looked up from her reading and glanced at the solution. "Very wise, daughter." Wendy raised an eyebrow, but only a little; she was used to it. "And, as you implied, that is not the only solution. Another elegant one might be to avoid the iterator declarations entirely, and perhaps improve readability, by using for_each and expression templates, where possible [3]. My apprentice," the Guru continued to me, "while it is important to know, understand, and obey the Holy Standard, it is also important to know your tools, even those that are Deviant. Bob is using a compiler that does not obey the scope of a variable declared in a for-init-statement. He cares not about portable code, as long as his own compiles."

"Hmm…" I said, rubbing my chin. "Writing portable code is harder than I thought. So, we have to wait for a new release of Bob's compiler before we can take advantage of the code I originally had?"

The Guru looked off in the distance for a moment. "I am not a prophet, I cannot see the future. However, this I do foresee: Portability issues will never truly disappear. Compilers will not all become Standards-conforming at the same rate. Some vendors may even leave outdated behavior in their compilers, in order not to break legacy code.

"Also, some compilers will introduce new features to extend the Standard. In and of itself, this is good, else would the language become stale, inflexible, and eventually die stagnant. Programs destined for only one platform may benefit from the new features. Using those non-Standard features in portable code, however, would be disastrous -- and some novitiates, such as yourself, may inadvertently use them, without being aware of their non-portability. Do not come to love one compiler so much as to fear others. Fear is the path to the unportable."

She paused a moment, then turned and walked off. As she left, we could hear her soft voice saying, "I wish I could remember the name of the wise one who said, 'The difference between theory and practice is greater in practice than it is in theory … .' "


"Bob just wasn't a big-picture fellow," I finished.

"Nobody had better pull a Bob on Ganymede," Jeannine agreed. "It's not quite that forgiving a place, just yet. Hey, you'd better go. Anderson's waiting."

I looked at the time and stood up. "Duty calls. What are you and Laura doing after third watch?" I asked, since I was discovering that there were, of course, more pleasant things to talk about than Bob, or even the Guru … .

Notes

[1] To the tune of "For He's a Jolly Good Fellow."

[2] Bjarne Stroustrup. The C++ Programming Language, 3rd Edition (Addison-Wesley, 1997).

[3] See the feature article on expression templates in the May 2000 issue of C++ Report.

Jim Hyslop is a senior software designer at Leitch Technology International Inc. He can be reached at [email protected].

Herb Sutter is chief technology officer of PeerDirect Inc. and secretary of the ISO/ANSI C++ standards committee. He can be reached at [email protected].


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.