Friday, January 9, 2009

C

C

C combines all the power of assembly language with all the ease of use of assembly language.

-- unknown

I'd just like to take this moment to point out that C has all the expressive power of two dixie cups and a string.

-- Jamie Zawinski, in the source code for xkeycaps

There is a point in your life when you realize that you have written enough destructors, and have spent enough time tracking down a memory leak, and you have spend enough time tracking down memory corruption, and you have spent enough time using low-level insecure functions, and you have implemented way too many linked lists.

-- Miguel de Icaza

Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."

-- Philip Greenspun

Your superior intellect is no match for our puny weapons.

-- unknown, via Aaron Stern

I'll be blunt: C is a horrible language for developing large projects in. C contains almost all of the bad language features described above and almost none of the good ones. Furthermore, some of the good features it does have (like static type checking) are so corrupted by type casting that they are much less useful than they would otherwise be. C also offers essentially no abstraction capabilities whatsoever except for arrays (which are really just pointers in disguise) and structs (ditto).

Does that mean that C is a useless language? Far from it. It has its place, and its place is a vital one. C is an excellent language for writing code that has to interface directly with the machine ("bare-metal" programming). It is also a good language for implementing better languages in ;-) The Ocaml runtime engine, for instance, is written in C (with some help from assembly language). C is useful for writing the 1% of your application that absolutely, positively, has to run as fast as possible. However, if you're trying to write a large application entirely in C you are in for a miserable time. Instead, you're better off picking a good language that has a C foreign-function interface (FFI), so you can "drop down" into C when you really need to (which hopefully won't be that often).

Some people (particularly novice programmers who have no idea what they're talking about) are under the illusion that because C offers such direct control over the machine, it's the only "real" programming language for "true hackers". If that were true, all "true hackers" (whatever that means) would be writing code in assembly language, which is much closer to the machine than C is. I hope some of the quotes above, some of which are from world-famous open-source hackers, will help to dispel this myth. If not, then I recommend that you try to write a really large program (> 100,000 lines of code) in C, and tell me what you think at the end of that experience. I think you'll find my arguments much more persuasive.

No comments:

Post a Comment