Pages

Monday, July 3, 2017

The miracle of C




It is not fashionable to love the C language. Not like, but love. Time moves on and glorifying something from the seventies seems idiotic. But let us use a standard historian's trick: take C away and see what changes.

What would systems programmers use? FORTRAN? Assembly language? Pascal?

We would not have Java with the current syntax or semantics, no dotNet beyond Visual Basic, no browsers, no TCP/IP, no proliferation of language wars, and no C++.

LISP may have evolved to be more widely usable, and Pascal would have likely gained some practicality, at least proper initializers and scoping, and maybe modularity. FORTRAN would have remained dominant. We would still have APL, it was miraculously written in 360 assembler.

Oh yes, JavaScript would be LISP.  Git would be an unrealized dream, as would Lua and LabVIEW and probably MatLab.

Maybe evolution would have found a way, and maybe the down side of C's flexibility and power is fertility. It is an engine of change.

C is attributed to Dennis Ritchie, but its precursor, B, which has an almost identical syntax is pure Ken Thompson. It is this syntax that is so compelling. It is the closest thing to the thought process of a programmer. I would rather write in C than in anything else, and I am confused by those that thought that something like awk of sh were even necessary to make scripting easier. I can do most of it faster in C. And please do not bring up Perl.

C is compact, and it is complete and orthogonal and close to damn perfect. Arrays, pointers, and increments all mesh, probably thanks to Ritchie's semantics.

C is not a glorified assembly language, it is the centre of computer languages, instruction sets have evolved because of it. It distilled all that one needs or wants to do with a CPU.

Objects and user interaction are layers that can easily be added, and C has no trouble with these things, it can stretch.

Many say one can get hurt by its lack of constraint, but I find I get much more hurt by a lack of elegance and clarity in other languages, I sometimes feel like the ugliness of a language can hinder expression, or at least confuse it, and waste programmer's time.

I use C to develop algorithms that I sometimes need to implement in other languages, because it helps to reduce notational ambiguity. It is also universal. In that sense it is a kind of mathematics. Ken Thompson holds a very mathematical view of computing, in a good pragmatic sense, this is the guy who implemented regular expressions, who writes papers that are a joy to read, and who seems to think in a bottom-up clear way. Because of this, I think of him as a founding father of modern computer languages, he took Algol and its kin and made a definitive contribution.

C++ is not an improvement, it is a shameful opportunistic perversion. I use it because it has C in it, and luckily it remains compatible, and its flaws have inspired improvements, like Go. Even C# is a blessing compared to it. Bjarne's complaints and justifications around the preprocessor are noise. The preprocessor in C is a wonderful tool, and used well it can make the language do anything and work anywhere. It is brilliant meta hack. A leap of inventiveness, and rather than dismiss it as a source of bugs, it should be entrenched. C++'s templates are good things, but please do not claim they came to be as replacement for the preprocessor. Without it C++ would not have come into being so quickly. That same preprocessor was used by Bjarne to implement C with classes before it became C++.

Linus is right to dislike C++, it is easy to mess up in it, simply because it hides stuff, and I mean encapsulate is a bad word sometimes. So is overloading if misused, and programmers are a conflicted bunch, they sometimes favour cleverness over communication. I know, I do it and then regret it when I read my code later.

On the other hand, I do the OOP thing, and I think it can help code maintenance and force teams to adhere to design intentions, however it can all be done in C, if you are familiar with its philosophy. One architect I worked with a long time ago would write .h files and pass them around to developer to flesh out into .c or .cpp files. Objects take this a bit further, but not much. The biggest benefit is when doing UI's and other repetitive verbose coding, or when you want to get rid of switch statements all over the code.


And yes, no C, no Unix, no Linux, no Web as we know it.

I am not slamming other languages, just trying to put C where it belongs, at the top (because it is bottom up :-), for its expressiveness, rigour, elegance and efficiency. Did I forget anything?

No comments: