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

Web Development

Resource Contention


I'm not entirely sure, and I do hope they will forgive me if I am wrong, but I have a feeling it was one of the great Meyers triplets who started the movement back in favour of freestanding functions. Nathan or Scott in all likelihood; it seems improbable that the dissident brother Bertrand would concern himself with a stylistic nuance in a computer language he affects to despise (though we are sure he loves it really!).

Received a bit of a rocket from the Dr Dobb, no less.

Whoever started it, the word is now out, and the movement grows stronger by the hour. Freestanding functions are the new templates! The humble f() is the multiple inheritance of the 21st century! Curly bracket thinking has come round full circle. The sources of moth-eaten UNIX applications, written in Cold War K&R are, even as you read this, being de-archived from ancient, obsolete tape drive systems and held up as exemplars.

The venerable doctor is now retired, and lives in a home for tottering techies at the edge of town. He frightens away the kids that come to tease by shouting at them in VAX Modula-2.

I've got a spanking brand new PC.

When last week an innocent posted to the legendary Brainlobe of the week newsgroup suggesting a solution to the current puzzle that embodied the principal algorithm in a couple of methods, the hootings of mirth could be heard all the way across the comp.* groups. Member functions are still acceptable as accessors for the instance data; anything more is vulgarity.

However, the habits of a lifetime are not easily dropped, and the eponymous founder still keeps an eye on the old magazine and its webby satellites; dispatching curt, capitalised emails to those who fall below the mark. Mine reads

QUIT FOOLING. WHERE CODE?

It's not beige like yours. It's black and silver. It's sex on a stick.

Stated briefly, the argument goes like this. The methods of a class are inevitably tightly coupled to the format of the class's fields. But the work done by the class need not be. If the only code that can 'see' these fields is contained in the accessor methods, then the ties that bind have been eased. Change the representation of the data as much as you like; you have only to change the code in a few, known places.

It has a 1.7 GHz Pent 4 proc, half a gig of RAM, a bog-off hard disk and a DVD.

Thus encouraged, I thought I'd do a serious, bread-and-butter piece this time on a topic close to most of our hearts: C++ design. Or more properly '++C Design' as those like me, with a responsible attitude to the unnecessary creation of temporaries, prefer to call it.

It can bring up Visual C++ in about five seconds. Delphi's even quicker.

But where to put the non-accessor member functions? If we allow them to retain membership of the class, how do we know that they won't put sticky unauthorised fingers directly into the data? One could make all the data fields private as you should and then inherit the whole class and add member functions to that. But in many shops you more or less need written permission to inherit anything from anything these days, and one could foresee difficulties attempting to smuggle this idea past the 'IS A' thought police.

Yup, it's an 18" screen. Flat panel, naturally. Do they still make the other kind?

Naturally, the next idea was to find a baffling way to do this with generics and a shed load of angled brackets. However when we looked for the template expert he was poring over a 17-line compilation error message, following an unsuccessful attempt to use one of the more exotic STL algorithms. We don't expect him back for a while yet.

The mouse that came with it is black too, but I am not using that. I've got one of those new touchy feely ones. It vibrates.

Another item of housekeeping: it might prove a convenience to make a printout of this page before you start. It may well turn out that you will want to highlight the key sentences in fluorescent green with your Stablo Boss, and write 'So simple -- once it has been pointed out!!!' in the margin.

And it's got a blue LED on its bottom. No balls.

Then somebody said: why not just use standalone functions? Barring friendship, these guys never touch instance data anyway. Lets just move all the manipulation stuff out of the class. It's elegant. It's simple. Best of all, it's something the Java people can't do!

Java people --- come back! You will enjoy this. I promise that I'll put in something about the wretchedness of header files, and the confusion and danger caused by using pointers, just so that you can experience a pleasant frisson of schadenfreude.

The keyboard is black too. It has some extra keys. Press this one and Dell sends round a man to find out what you want. That one puts it into hibernate.

My contention is that there is an important flaw in this solution. It is this: how does one remember the names of these standalone functions? How does one avoid typing them? Elderly programmers such myself have grown to depend on that little box of method names that springs up when you hit the '.' key, whereas many coders born after 1980 don't even realise it is possible to enter a function name by hand.

The action is special. Every time you press a key, an angel in heaven gasps 'Meg Ryan!'

As a matter of course, I will be using all the vogue words. You know, the ones that look as though they will yield up their meanings when stared at in context, but in fact do not. 'Affordance' is a good example. It would be a sensible idea to get hold of the office copy of the Concise Oxford or Webster's and get its engine warmed up before the action starts. The dictionaries won't help with interpretation as these words aren't in them, but discovering this for yourself will help you maintain your confidence.

You can try it if you like. Go on.

The solution is the double pimpl idiom or gobstopper idiom or journey to the centre of the earth. As usual, the outer class is just a skeleton. All its functions defer to functions called on a private implementation pointer, which refers to a forward reference class. This middle class contains the implementations of all the methods. However -- and here is the clever bit -- it in turn defers to another class. It is this central class that actually contains the instance data.

The functions will all be called 'foo()' and 'bar()', the templates 'T', the classes 'A', 'B' and 'C'. At the end, you will be sent away to check out the so-called standard so-called library that your compiler vendor has had the gall to ship to you, and it will turn out to be chocker with this very flaw I have described.

Don't hit the space bar, fool. Try something that doesn't get used all the time, like Num Lock. Not that button; that's hiber


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.