40 points · by lucasr · 15y ago · 57 comments
(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.)
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.
... 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?
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++?
If that describes you, it's a damn fine tool.
People that make this kind of complaint were using C for the wrong type of software. If you want to write a web application in C you just need to reevaluate your tool set.
The author points out that C is gradually being superseded by languages like Python. The author is right. Every year, less new C is being written, and for good reason.
You point out that there are obviously environments (like your life support valve driver, as Julio Capote says) where C is going to be the most appropriate choice for years to come.
We can be reasonable people and accept that there's truth to both of these sentiments, or we can synthesize a phony controversy in which we compete to defend extremes and get nowhere.
If we're reasonable, I think the author's point is well taken. Less C/C++ is written every year. In part that's due to the web and the increasing power of (what we used to think of as) "embedded" environments. But in part, it's because people who would have reached for C as their go-to language for some cases are (properly) going to stop doing that.
As someone who spends most of his working days looking at other people's projects for flaws like the stuff this guy is alluding to, particularly for Python and Ruby projects --- when you want to find something particularly fun and gruesome, you go straight for the C extensions.
These devices will only continue to grow, so more C will be written not less. More Python, Ruby etc will be written too, because more software will be written in general. The proliferation of software isn't a zero sum game and probably won't be for a very long time.
Some people out there actually know how to use C, and instead of battling with async JS do get a 5000hits/s hello world, they code a webapp that can easily handle hundreds of thousands of requests/sec without a blink.
Granted, it's less and less common.
Now, is C the best possible tool for these jobs? Certainly not. But it's the one we have and it's got a pretty fantastic track record.
The heart valve code inherits constraints that make C a lot safer: it has an extraordinarily limited feature set, its functional interfaces are simple, it changes rarely, and the time - to - market pressures it faces are dwarfed by other factors like certification and manufacturing.
Consumer software is richly functional, has complex interfaces, changes constantly, and is written under ridiculous scheduling pressure. In that environment, C does indeed give you software that is as likely to enable someone to install a trojan on your system as it is to properly render an image.
For "mainstream" (read: enterprise) software, yes, C may fall out of favor, but it's a great language and here to stay.
There are just too many cases where C is the right tool for the right job and it would be silly to use a different language.
I'm thinking particularly of embedded software. While languages like Lua are great for embedded scripting, they still need something to script against.
So, C isn't going anywhere and I expect that 100 years from now it will still be a worthwhile language to program in.
(Also, C is very much the Latin of modern programming. Learning it helps your understanding of so many other things. In case you're curious, Lisp is Greek and I mean that affectionately).
What a depressing thought.
I love writing C code (the model of programming where virtually anything is possible by modifying and reinterpreting integers is often a fun one), and it's my best language, but I hope we grow out of it sooner than later.
Of course it's possible to write a lot of things in, say, Python, then rewrite the slow bits in C. The number of programs that absolutely must be written close to the metal is pretty small in absolute terms.
I often wonder what a more radical evolution of C might look like. What will "ISO C[20]99" look like? gcc has many clever compiler warnings, but a safer superset/subset language that is mostly source compatible might be valuable.
Until that changes, C is here to stay.
Unless its something like Factor, where most of the implementation is written in Factor.
Even if you stretch to find a class of flaw that only affects a language like Perl (for instance, the ease with which Perl allows you to write code where metacharacters will allow an attacker to stuff commands into a shell), you simply have to go back 10 years or so to see the 8lgm posts that did the same thing to C programs all over the Internet.
http://webcache.googleusercontent.com/search?q=cache%3Ahttp%...
Plain text copy: http://c-migration-plan.pen.io
"This one, you really can't tell."
A competent C programmer can absolutely discern what is happening... more importantly, though, is that the two examples don't do the same thing.
I stopped reading at that point.