3 ms·
You know what I've learned over the years? While many people are very bad about writing C, I'm pretty good at it. So C is not a bad language; there are just a
by jrockway 15y ago
You know what I've learned over the years? While many people are very bad about writing C, I'm pretty good at it. So C is not a bad language; there are just a lot of bad programmers around using it.
(And if you're wondering how to write good C? Make object ownership explicit. Never have malloc and free more than a page of code apart. Don't expose data structures as API. Make errors put your routine into a defined state that the caller can understand. Use bstrings instead of cstrings.
And although I don't write much C++, the more I learn about it, the more I believe it's possible to write good C++. Most people won't spend the time to write good software, and when you do that in C or C++ it's an absolute disaster. But if you go slowly, plan, think, exercise care, and review your work regularly, you can get good code that runs fast.
You can write a safe Haskell application in 10x less time than you can write a safe C++ application, though.)
- tptacek 15y agoIf defects are at best constant over number of lines of code, then just the extra situps C makes you do are inevitably going to create reliability issues. But we all know that it's not that simple, and that some languages are more defect-prone than others; that leaves you to make the case that C is more resilient than most high level languages. Amusingly (just in this context), when Joe Damato makes fun of Ruby reliability, what he's actually making fun of is the terrible C code MRI is built out of. It's interesting to look at how Apple has dealt with this problem. iOS developers write a dialect of C --- in fact, a dialect that is nominally less safe than C++. But idiom in iOS keeps most iOS programs away from unsafe code patterns. You can try to tokenize a string in an iPhone program by taking it's char* (even that is a pain to get because of character encoding) and then strsepping it, but the whole rest of the programming environment works against you when you do.
- kev009 15y agoC is a language for people that care about programming. Like, you treat it as a lifestyle, not a career or side activity. You read books on it cover to cover. You continually seek to simplify and make your code more clear. If that describes you, it's a damn fine tool.
- kmm 15y agoVery true. I think most hate for C is really misplaced or for completely the wrong reasons. I like to think I'm a good C programmer, and I use most of the techniques you describe. But lately I've been wondering whether I'm not actually coding object-oriented code in an inefficient manner. Do you try to somewhat avoid falling into an OOP pitfall, or do you embrace OOP and if so, what reasons do you have to use C over Java or C++?
- bmastenbrook 15y agoHail, god amongst men. Once upon a time I thought I was a very good C programmer. Today I know myself to be a terrible programmer. I surround myself with safety mechanisms such as -Wall -Wextra -Werror and the clang analyzer, but even these tools cannot stop me from shooting myself in the foot time and time again. Please enlighten me, such that I may learn to write safe code in an unsafe language infallibly.
- to3m 15y agoEach time you put a bug in, make sure to take it out afterwards. Nobody cares how many bugs you put in, only how many are left :)
- bmastenbrook 15y agoOh! It's so easy. I can't believe I didn't think to do that before. ... but every time I do find a bug, I am surprised to find it. Thus it stands to reason there are bugs I have not yet found. How do I find all of them?
- jrockway 15y agoCare. I recommend "Programming Pearls" as a good introduction to this concept.