6 ms·
#includes are a totally broken idea. They are the canonical violation of the DRY (Don't Repeat Yourself) principle. With #includes, whenever you want to make
by lisper 11y ago
#includes are a totally broken idea. They are the canonical violation of the DRY (Don't Repeat Yourself) principle. With #includes, whenever you want to make a change in the signature of a C function you have to do it in two places. The only reason we have them at all is because early C compilers were one-pass compilers, and so they needed to have declarations spoon-fed to them in the right order so that they could run in the 64k or so of RAM that was available on the PDP-8. There is no good reason to have includes today except that the C legacy is so strong and the idea is so deeply entrenched in people's minds and in the structure of C that they are nearly impossible to get rid of.
- aab0 11y agoIsn't the problem here imports in general? Imagine these examples written in a literate Haskell file .lhs: 'Let us discuss graph search... > import Control.Monad'
- akkartik 11y agoI think lisper was raising a tangential but still valid point. Haskell imports don't suffer from the problem he was pointing out; we only see the function type in one place. It's not the fault of Haskell imports that literate programs put them first.
- dllthomas 11y agoNote that in Knuth's formulation of literate programming, you could freely reorder the program source. So those imports needn't be at the start of the literate program - they could be off in a parenthetical appendix.
- feklar 11y agoWhich basically nulls one half of the beef this blog post has re canonizing typesetting over organization.
- akkartik 11y agoHey, give me some credit here. My beef wasn't that CWeb doesn't let you reorder code. I was talking about policy rather than mechanism, that the ability of CWeb wasn't being put to best use.
- feklar 11y agoCan't blame me for misunderstanding when you have clearly written: >These are systems that focus on generating beautifully typeset documentation without allowing the author to arbitrarily order code. As CWEB does allow the author to arbitrarily order code. There's also this statement: >I speculate that nobody has actually read anybody else's literate programs in any sort of detail. Which suggests you yourself have never read any literate programs in detail. Thankfully this statement is provably false as the MMIX Group at MUAS exist? = true so I can merely comment that buggy line out and read the second half of your argument without it being entirely invalidated.
- akkartik 11y ago> "These are systems that focus on generating beautifully typeset documentation without allowing the author to arbitrarily order code." The previous sentence was: "When I look around at the legacy of literate programming, systems to do so-called semi- or quasi-literate programming dominate." Semi- or quasi- being intended to distinguish with classic literate programming as in CWeb. I usually try to go out of my way to assume that if someone misunderstands me it's a failure of my writing, but in this case my post is filled with Knuth's original CWeb programs. It didn't seem realistic that you missed them all, or that you thought I missed that CWeb permits reordering in spite of having read so many CWeb programs. And then you make this new jab about me not having read any literate programs, in spite of me showing repeatedly in the article that I went all the way down the list at http://www-cs-faculty.stanford.edu/~uno/programs.html http://www-cs-faculty.stanford.edu/~uno/programs.html. Are you sure you read my post? --- Thanks for the pointer to http://mmix.cs.hm.edu/local http://mmix.cs.hm.edu/local. Not immediately obvious that it cares about literate programming, but I'll take your word for it. I'd appreciate intros to anybody in that group with experience reading literate programs (email address in profile).
- lisper 11y ago> Isn't the problem here imports in general? That's a very complicated question. In a complex world, you have to do something to establish the context in which a particular piece of code is being evaluated. It's a matter of taste whether that context establishment should come before or after the code for which the context is being established. Personally, I prefer the context establishment to come before, but reasonable people can disagree. This extends even to the language level, where you have some languages (like the C family, and Lisp) where variable bindings always precede their usage, and others like ML and Haskell, where the bindings can come after they are used. But what I was complaining about was specific to #includes, which use a very particular (and badly broken) mechanism to establish context, namely, textual inclusion. It was a horrible hack invented to appease a brain-damaged compiler which could only deal with one file at a time. In 1970 this was an understandable engineering trade-off. In 2016, not so much. The right way to fix it is to get rid of it and replace it with something less brain-damaged rather than layering more hacks on top of it (IMHO).
- tbirdz 11y ago#include repetition is not really that much of a problem these days. Put all your #includes in one include.h file, and include only that include.h in the rest of your code. Then make a precompiled header for include.h (gcc, clang, and even msvc can do this), which will vastly speed up compile times, and compensate for the overhead of including unnecessary headers in your code that doesn't need to include everything.
- jmount 11y agoI think lisper is (rightly) criticizing the repetition of the same code being multiple places due to the includes (not the also irritating need to write the includes multiple places). The problem does go from bad to worse in C and C++ when you work through what happens when static decelerations and method bodies make it into ".h" files. A lot of rules are in place to have these multiple code units appear as a coherent single unit.
- lisper 11y agoExactly right.
- MaulingMonkey 11y ago> Put all your #includes in one include.h file RIP incremental build times. > Then make a precompiled header for include.h (gcc, clang, and even msvc can do this), which will vastly speed up compile times, and compensate for the overhead of including unnecessary headers in your code that doesn't need to include everything. Speaking from experience, god no it doesn't. Those are the codebases where fixing a single typo may take an hour to fully rebuild a single build configuration. As the poor bastard charged with porting things and managing said build configurations, this means I either break the build from time to time, or have to wait overnight to have a full set of builds to test before checking anything in.
- signa11 11y ago> ...vastly speed up compile times, ... Just use ccache if build times are getting atrocious, then focus on getting link times down :)
- srtjstjsj 11y agohttp://programmers.stackexchange.com/questions/142192/why-would-a-programmer-want-to-separate-implementation-from-interface http://programmers.stackexchange.com/questions/142192/why-wo...
- thaumasiotes 11y agoThis doesn't address the objection, though. If you change a function signature in library.c, you have to make the same change again in library.h, because they are coupled. The concept of signature-only header files is just making your code more brittle.