Tuesday, 16 June 2009

C++: #include <rules>

My blood is boiling. I'm seething. I'm going to go mad. Is that an overstatement? Possibly. But it's not far off...

You see, you can get so used to doing things the Right Way that when you stumble across someone doing it the Wrong Way it comes as quite a shock. And a frustration.

Lately I've been working on a C++ project with appalling include file discipline. It's embarrassingly bad. There is a well-known gentleman's agreement over include files. A #include <etiquette> if you like. Doesn't everyone know about this?

(Of course, many programmers will cite the fact that C and C++ require such "good practice", rather than ENFORCE it, as a weakness of the language. Perhaps they are correct, but that's a different story.)

The most basic of the #include <rules> are:

1. A header file must be self-contained and complete.

#include-ing it CANNOT produce a build error.

It does not require you to include more files first. Anything it references is either forward declared (if possible) or #included in that file.

If this is not the case, the user has to jump through innumerable hoops to work out exactly where the undefined bits of code live, and so what other files must be included first. Naturally, when those files do not include cleanly the task recurses painfully.

It's annoying. It's wrong. It makes extra work, reduces the code's self-understandability, and opens the door for errors (for example: the wrong files may be included; and it is possible to write header files that behave differently when different sets of include files are brought in beforehand).

2. Include files internally prevent problems from multiple inclusion

The canonical format for a header file is:

// any required #includes go here

// header file contents go here

That "unique" name should be well chosen, and usually based on the name of the include file in question. It should also include the name of the project, and possibly the name of the subsystem, too, in order to avoid conflicts with header files you mnight be importing from a any third party libraries.

Many compilers provide #pragma once as a helpful way to write the same thing. However, this is NOT a standard C or C++ feature. It's cute, but it means that your code is not portable. Does this matter to you? It really ought to. The best advice is to used #pragma once AND the standard include guards together.

If you do not do this, then multiple includes of a header file will almost certainly generate build errors as the compiler sees re-definitions of the same code constructs.

Of course, some (very few, comparatively) headers are designed for multiple inclusion (e.g. by defining types based on some preset #define value that you establish prior to the #include). You CAN still define include guards for these headers using a little preprocessor ## macro string gluing.

Objective-C provides an interesting alternative to #include guards or #pragma once: the #import directive. This states at the include site that if the file has already been included, do not include it again. Otherwise go ahead and include it now.

It's cute, but it is just plain wrong. The calling site is NOT the place to specify if a file should be included one time only. This is a part of the contract provided and required by the include file, and so should be stated and enforced there. Also, import and include can be freely interchanged on the same files. The user should not be able to break the contract by accidentally including rather than importing the header.

Precompiled header files are another source of weirdness that I don't have the time to moan about properly here. They bring their own set of potential misuses.

3. Define things in ONE FILE ONLY

Do not have multiple #include files with different declarations of the same name.

This is a violation of the ODR (One Definition Rule). If the files define specific variations that are needed for different build configurations or targets, then still put them all in the same file. DO NOT leave it up to the #include-r to work out which of the files to use themselves. Any silly #ifdef determination logic should be INTERNAL to the header file, not external at the include-site.

It's obvious what will happen if you ignore this rule. Someone will accidentally include the wrong header version somewhere, and cause errors. Probably subtle, odd, and hard to track down runtime errors, too.

4. Differentiate "public" interface header files from implementation files

Many projects contain subcomponents with a small number of "public" header files defining their interface, and many internal .h files for implementation classes.

Differentiate these files.

This should be done by placing them in very different file locations. This will prevent a subcomponent interface user from accidentally including an internal header and using it as if it's part of the public interface.

There are more good practice rules than those. But these are the most basic and important ones.

The project I've been working on breaks these rules all over the place and it makes working with the code really, really complicated. Yes, I'm moaning. But we should know better than this by now. And we really should have developed sharper standard tools for this by now.

The greater the size of the project, the harder these problems are to fix. I'd love to dive in and sort the whole thing out in the current project, but I fear it will take a very, very long time. It's particularly hard as it's a large codebase that builds on several platforms with a number of compilers.

Tuesday, 9 June 2009

Tech toys: Synergy

I have four machines on the desk in front of me, each in active daily use. The upshot? A tangle of keyboards and mice on the work surface, and no space for my arms.

But I found the solution. Perhaps I'm a bit late to join the party, but it's here: Synergy.

Synergy is a wonderful application that shares one keyboard and mouse between many machines. It's as if you were using a multi-head display. But with many bodies, too. Synergy is relatively easy to install, although by default requires a little text file configuration. Nothing frightening for a techie, but a casual user might need a deep breath and a good run up.

Performance is really good (it does slow down when one machine is heavily CPU loaded or performs a lot of network access, but this is relatively rare). The application works on Linux, Mac OS, and Windows. I run the server on Linux, with two mac clients and a Windows client. The code hasn't been updated in about two years, but it seems every bit as good as it must have been two years ago!

And now? I have one keyboard. One mouse. Lots of free desk space. And deep joy.


There are several Mac OS GUI front ends that might be useful floating around the ether. I've not used any of them, so the many or many not be any good...