Sitemap
Syver Enstad

Syver Enstad

Programmer

C Headers are not a valuable thing

3 min readNov 22, 2014

--

It is funny how we are liable to perceive useless stuff we are forced to do as beneficial. My friend Marius and I was talking the other day about how Objective-C programmers he knew insisted that their header files were valuable to them. In my experience this also extends to C and C++ programmers. The argument I often hear focuses on the documentation value of the header file because it defines the public interface for a module or set of modules. I will try to show why I don’t think this is a valid argument but rather seems to be an example of after the fact rationalization of unnecessary work. I’ll also point to ways you can avoid hand-maintaining header files all together.

Get Syver Enstad’s stories in your inbox

Join Medium for free to get updates from this writer.

The following account of C history is entirely fabricated, but it is how I imagine it could have happened: The C header file was introduced because of the primitiveness of the C compiler. The C compiler compiles each source file in complete isolation and then leaves it up to the linker to connect function calls to functions defined in other source files. To be able to perform type checking to ensure that the call to a function was in fact using the same types that the function was defined for, they came up with the concept of function prototype declarations that the compiler could see and check that the call to a function was in accordance with its declaration. But to manually have these declarations in the source file was a too blatant example of duplication for even hardened C programmers to accept. They pulled out their favorite tool, the C preprocessor (cpp) and used its #include directive to textually include another file into the source code that contained the necessary hand maintained declarations for functions that needed to be called outside their defined source file.

But this could be done much easier without the duplication introduced by a separately maintained header file. If we used some simple annotation in the source file to declare that a given function was to be callable from another source file, a compiler or other tool could use that information to export the necessary declarations for users of that code. This is how for example Java does it with its public/private/protected scheme for classes and methods.

And a quick google reveals that such tools do indeed exist: Lazy C++ and Makeheaders are the first results of a quick googling. They seem interesting but I can’t vouch for their suitability as I haven’t tried them, and I doubt if many are using them. This might point to a larger problem in the C community: A seemingly glacial inertia when it comes to improving the current state of tools and practices. The resigned attitude of comments like this one copied from Stack Overflow is perhaps indicative: “Btw — once you’ve got used to it it’s not that bad anymore.. C++ (and any other language as well) has more annoying things.”

--

--