8 ms·
Unfortunate direction. Header files are great and I wish modern languages would adopt them.
by wly_cdgr 3y ago
Unfortunate direction. Header files are great and I wish modern languages would adopt them.
- tgv 3y agoI'm glad other languages solved that in another way. With C(++) headers, you pick the declarations at compile time, and the implementation at link time. That's a disaster waiting to happen, and unpleasant to unravel.
- flohofwoe 3y agoIt's almost impossible to get wrong though, because the same header will also be included in its own implementation. If there's a mismatch between the declaration and implementation you'll get at least compiler warnings.
- tgv 3y agoIt’s been a while, but IIRC there is no problem when you eg. change a struct, as long as the name is the same.
- mathstuf 3y agoIndeed. Mismatching headers and libraries is not all that rare. I've seen headers found from Homebrew, libraries from the macOS SDK, and some related tool from `/usr`. Things…rarely work out well, but you only find out in your test suite (or linker if you're really lucky).
- flohofwoe 3y agoThe only realword problem with headers I've seen so far is when there are multiple versions of the same header in the same project (bad idea anyway), and one isn't extremely careful about search paths. The module equivalent would be different versions of the same module under the same import name. Can the C++ module system actually deal with that?
- mathstuf 3y agoModules don't have search paths unless you toss everything into one directory. The robust way is to instead do `-fmodule-path=modname=path/to/bmi` where ambiguity is non-existent. It is IFNDR to have two modules of the same name in a program (basically just a restatement of the ODR on the module initialization function). Due to the compatibilities, there may be more than one BMI for a single module in a build, but that module can only exist at link or run time once.
- deleted 3y ago[deleted]
- galangalalgol 3y agoDown voting because you disagree with someone isn't what we strive for here is it? Edit: Ok I get it, disagreement is an encouraged reason for downvotes on HN. If a reason for an opinion is given, a refuting response is preferred, but in this case, no reason was given.
- mgaunard 3y agoI don't remember ever seeing another reason I had a comment downvoted.
- tialaramex 3y agoI used to sometimes get downvoted heavily for disagreeing with longtime HN favourite Thomas Ptacek, but that eased off a lot after one time I'm like "LOL, the transcripts for your show are so wrong" and Thomas misread that as me claiming the experts on the show are wrong rather than the transcripts so he was indignant. Initially I got the most downvotes I think I'd ever seen†, and then Thomas realised oops, yeah, he's not saying what I thought he's saying and it shot back up over the course of an hour or so. IIRC the auto-transcripts for example transcribe PKIX (an RFC explaining how we shall use the X509 certificate system for the Internet, and the regime resulting from that RFC) as "Peacocks". Hilarious. But, not a criticism of the person on the show (Ryan Sleevi I believe, who I rather like although we've never met in person). I have written things I don't feel good about, especially trolling groups who believe something I don't, and deserved to be downvoted even if they were strictly true. But those mostly get -1 or -2 or something. Thomas' fans just keep hitting that down arrow apparently. Other than disagreeing with Thomas, the main way I got sizeable negative voting was being wrong and then going to sleep without correcting it. Say 2+2 = 3 on a Thursday evening, but correct it to "1+2 = 3" a quarter hour later? Probably get one or two downvotes. Leave it that way overnight? Wake up to 10 downvotes at least. People will kick you if you don't react. I remember picking an erroneous example in one of the endless Rust v C++ threads, and then going to bed, and yup, very clear I was wrong when I woke up.
- mgaunard 3y ago
- 38 3y agohttps://wikipedia.org/wiki/Poe's_law https://wikipedia.org/wiki/Poe's_law modern languages dont adopt them because they are terrible. a header import just dumps all the declarations into the global scope, so you have no idea where function or type usages comes from.
- jprete 3y agoI consider them much worse than that. Header files are explicitly a copy-paste of the included text file into your source. There’s no rule that they have to only contain declarations, or that they have to appear at the top of source files, or that they have to be .h files. All of those are usage conventions and not required by the language itself. The full capabilities of headers to muck with your code are arbitrarily large.
- Galanwe 3y agoIt's the same for pretty much any language. There is nothing special about header files... > There’s no rule that they have to only contain declarations Same thing in Python, you can write statements at the top level of a module, and it will be executed whenever someone first import that module... > or that they have to appear at the top of source files Same thing in Python, you can `import` anywhere, and the context/order matters and overwrites existing scope declarations. > or that they have to be .h files Same thing in Python, the extension doesn't matter if you use import lib, only if you use the `import` keyword does the name/path matters.
- 38 3y ago> It's the same for pretty much any language. no, its not. just off the top, I can say that D, Go, Nim, Rust and Zig dont do this. also this isn't really an argument. instead of actually disagreeing with the original premise "headers are terrible" (which you cant), you've started a new argument, which is "other languages use headers", which no one was contesting.
- rsaxvc 3y agoThat's the best(weirdest?) part! I used to have a job porting LabView and matlab to C/C++, since headers don't even have to include full statements, so share our test data and coefficients like so: static const float FIR_FILTER={ #include "fir_coefs.csv" };
- jprete 3y agoWhat 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++.
- WalterBright 3y agoI spent a long time implementing precompiled C++ headers. The main problem was converting the pointers in the complex data structures to/from file offsets. I never wanted to go through that again. D has a simple and effective module system. It even treats unmodified .c and .cpp files as modules (though the C and C++ files are unaware of that!). D's builtin C compiler can even import other C files as modules. Both C and C++ should have just copied it.
- flohofwoe 3y agoYou're downvoted for some mysterious reason, but you're completely right, in languages with module support I really miss the compact API overview that is provided by C headers. This is good because the interface is important, and the implementation is not. In modules, the implementation completely dominates, and it's impossible to figure out what the module API looks like from looking at the source. You need an IDE to generate an outline for you, which frankly sucks. For C at least, headers are pretty much perfect because they are supposed to only contain public API declarations. It's just C++ that messed everything up with the inline and template keywords, which required implementation code to move into header files.
- dureuill 3y agoI agree that the compact API overview is a good thing to have, but I loathe that C forces the user to write it by hand, while modern languages just generate it from the documentation comments of the source. See: `cargo doc` that generates nice HTML documentation. See also language servers that provide completion using the same documentation comments. Meanwhile headers, on top of the forced duplication of signatures between header and implementation, and the associated cost, is also implemented on terms of the preprocessor and textual inclusion. This compilation model makes code analysis much harder than necessary, because it becomes hard to know which piece of the preprocessed compile unit comes from which header. This makes it hard to implement e.g. "remove unnecessary import" or "import symbol automatically" functionalities that are the bread and butter of module-based languages. Also impacts automatic refactoring negatively