8 ms·
My not-so-humble-opinion is that if you find yourself doing metaprogramming with the C preprocessor, it's time to upgrade to a more powerful language.
by WalterBright 9y ago
My not-so-humble-opinion is that if you find yourself doing metaprogramming with the C preprocessor, it's time to upgrade to a more powerful language.
- jstimpfle 9y agoMy take: rethink the problem and the solution. While there are perfect applications of the preprocessor (such as pasting or stringifying tokens) - yes at some point (rather sooner than later) it gets unmaintainable. But I don't share the sentiment that C wasn't powerful. It has everything one needs (and, due to memory control and cpp, arguably more than most languages). These days I mostly use C, and occasionally resort to python as an accompanying scripting language to generate data definitions (either in plain text formats or as C structures).
- vardump 9y agoSometimes you just don't have other options than C. Like kernel drivers, embedded (microcontroller) development. Or perhaps you just need to develop a small loadable library without any runtime (no malloc, etc.). Or perhaps writing Python, Lua etc. C-module for better performance. You just need to treat C with proper respect, not be too arrogant about your skills. I hope over time we can move more and more to Rust or some other language which gives as many errors at compile time as possible and helps with not shooting at our feet. Shouldn't have too many C-macros, of course. Some macro messes I've seen can be nearly impossible to debug...
- pcwalton 9y ago> You just need to treat C with proper respect, not be too arrogant about your skills. We've been hearing "C isn't the problem, bad programmers are the problem" for 40 years. This has continued to perpetuate the problems with the language. I mean, I certainly agree that there are no alternatives to C now for certain domains, but there are real problems with it that aren't solved by just "treating it with proper respect".
- vardump 9y agoHaving programmed C for soon 30 years, I absolutely agree with you that C is the problem. But often you just need to get the job done, and have limited options available. Perhaps in 50-100 years, people looking back at software from this era won't be able to understand how we managed to make it work at all. I haven't met or know anyone mastering C/C++ (especially C++), and doubt I'll ever hear about one.
- jstimpfle 9y ago> I haven't met or know anyone mastering C/C++ (especially C++), and doubt I'll ever hear about one. C can definitively be mastered. C at it's core is pragmatic minimalism. The basic ideas are very simple. If you want to be a language lawyer and know the standard completely (including all historical accidents), it can get complex, but still manageable. But anyways, you shouldn't be a language lawyer. That's not C. Make maintainable programs instead, stay in the middle of the road. C gets out of the programmer's way. It's not about mastering C, but about mastering programming. And while I agree there's much bad C software out there: I haven't seen one maintainable non-bloated Java project. Here is one master of C that I'm sure you know: Linus Torvalds. Of course there are many, many more. Just remember it's not about C, but programming machines in general.
- vardump 9y ago> C can definitively be mastered. If that's true, how come there are almost no widely used network facing C programs that didn't have plenty of security vulnerabilities? (Anything written by Daniel J. Bernstein doesn't count, because there's no way he's a mere mortal. :))
- shaklee3 9y agoWhich language has? Keep in mind market share, so don't say Go.
- littlestymaar 9y agoEven djb had flaws in his C code despite being a semi-god. It took a $10k bounty and several years to be found but it was there !
- nickpsecurity 9y ago"Sometimes you just don't have other options than C." You always have other options if better languages can compile to C or integrate it via FFI. There's been plenty to do that, too. For simple and safe, Modula-2 was early one compiled to C that couldve had syntax modified to be more like it. If aiming for power, PreScheme was a systems dialect of Scheme that compiled to C at one point. The OcaPIC (Ocaml) project and use of ATS language for 8-bit show stuff like that could probably be made to wofk in areas C is used in, too. So, I'd say it's a myth you need to develop in C just because a target has only C libraries or compilers that safer languages can use. Instead, we get a new claim that people just didnt do it for reasons that varied per project or person but wasn't technical capability.
- kurtisc 9y agoFor the microcontrollers example, the 'C' that the device's compiler uses could be a very loose implementation of a subset of the language. How do you ensure that the code you're compiling to will work for it?
- nickpsecurity 9y agoThat's a good point. I doubt most languages generating C put a lot of thought into what subset is supported in proprietary compilers. If anyone tries one, then there could be profiles done for unusual compilers where the generated code has certain properties. Far as the past, I think people couldve tested the output just to see what happened. Then, either ditch high-level language per fuction, module, or project depending on how far incompatibility effects went. How does that sound?
- pjmlp 9y agoDepends on the microcontroler. The majority of them also enjoy Pascal and Basic compilers. https://www.mikroe.com/ https://www.mikroe.com/ So sometimes, there is indeed an option. Also many languages have compilers that are able to generate C code. As to ensuring, well that is what testing is for.
- flavio81 9y ago>Sometimes you just don't have other options than C. Like kernel drivers, embedded (microcontroller) development Use Common Lisp to generate C code. There are three libs that are specific for this, see C-mera for example.
- hedora 9y agoWait. If I use non-tail recursion on a micro controller, then I’ve probably implemented a stack out of memory crash, which, for embedded devices, is actually worse than a buffer overflow on unsanitized input. Is this a joke, or do they do some sort of stack-bounding static analysis?
- sedachv 9y agoWhat are you talking about? Running out of stack has nothing to do with what you use for code generation/preprocessing. I also don't see why running out of stack would ever be worse than a buffer overflow. The former is at least going to be fail-fast.
- pjmlp 9y agoThat is how the .NET GC was originally implemented.
- audunw 9y ago> Sometimes you just don't have other options than C. This is true. With all the new and fancy languages around, it's strange that there are so few attempts at making a better C. I would say that the characteristics of C is: dead simple, manual memory management, "fancy assembly". Rust is close, but fails on being dead simple. The best attempt I know of, is Zig: http://ziglang.org/ http://ziglang.org/ But it's still very early in development. I am using it for microcontroller code right now though, and it's much nicer than C already, despite having to deal with bugs.
- quicknir 9y agoI keep hearing this. How exactly common is it that you can't use C++ for technical, as opposed to human, reasons? gcc is by far the most talked about compiler in this space (at least from what I hear) and obviously it's a C++ compiler as well. My guess is that in most (not all) cases C++ is usable instead of C, but the culture of such development is to prefer C. It's a real pity, because for someone with a moderate amount of judgement (i.e. not going off the deep end on unnecessarily complicated C++), it only takes a moderate amount of C++ knowledge to be able to write more maintainable and correct code that performs equally well.
- Koshkin 9y agoWhat happens in reality though is that as soon as developers get access to the stanard library all hell breaks loose. People switch to the Java mode of thinking and start almost mindlessly using vectors, maps, shared pointers, etc. and C-level performance goes out of the window. With their exposure to templates, there goes their sanity and, as a consequence, code maintainability.
- mort96 9y agoI don't think I agree with your point in the general case, but even if you were 100% correct, and nobody should do metaprogramming with the C preprocessor in regular production code, I still don't think that's very relevant. The specific use case I had for all of these features were a unit testing library, which is very different from a regular program. If you need a testing library for C, that's probably because you have C code. You probably shouldn't try to write a test suite in Python for your C library.
- nickpsecurity 9y agoWhy not? The majority of engineering professions say use the right tool for the job. Whatever is most cost-effective. Programming is one of few things where people try to do the opposite. Fortunately, many in the profession do right tool for the job philosophy at least for whole domains like OS code, web apps, and so on. Still an insistence on sticking to same language too much. Python has incredible productivity and versatility with ease of learning. It's readable. It's easier to prototype things like verification tools in it than C. There's also tools like Hypothesis to test that. Then, there's tools to speed up the code like Cython. If anything, Python would be a safer, faster way to write tooling for modifying or testing C code than C itself. One could also convert Python generated tests to a C library of tests with automatic validation of data types, coverage, etc. So, I think it would make sense if someone did anything from prototyping C in Python to using it to test C code. And do note Ive enjoyed reading your experimentation with macros in C. I learned about defer through it. Just commenting on that one point.
- Const-me 9y ago> Why not? TLDR: because you need to test the real thing, and using Python to test C code causes too many important differences between tests and production. To test C code you need to compile it, and there’s no C compiler inside Python. Also if you have C++, sometimes you can abuse templates for similar effects, and Python doesn’t have C++ AST either. C toolchains largely depend on environment: included & linked files, environment variables, compiler & linker switches, they all make a huge difference. You’ll spend much time replicating that in Python, and it’ll break when you’ll upgrade any part of toolchain (compiler/IDE/build automation/etc). To a lesser extent, same applies to native binaries as well. If you’ll manage to use Python to produce tests, C compiler to build them, then Python to run them — the runtime environment will be different from the real one.
- pcwalton 9y agoAgreed. A sad amount of the history of Unix has consisted of gradually migrating ill-advised solutions made with text-pasting preprocessors to more sensible designs. See the Bourne shell (written in Bournegol), autoconf (written in m4), troff, sysvinit, etc.
- stochastic_monk 9y agoExactly. I like C++ because it lets me abstract over the absolute fastest C-like code. For example, to produce generic SIMD-accelerated code over both Intel-specific SIMD operations and the Sleef vector math library I needed for FHT-based JL-transforms and kernel projections, I used template specialization to abstract to operations on physical memory. [0] The generic methods themselves were gross, but in application, it was extremely simple in downstream code to specify an operation and a container and have the compiler pick the fastest method for the job. I’ve also checked my generated assembly and the smaller functions are always inlined, even though they’re called from static constexpr function pointers. This is also a great use case for macros, as they saved me huge amounts of typing. I’m sure many will say C++ is the wrong direction to go for metaprogramming, but it’s worked great for my purposes and lets me get straight to the hardware. [0]: https://github.com/dnbaker/frp/blob/master/include/frp/vec.h https://github.com/dnbaker/frp/blob/master/include/frp/vec.h
- stochastic_monk 9y agoAs a heads-up, in case anyone checks, I’ve moved this to its own repository so I can use it in other projects easily: https://github.com/dnbaker/vec https://github.com/dnbaker/vec.
- nialv7 9y agoWhich more powerful language do you suggest? Macro metaprogramming is still needed in C++. Other powerful languages might lack some nice properties of C. Like easy interfacing with other languages, and a wide availability.
- jacobush 9y agoFor instance by inventing one. :) Was this how you got started?
- faragon 9y agoWell, if you use metaprogramming in e.g. 5% of the C code base, it would be OK, in my opinion, as C has lots of benefits vs other high level languages.
- xtrapolate 9y agoI have been working on Snow, a unit testing library for C. OP requires a unit testing library for C, so they've written one. What is the alternative solution you propose? The framework itself may appear complex, but the entire end goal here is to provide simple/abstract tools to write tests in a readable/self-documenting manner. The framework itself is the mean, not the end goal.