Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits and drawbacks of using header files? [closed]

I had some experience on programming languages like Java, C#, Scala as well as some lower level programming language like C, C++, Objective - C.

My observation is that low level languages try separate out header files and implementation files while other higher level programming language never separate it out. Those languages use some identifiers like public, private, protected to try to do the jobs of header files. C++ also have both identifiers and header files as well

I saw one benefit of using header file (in some book like Code Complete), they talk about that using header files, people can never look at our implementation file and it helps with encapsulation.

A drawback is that it creates too many files for me. Sometimes, it looks like verbose.

It is just my thought and I don't know if there are any other benefits and drawbacks that people ever see and work with header file

This question may not relate directly to programming but I think that if I can understand better about programming to interface, design software.

like image 858
vodkhang Avatar asked May 09 '10 15:05

vodkhang


4 Answers

They allow you to distribute the API of a library so the compiler can compile correct code.

As in C, rather than including the whole implementation, you just include the definition of what is in the library when linked.

In this sense, the benefits are mainly for the compiler. Hence you installing a binary library into say /lib and headers into your include search path. You are saying, at runtime, expect these symbols with this calling convention to be available.

When they are not required by the compiler/linker/interpreter then the convention for that language is the best way to do it because that's what other programmers expect to find. Conventional is expected.

Languages such as C# include the ability to inspect libraries for information from the binary blob, hence in many of these languages you don't require headers. Tools such as Cecil for C# also allow you to inspect a library yourself (and even modify it).

In short, some languages remove the benefits of headers and allow a library to be inspected for all the compile-time information required to ensure linking code meets the same interface/api specs.

like image 170
Aiden Bell Avatar answered Oct 08 '22 05:10

Aiden Bell


I'm not sure exactly what question you are asking, so I will try to rephrase it:

What is the benefit of putting public information in a separate (header or interface) file, as opposed to simply marking information as public or private wherever it appears?

The main benefit of having a separate interface or header file is that it reduces the cognitive load on the reader. If you are a trying to understand a large system, you can tackle one implementation file at a time, and you need to read only the interfaces of the other implementations/classes/modules it depends on. This is a major benefit, and languages that do not require separate interface files (such as Java) or cannot even express interfaces in separate files (such as Haskell) often provide tools such as Doxygen or Haddock so that a separate interface, for people to read, is generated from the implementation.

I strongly prefer languages like Standard ML, Objective Caml, and Modula-2/3, where there is a separate interface file available for scrutiny. Having separate header files in C is also good, but not quite as good because in general, the header files cannot be checked independently by the compiler. (C++ header files are less good because they allow private information, such as private fields or the implementations of inline methods, to leak out into the header files, and so the public information becomes diluted.)

It's folklore in the language-design world that for typical statically typed languages, only about 10% of the information in a module is public (measured by lines of code). By putting this information in a separate header file, you reduce the reader's workload by roughly a factor of ten.

like image 29
Norman Ramsey Avatar answered Oct 08 '22 04:10

Norman Ramsey


They use some identifiers like public, private, protected to do the jobs of header files.

I think you're wrong there: C++ for instance still has public private and protected, but it's common to split the implementation from the interface with a header file (although that doesn't go for function-templates).

I think it's in general a good idea to seperate interface from implementation when you're creating libraries, since you then never expose the inner workings of anything to the client and thus the client can never deliberately make code that depends on the implemenation. The choice if you want to split it is up to you. I must admit that for my own code (small programs I write for myself), I don't use it often.

like image 25
Nick Avatar answered Oct 08 '22 05:10

Nick


In context of c The header file implementation brings lots of readability in our program and it becomes easy to understand. If it is the way to write our code systematically and header file brings abstraction,standardization and loose coupling between our main function file(.c) and other (.c) files which we are using.

like image 43
BODY Avatar answered Oct 08 '22 04:10

BODY