5 ms·
Having written a conforming C compiler, at one point I knew everything there was to know about C (I forget details now and then, or confusing them with C++ and
by WalterBright 7y ago
Having written a conforming C compiler, at one point I knew everything there was to know about C (I forget details now and then, or confusing them with C++ and D).
But knowing every engineering detail is not the same thing as knowing how to program in C effectively. It's like being the engineer who designs a Grand Prix car. It does not mean you can drive it faster around the track than anyone else. Not even close.
For example, the C preprocessor is surprisingly complicated. I had to scrap it and rewrite it completely 3 times. If you try to make use of all those oddities, my advice is don't waste your time. Over time I removed all the C preprocessor tricks from my own code and just wrote ordinary C in its place. Much better.
- rofo1 7y agoStrong agreement here. You can limit yourself by choice to certain areas of the language that you know inside out (you know the asm they produce, etc.) and use it to solve problems. Don't worry about every single corner case. As you said, you can easily forget those things, especially if it's not your day-to-day job. The same idea and principle can be found in "JavaScript - The Good Parts".
- mhh__ 7y agoHaving recently been doing some web development, I'd argue that JavaScript's design (even today, despite some modern additions) is so conducive to undebuggable spaghetti code (i.e. this ) that there really are no good parts. Sometimes the technology is objectively the wrong choice i.e. compiling javascript to native code (Not a JIT).
- WalterBright 7y agoSome languages do fight your attempts to write good code every step of the way :-) No, I won't name names.
- mhh__ 7y agoI'm currently writing a library that loads, parses and includes c headers at compile time, in D. It's not finished but let's just say I've managed to make yours and Andrei's thoughtful design into a monster
- ogoffart 7y agoAnd that's why people claiming that C++ is more complicated than C because it has an even bigger specification miss the point. What counts is how easy to use in practice. You can get along just fine in C++ without knowing the exact aliasing rules from C or or how to specialize a template. What matters is that the extra features of C++ makes actual programming simpler, not harder. (for example destructor (RAII), standard library, classes, ...)
- 0815test 7y ago> You can get along just fine in C++ without knowing the exact aliasing rules from C Nope, you can't. These are exactly the things that introduce undefined behavior (i.e. total breakage) if you aren't very careful about what you're doing at all times. Don't take my word for it, check out what the C++ designers themselves state about the issue in the C++ Core Guidelines. C/C++ is far from simple, and thinking that you can just make things up as you go along is a serious mistake.
- ogoffart 7y agoYou are absolutely right, but my point is that when you do modern C++, in the application code, it is very unlikely that you need to use reinterpret_cast in your code, and therefore you don't need to know all the subtlety about it. So despite C++ being more complex than C, if you limit yourself to some practical subset, it is actually easier than C.
- seba_dos1 7y agoSame can be (and has been) said about JavaScript. Or C. Or any language with any kind of issues. And it doesn't work in practice.
- Leherenn 7y agoI think it does sometimes? At least for enterprise software. We use both C and C++ embedded as well as C++/Qt for the desktop control system, and we have no issue keeping to a sane subset of C++. There are clearly defined rules and code review does the remaining enforcement. And it's not even hard or time consuming as everyone is pretty much aligned and every small issue can be easily resolved with a quick chat.
- pcwalton 7y agoI couldn't agree more. After spending many years working with LLVM, which is at its heart a C compiler, and understanding why it has to do the sometimes-terrifying things it has to do to get C to run well, I've become very paranoid when writing C or C++. My C/C++ code is as boring as possible. (In fact I try to avoid writing C or C++ whenever possible these days; undefined behavior in the language is too pernicious and unfixable without breaking compatibility. I think both languages are approaching obsolescence.)
- dvdbloc 7y agoWhat would we write drivers in then if C was obsoleted?
- cperciva 7y agoOver time I removed all the C preprocessor tricks from my own code and just wrote ordinary C in its place. Much better. I don't get it. If my macros produce standards-compliant code and they make my code easier to read and understand, why shouldn't I use them? My goal as a developer is to write clean performant bug-free code... not to make life easy for compiler developers.
- WalterBright 7y ago> If my macros produce standards-compliant code and they make my code easier to read and understand, why shouldn't I use them? The problem is they don't make code easier to read and understand. Worse, the unhygienic nature of C macros makes it hard to contain them. I haven't seen your code, so I'm speaking based on what I've seen of mine and others' code. If you dial it back, the person who has to deal with your code after you leave will appreciate it. More generally speaking, if you're doing metaprogramming with the C macro system, you've outgrown the language and should consider a more powerful one.
- deleted 7y ago[deleted]
- icedchai 7y agoI once worked at a place that had a platform specific "DEBUG" log macro. It worked something like: DEBUG(msg); Except, it was defined in such a way that you actually had to have two closing parens, like DEBUG(msg)); It looked syntactically invalid, but whatever the macro did required it. The entire code base was littered with WTFs like that...
- WalterBright 7y agoI hated debug macros. There just was never a clean way to write them. I was determined that D would not suffer from that problem. `debug` is a keyword in D, and you can do things like: debug printf("I got here\n"); and the printf only gets compiled in when compiling with -debug. (Any statement can be used after the printf.) Even better, semantic checks for debug statements are relaxed - for example, purity is not checked for them. Meaning you can embed debug printf's in functions marked 'pure', instead of having to use a monad.
- mobilemidget 7y agoHow ('yeah im going to write this') do you start such project like a c compiler? Seems like a huge thing to write. What did you write first? Did you already have a lot of compiler knowledge so you had a good idea/structure/flow etc in your mind already? And how long did it take you? tia
- mhh__ 7y agoA compliant C compiler is probably a year's work in a modern language give or take?
- WalterBright 7y agoIt takes 3-5 months just to write a compliant preprocessor. Writing a basic code generator is a year, writing a basic optimizer is another year. But if you want a competitive compiler, better pencil out 10 years.
- WalterBright 7y agoI'd written a couple compiler like toys before, but nothing like a real compiler. I just started writing it. Took a couple years.
- hyeomans 7y agoSomewhat related, what would you recommend for someone who has never programmed in C?
- m463 7y ago> at one point I knew everything there was to know about C after taking the test, wouldn't this be: at one point I knew everything there was to know about (my implementation of) C also: one time long ago I tried to use the c-preprocessor to preprocess a data file. ha ha ha ha ha. (conclusion: don't do that)