views:

593

answers:

9

I don't quite understand the point of having a header; it seems to violate the DRY principle! All the information in a header is (can be) contained in the implementation.

A: 

And what if you want to give somebody else the declarations to use your library without giving them the implementation?

As another answer points out - the original reason for headers was to make the parse/compile easier on platforms with very simple and limited tools. It was a great step forward to have a machine with 2 floppies so you could have the compiler on one and your code on the other - made things a lot easier.

Martin Beckett
I could imagine a compiler that would have an option to take your source files and generate declaration files automatically... no need to maintain them yourself.
emddudley
Yeah you could do that, but you'd need a way to tell it what should be exported and what should not. Writing you own headers allows you to be very specific.
dmckee
+20  A: 

It simplifies the compilation process. When you want to compile units independently, you need something to describe the parts that will be linked to without having to import the entirety of all the other files.

It also allows for code hiding. One can distribute a header to allow others to use the functionality without having to distribute the implementation.

Finally, it can encourage the separation of interface from implementation.

They are not the only way to solve these problems, but 30 years ago they were a good one. We probably wouldn't use header files for a language today, but they weren't invented in 2009.

Steve Rowe
But if you don't distribute the implementation then how does your calls work? it magically reaches out across the internet to call your copy?
RCIX
You ship compiled library code + headers.
timdev
Then how does things like .NET or such allow API exploration of compiled DLLs?
RCIX
You'll need to translate the headers, but I think that deserves an entire question in itself.
Dave Van den Eynde
@RCIX, they don't. Not in all cases. DLL's can export certain functions by name and this can be queried. Some DLL's contain COM interfaces implementing IDispatch which includes type library information and this can be queried. Raw C/C++ functions that are not in some way exported, however, cannot be (to my knowledge) explored easily.
Steve Rowe
Headers unfortunately don't separate interface from implementation (your class still has to list private member variables in the header). But your first paragraph is right on spot. That's why headers were invented. It wasn't convenience, it was necessity.
jalf
@jalf: see the pimpl idiom
Bill
@jaif: The languages people started doing this in weren't object oriented. Header to a perfectly adequate job of separating interface and implementation for procedural languages, and they allow you to hide your data structures, too, if you really want to, just access them though an opaque pointer.
dmckee
+3  A: 

It helps to think a bit about the capabilities of the computers that were available when, say c, was written. Main memory was measured in kilowords, and not necessarily very many of them. Disks were bigger, but not much. Serrious storage meant reel-to-reel tapes, mounted by hand, by grumpy operators, who really wanted you to go away so they could play hunt the wumpus. A 1 MIPS machine was screaming fast. And with all these limitation you had to share it. Possibly with a score of other users.

Anything that reduced the space or time complexity of compilation was a big win. And headers do both.

dmckee
"Grumpy operators" did not play hunt the wumpus. We were lost in a maze of twisty passages, all alike.
themis
+4  A: 

The architects of many modern languages such as Java, Eiffel and C# clearly agree with you -- those languages extract the metadata about a module from the implementation. However, per se, the concept of headers doesn't preclude that -- it would obviously be a simple task for a compiler to extract a .h file while compiling a .c, for example, just like the compilers for those other languages do implicitly. The fact that typical current C compilers do not do it is not a language design issue -- it's an implementation issue; apparently there's no demand by users for such a feature, so no compiler vendor bothers implementing it.

As a language design choice, having separate .h files (in a human-readable and editable text format) gives you the best of both worlds: you can start separately compiling client code based on a module implementation that doesn't yet exist, if you wish, by writing the .h file by hand; or you (assuming by absurd a compiler implementation that supplies it;-) can get the .h file automatically from the implementation as a side effect of compiling it.

If C, C++, &c, keep thriving (apparently they're still doing fine today;-), and demand like yours for not manually writing headers grows, eventually compiler writers will have to supply the "header generation" option, and the "best of both worlds" won't stay theoretical!-)

Alex Martelli
How is it the best of both worlds to have to maintain the same information in two files? The best of both worlds would be to have something like header files automatically and implicitly generated for the programmer's convenience. Headers are a primitive hack requiring a ton of extra effort, slowing down compilation (in todays codebases on todays CPUs), and complicating your code by forcing you to think about inclusion and declaration order to avoid cyclic references.And compilers can't just automatically define the .h file. It would alter the semantics of your code in some cases.
jalf
@jaif, as I said, nothing in the C standard _forbids_ compilers from having a flag to optionally generate .h from .c at compilation (including all externally visible entities and those only): if you think otherwise, show me where that's forbidden. If compilers don't DO it in practice, it has to be for lack of demand -- i.e., users of C or C++ clearly don't think the lack of this feature is a big deal, otherwise they'd pressure compiler suppliers to offer it!
Alex Martelli
+1  A: 

No you dont have headers in Java -- but you do have interfaces and I every serious Java guru recommends you define anything used by other projects/systems as an interface and an implementation.

Lets see a java interface definition contains call signatures, type definitions and contants.

MOST C header files contain call signatures, type definitions and constants.

So for all pratical purposes C/C++ header files are just interface definitions and should thus be considered a Good Thing. Now I know its possible to define a myriad other things in header files as well (MARCROs, constants etc. etc. ) but that just part of the whole wonderful world of C:-

int function target () {
    // Default for shoot
    return FOOT;
}
James Anderson
You might not have noticed it, but Java doesn't require you to #include an interface definition before you're able to use it. Headers do. Does that make them a Good Thing?
jalf
Yes, but interfaces have a specific purpose: to ensure that you can access *some* properties and call *some methods on a class of objects without actually knowing what that object specifically is. Headers exist solely as a repetition of the contents of a single (Objective-)C(++) file, only in condensed form (at least as anything ive seen).
RCIX
+1  A: 

For Detail Read this

A header file commonly contains forward declarations of classes, subroutines, variables, and other identifiers. Programmers who wish to declare standardized identifiers in more than one source file can place such identifiers in a single header file, which other code can then include whenever the header contents are required.

The C standard library and C++ standard library traditionally declare their standard functions in header files.

sat
Good point i suppose.
RCIX
+2  A: 

The whole idea of inspecting the binary output files of language processors would have been hard to comprehend when C invented .h files. There was a system called JOVIAL that did something like it, but it was exotic and confined more-or-less exclusively to military projects. (I've never seen a JOVIAL program, I've only heard about it.)

So when C came out the usual design pattern for modularity was "no checks whatsoever". There might be a restriction that .text symbols could only link to .text and .data to .data, but that was it. That is, the compilers of the day typically processed one source file at a time and then linkers put them together without the slightest level of error checking other than, if you were lucky, "I'm a function symbol" vs "I'm a data symbol".

So the idea of actually having the compiler understand the thing you were calling was somewhat new.

Even today, if you make a totally bogus header, no one catches you in most AOT compilers. Clever things like CLR languages and Java actually do encode things in the class files.

So yes, in the long run, we probably won't have header files.

DigitalRoss
+2  A: 

Don't forget the documentation a header provides. There is usually anything in it you need to know for using the module. I for my part don't want to scan through a looong sourcecode to learn what there is that I need to use and how to call it... You would extract this information anyway, which effectively results in -- a header file. No longer an issue with modern IDEs, of course, but working with some old C code I really love to have hand-crafted header files that include comments about the usage and about pre- and postconditions.

Keeping source, header and additional documentation in sync still is another can of worms...

Secure
A: 

When you divide code in header and source files you divide declaration and definition. When you look in header files you can see what you have and if you wand to see implementation details you go to source file.

Davit Siradeghyan