views:

167

answers:

3

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.

+5  A: 

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.

Aiden Bell
In contrast, languages like Java and C# expose library APIs with metadata in the library itself.
Martinho Fernandes
Thanks for your answer.I still had another related question : in C/C++, when you install headers into your include search path, other programmers can never see your implementation detail. That is because you have header files and people can only look into them. However, in C#, when I try to read in the built-in library: I can only see public methods. How can they do that without header files? And in Java, if I try to look into built-in library, I can see all of them.:)
vodkhang
@vodkhang - This is the semantics of the language's packaging system. Look at the languages inspection routines and you may uncover more.
Aiden Bell
@vodkhang: try using Reflector: http://www.red-gate.com/products/reflector/. You can see everything you want.
Martinho Fernandes
@Aiden Bell, @Marrinho Fernandes : thanks for both, I will look at them:)
vodkhang
+2  A: 

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.

Nick
Yeah, that was my wrong. I will fix it now:)
vodkhang
+1 for a really good description of why interface and implementation should be loosely coupled.
sal
+2  A: 

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.

Norman Ramsey
So, it is a really good practice that in a big system, we always try to separate out the interface and the implementation. But I just wonder if it is good, why languages like Java, C# does not force developers at the beginning to follow that good practice?
vodkhang
@vodkhang: I've never understood why people are so eager to mix interface and implementation. Even really great designers like Niklaus Wirth and Barbara Liskov have done it.
Norman Ramsey