[Ilugc] .a-tip-a-day (patch - apply diffs and act as inverse of diff)

  • From: kapil@xxxxxxxxxxx (Kapil Hari Paranjape)
  • Date: Wed Sep 30 18:01:24 2009

Hello,

I apologise for offending people and igniting a flamewar.
However, I could not prevent myself from writing the following long
response. :-(

On Wed, 30 Sep 2009, Manvendra Bhangui wrote:

I am sorry about that. I honestly wanted to have some meaningful
conversation and my point was only on someone making a comment that C is
difficult to write and difficult to read - which is untrue if one knows the
language.

While your intentions were honourable, your remark was about speed
and efficiency and number of syscalls --- none of which has anything
to do with reading/writing C!

We all love the language which we are comfortable with. And a artist can
take any tool and make a poetry or a painting out of it and not complain
about the tool.

That is certainly true. There are indeed a large number of good
programs written in C --- however, even those programs would be
almost un-understandable if the comments are stripped out!

The kind of things you talk about, like assigning a char to an int
and so on are _precisely_ the things that make the language difficult
to understand since one has to run a small compiler + semantic
analyser in one's head to keep track of programs that do that.

C is a good language for writing operating systems, device drivers
and system libraries. That is one of the things that it was designed
for. Until Unix, most of the above tasks were programmed in
assembly. Using C allowed one to deal with them at a higher level and
create greater structure in the OS code, hence making it more readable
(than assembly code) and yet almost as efficient (as assembly) at the
same time.

Readable code is not necessarily efficient. For example, in Haskell,
one definition of the list of primes is:

  primes :: [Integer]
  primes = sieve [2..]
        where sieve (p:xs) = p : sieve [x | x<-xs, x `mod` p /= 0]

This is almost exactly the mathematical definition of the Sieve of
Eratosthenes written thousands of years ago! So it is very readable
(comments are not required!). However, it is not an efficient program!

Now, scripting languages like Perl have some higher level facilities
so that it is comparatively easy to translate the above program into
it (retaining most of the inefficiency!). In C you would have to
implement list operations (and probably garbage collection) before
you could translate the program. However, in the course of doing
this you would find ways to make this more efficient as well! You
could also translate this new C program _back_ into Perl --- but
translating it back to Haskell would be significantly harder.

This example (if you work it out) will explain why I said that
programs are easier to write in Perl and easier to read in Haskell;
whereas neither is the case with C. As _you_ pointed out, good
programs in C do tend to be more efficient than any of these
languages and if a thoughtful author has provided enough comments,
then they become readable as well.

Hope this clarifies --- and douses the flames a bit!

Kapil.
--

Other related posts: