8 ms·
C, The Beautiful Language
- angusgr 16y agoThis is a nicely written tribute, but there are two generalisations that I feel are worth pointing out. Many parts of Linux kernel are really, really nice C code. Sadly, in my experience most C code is not especially nice. I don't know how this extends to the football metaphor, but certainly I wish most of the C code I have encountered (not to mention written) in my career to date was one tenth as neat. On balance, I'd say there are other languages (Erlang, Python) where I've personally found the median quality, and level of expression, to be substantially higher (for the random subset of code I've read and my own standards, clearly YMMV.) The other generalisation is "other languages". Which "other languages" are they? What other projects (despite the Linux kernel) are they reading, and in what contexts? You can find beautiful or precise code is almost any language if you try hard enough. You can find ugly or obfusticated code in nearly every language without barely trying at all.
- paulsmith 16y agoWhat are some specific examples of beautiful C code in your view? Asking for a friend.
- dxq 16y agoAfter finding the documentation and guides for writing nginx modules woefully inadequate, I did a lot of poking around in the nginx internals to better understand how things worked. The code is very readable, follows good style guidelines, and has its fair share of brilliant workings. I found that the source code served as much better documentation than a lot of _actual_ documentation that I've read.
- cpeterso 16y agoYou beat me to it! I was just about to suggest the nginx internals. :)
- dchest 16y agoPlan 9 code is beautiful http://plan9.bell-labs.com/sources/plan9/sys/src/ http://plan9.bell-labs.com/sources/plan9/sys/src/ Although there are a few extensions in its C compiler http://plan9.bell-labs.com/sys/doc/compiler.html http://plan9.bell-labs.com/sys/doc/compiler.html
- uxp 16y agoI like poking around FreeBSD's code, specifically some of the drivers and userland. I find a lot of code that rpaulo commits to be particularly clean and understandable. Some of rdivacky's work on boot2 and clangbsd is also inspiring to me. http://svn.freebsd.org/base/user/rpaulo/ http://svn.freebsd.org/base/user/rpaulo/
- burgerbrain 16y agoThe Busybox project.
- angusgr 16y agoI'm glad. Somewhere on my mental "code I'd like to read list" is the busybox code, because I don't understand how it does so much with so little. If it does it and looks good, I'm doubly impressed. :)
- silentbicycle 16y agoLua, SQLite, the OpenBSD/FreeBSD userland utilities.
- Turing_Machine 16y agoAs the old joke has it, you can write FORTRAN in any language.
- ssp 16y agoC is more a description of a Harvard machine than a von Neumann machine. In fact, a useful new language feature would be dynamic generation of machine code.
- cdellin 16y agoOf course this isn't a feature of the language -- but most implementations (e.g. gcc) technically allow this, if you really, really know what you're doing. For example: http://www.cesarbs.org/blog/2010/07/19/run-time-machine-code-generation-and-execution/ http://www.cesarbs.org/blog/2010/07/19/run-time-machine-code...
- ssp 16y agoYes, you can generate machine code yourself, but then you have to write machine code ... I'm suggesting a JIT compiler as a built-in language feature so that you could write code in a high-level language and have machine code generated that was specialized based on the program's dynamic state. It could be done as a library, but it would benefit from language integration so that you wouldn't have to write the code as strings or ASTs.
- defrost 16y agoLanguage integration would be nice, however dynamic code generation from a library has been about for some time now: "With libtcc, you can use TCC as a backend for dynamic code generation." from http://bellard.org/tcc/ http://bellard.org/tcc/
- barrkel 16y agoWriting code that writes code, where that written code is executable at machine speeds rather than interpreted, is a qualitative difference over a simple "eval" implementation; and it can most definitely be a feature of the language. A language which supports it as a feature may do things like partial evaluation of closures, inlining of runtime-constructed code, transforming e.g. the typical block-passing idiom of a Ruby into specific optimized cases. Being able to rely on such transformations in turn lets you build more abstract (i.e. more highly parameterized) libraries because you know you won't have to pay the same degree of abstraction tax.
- hacker-gene 16y agoNice!
- Enlightenment 16y agoAgree! Far better than the cluster-f*ck that C++ has become.
- Groxx 16y ago>In other languages, the abstractions and sweet (if helpful) syntactic sugar that attend the code conceal the heartbeat. IMO, that means they're failed abstractions and syntactic sugar. Proper ones should result in better understanding due to simplification - you can learn it in levels, easily encapsulating the knowledge at each level so you know you can "trust" what it's doing without having to remember all of it.
- rbanffy 16y agoAs such, any abstraction or syntactic sugar that distances you from the flow of the program fail. Bu that's for programs that are procedural. Functional programming has no flow. Truth neither moves nor changes. To use an analogy, a procedural program is like music, where rhythm and motion of the instrument are the essence of what you are representing. Functional programming is more like sculpture, where you create an elaborate piece that has no rhythm or movement of itself, but guides the flow and transformation of the data around it.
- Groxx 16y agoI haven't done functional programming really, I didn't think of that case. Interesting that it still seems to apply, though. If your abstractions in functional programs obscure that flow, they still strike me as a failure. Though I'd be interested in if functional programmers would disagree, I obviously don't know functional design patterns.
- silentbicycle 16y agoIf you reason about functional code with appropriate assumptions: they don't lie. A big part of functional programming is making code easier to reason about in isolation. If you want good procedural C code, read Lua. That shit is polished.
- rtaycher 16y agoI always thought that part of the reason that c sometime fosters a feeling that "syntatic sugar" is because it doesn't haven nearly as much ability to abstract things as other languages. Although this can be useful to some people in that things may be less context sensitive and more straightforward.
- TillE 16y ago> we watch Ronaldo doing things on the pitch Hmmm... Lionel Messi does crazy things you never thought were possible. He's Lisp. Gareth Bale has few fancy tricks, but does what he does very well, and often relies on pure speed - C. Joey Barton is good, but has a bad reputation and a sordid history - Fortran? Etc :-)
- smcl 16y agoI wonder which Ronaldo he meant. I can't quite get over the moody, cheating side of Cristiano Ronaldo to appreciate his abilities. Having a Cristiano Ronaldo as a developer would be a bloody nightmare. It'd be like having a co-worker who turned in some inspired code, but who lied, stole, picked fights and was generally a chore to be around. I hope he meant Brazil's Ronaldo circa 2002 :)
- pprov 16y agoOf course I meant Brazil's Ronaldo...what kind of sports fan do you think I am? :)
- trustfundbaby 16y ago... A Real Madrid fan?
- smcl 16y agoAha the author. I enjoyed your article, but I might be biased as I love football and I quite like C!
- trustfundbaby 16y agoYou know your soccer my friend ... but you left out Java. I'd say that's like Miroslav Klose ... big, strong, not particularly fast or lovely to watch but damned good at putting away goals when it matters. surprisingly so.
- rahoulb 16y ago
- zyb09 16y agoIn my experience it is more 'fun' to write C code then to lay down yet another line of boiler-plate high level code. However, it seems to take longer to get things done and the resulting code is also much more error-prone (at least my code that is).
- dkarl 16y agoAll it means is that he understands C better than other languages. Even Java (yes, Java) has these rhythms that he speaks of. You look at code and you feel the logic, even the necessity of it. That's just the feeling of being in tune with code and with the person who created it, thanks to a shared understanding of the language. Master any language and you'll get the same feeling when you read well-written code. Actually, though, the less boilerplate a language forces you to write, the less you'll feel that "inevitability," since much of the "inevitable" structure of the code -- the predictable patterns in the design of code, or, one might say, "design patterns" -- is code you shouldn't have to write in the first place.
- sid0 16y agoExactly, and C is the worst offender for boilerplate code. There isn't even a list abstraction! It's surprising that there's an array abstraction and that people aren't just forced to use pointer arithmetic.
- geoka9 16y agoThis is not fair. C was (and still is) intended for situations where writing your own list abstractions is the right thing to do. Unlike lists, the array abstraction doesn't cost anything, as it's just straightforward pointer arithmetic underneath. This is why it is in the language.
- Peaker 16y agoThere are list abstractions in libraries. See the Linux kernel's list.h for a very nice and generic linked-list implementation.
- codexon 16y ago+1 C is ugly. - Why do I need to typedef a struct to make it look like any other type? - Why do function pointers look so bad? - Error handling in C? Those goto exception; if(0){exception:} sure look fantastic. - int* var1, var2; var2 is an int, not an int* ... The language lets you stick the * right next to the type as though it modifies the type. Which it does in casts. (how do I escape asterisks on here?) - A system language where int can be 32 bits or more. And probably more from people more experienced in C than me.
- stevetjoa 16y agoC was my second love after QBasic. I still admire its simplicity, yet also admire the abstraction, readability, and community of Python. As anyone on Stack Overflow would tell you, it's a matter of using the right tool for the job. I like to imagine programming language selection as an optimization problem: given a job, over the set of tools, maximize the appropriateness of tools for the job while minimizing the number of tools. Without a tool number constraint, you could call from an infinite number of tools, and one would be best, but that incurs overhead, obviously. The tool number constraint keeps the problem feasible. For numerical computing, {C, Python} solves my optimization problem.
- starpilot 16y agoStupid question: what do you do when you're working in C and want OOP?
- burgerbrain 16y agoUse OOP. :) But seriously, you don't need a language to smack you in the face with OOP syntax to use OOP, and with anything in programming, if you try to do it just like you do in other languages, you're going to be disappointed.
- jpr 16y agoDepends on what you mean by OOP. But function pointers (mostly in structs) are one way to do it.
- bergie 16y agoWe use GObjects for that: http://library.gnome.org/devel/gobject/stable/ http://library.gnome.org/devel/gobject/stable/ Through GObject Introspection this also means we get automatic OOP bindings to a bunch of higher-level languages
- derleth 16y agohttp://www.bolthole.com/OO-C-programming.html http://www.bolthole.com/OO-C-programming.html That's a simple intro to doing OO in C, mainly by becoming a human compiler that goes from whatever OO language you really want to C. Note well that OO isn't a well-defined concept. Look at this list of features and ask yourself which of them you think of as being OO: http://www.paulgraham.com/reesoo.html http://www.paulgraham.com/reesoo.html
- eru 16y agoWhy would you want that?
- j_baker 16y agoWhile we're making silly analogies, let me make one of my own. C is a bit like a horse. It's fun to ride a horse from time to time, and there are still plenty of valid, practical reasons to ride horses. However, you're kidding yourself if you think you're going to talk me into giving up my car (well, ok... my public transportation) for a horse.
- burgerbrain 16y agoC is more like trucks or trains than horses. Backbone of the modern society, but using them for all, or even just day to day, transportation needs could be called misguided. Fortran is more horsey.
- KirinDave 16y agoThese rhythms reflect a greater underlying symmetry. All languages have them, and some of them reflect these symmetries more easily than others. When you start to get familiar enough with a language that you see past it to the underlying structure, that's when you really start to appreciate a language. This process is not unique to C, or any other language.
- sid0 16y agoThis sort of idiocy is what holds computer science back. C, because of how close to the metal it is, is pretty much the ugliest language in wide use, and the fact that we know a lot more about programming languages and user interfaces now than when C was designed doesn't help. It should only be used as a last resort, when you really need the speed, and even then one should keep an eye out for alternatives.
- varjag 16y agoWhich language would you suggest as replacement for writing of sched.c and page_alloc.c?
- waynecolvin 16y agoOberon or Modula-2 maybe?
- sid0 16y agoLast resort, remember. I'm not saying C doesn't have its place -- I'm saying that it should be used as little as possible. While I guess there's no other option for a memory allocator, it should be quite possible to write a scheduler in another, better systems language like Rust. edit: actually, possibly not -- Rust might be a little too abstract. My point still stands, though.
- gnuvince 16y agoWhat about Ada?
- waynecolvin 16y agoIt was a cute sales pitch for the company's C IDE. I especially like how they use "soccer", a word that resembles "sorcery". Talking about how one (only?) gets the gist of things and is not a kernel hacker was great to speak to the less-than-competent. Yes yes few of us write kernel modules but still. Encouraging coders to be comfortable with C when they sell a product to help, brilliant!