6 ms·
What is great about header files?
by jprete 3y ago
What is great about header files?
- mgaunard 3y agoThey're straightforward and easy to reason about. Textual inclusion also allows a number of things modules do not. The main disadvantage that they have, that they're stateful, is also true of module systems in many languages (e.g. python).
- dagmx 3y agoI greatly disagree with “easier to reason about” Maybe in a clean, smaller codebase with minimal dependencies. But the textual inclusion means that including one header can suddenly break previously compiling code. Unless that header is completely inclusive, that can mean a while of chasing down transitive headers till you find the problematic one.
- mgaunard 3y agoThe semantics are literally "it's the same as copy-pasting it where it's used". It doesn't get simpler than this. There are much more things going on in the module system that you'd have to know about if you care about what's actually going on.
- dagmx 3y agoThe module system doesn’t have side effects on inclusion, whereas headers do. So again, maybe if you have a smaller, cleaner or dependency free codebase, then it’s not an issue. But in any codebase of major scale or legacy, headers can introduce several issues that aren’t possible with the module interface.
- mgaunard 3y agoThe side-effects are merely the rules of C++, that you already know by virtue of writing C++.
- dagmx 3y agoI feel like you’re talking past any of the caveats I’ve mentioned twice. I have no interest in belabouring the point with someone who’s clearly ignoring what’s being written. Have a good day.
- maccard 3y agoYou're really burying the lede here. Headers are literal copy paste, and are entirely different. Anyone who has ever had to define WIN32_LEAN_AND_MEAN knows that there's _way_ more going on here.
- dagmx 3y agoThis exactly, thank you. Sometimes you can’t untangle the defines in another header and they infect your own. Modules are a nice barrier to guard against side effects.
- mathstuf 3y agoYes, the number of times I've had a `LoadLibrary` function name conflict with `windows.h`'s `A`/`W` suffix-switching or had to deal with X11's `#define Status int` "typedef" mucking with completely unrelated code is…sad.
- mgaunard 3y agoJust don't include those in headers.
- maccard 3y agoThat's not possible. Windows.h is a requirement for anyone working on Windows. Various standard library implementations make use of macros to implement debug-only assertions, or bounds checks in containers.
- mgaunard 3y agoDon't include it in headers = only include it in a .cpp file.
- mathstuf 3y agoBecause `void*` to pass `HANDLE` around is better how? Maybe if you're able to hermetically seal off platform-specific bits, but usually you'll have two parts that want to talk about Windows API types across their call boundaries…
- lelanthran 3y ago> What is great about header files? One great thing - it's nice to have an interface[1] which already has tooling to let other languages use your private, hidden and compiled implementation. If those C++ libraries that almost all of Python scientific work is built on had used modules[2] (or something similar) instead of headers, interfacing Python to those libraries would have been an immense effort. Even with modules existing now, if you're writing something core and foundational in C++, it's not getting adopted like Numpy, BLAS, etc unless you also provide header files. And that's just Python. Think of all the other languages that rely on performance critical code written in C++, and used with a C-only interface. [1] Of course, your interface can't be a C++ interface either, so there's that. [2] If modules were around at the time.
- mardifoufs 3y agoThat's interesting! Why would modules make it harder to interface with c++ from python ? Is it because the header files are a direct interface to the functions? How hard would it be to do the same from modules, would it just require tooling to support modules rather than directly provided header files? (I just realized that I don't completely understand how exactly the new modules work, so I have some further reading to do! )
- lelanthran 3y ago> Why would modules make it harder to interface with c++ from python ? Is it because the header files are a direct interface to the functions? Well, they're simpler, they're text, and the tooling already exists (I use swig all the time to automatically make C functions and symbols available to Java and Python. Some Lisps as well). The reason the tooling exists, is because they are simple, and they are text. Looking at the C++ module definition, it's unclear to me[1] that similar tooling for C++ modules will ever be possible. As long as the most popular languages (Python, Java, etc) rely on a C header file for inter-call interoperability, header files aren't going away. No one wants to manually write the interface into a C++ library by hand. [1] I'm no C++ expert though - the problems I see with modules includes things like lack of a standard to grab the interface from the compiled module, complexity of C++ to determine the interface from the actual source code, etc.
- hgs3 3y agoSeparation of interface and implementation. In languages without them you must rely on documentation generators or an IDE to explore a library/packages public interface. Header files provide this separation without the need for fancy tools.
- humanrebar 3y agoPython added pyi files in after the fact. Adoption on those is much slower than I would like.
- dureuill 3y agowhy would be using a "fancy tool" a bad thing? Headers are to be maintained manually, which is lost productivity and invite errors. The "fancy tools" can provide specialized search, formatting of the documentation, internal links, formatted and tested code examples, and of course the interface for free. In modern languages they're even part of the included tools. I'm having a hard time seeing the downside of the "fancy tools".
- flohofwoe 3y agoYou immediately see the public interface of a library without the implementation getting in the way (for C headers at least, typical C++ headers are an unreadable mess because they mix interface declarations with inline/template implementation code).