Hacker News new | past | comments | ask | show | jobs | submit login

It is bad not only because it predates C99, but also because virtually any example consists of a couple of global variables/fixed-size arrays, a couple of badly named functions which operate on the globals and use some very "descriptive" names of the local variables. This book was born in an era when the modern understanding of "good code" was barely honored at all.

"Relying too heavily on external variables is fraught with peril since it leads to programs whose data connections are not at all obvious" - k&r, ch. 1

Who are these Kernighan and Ritchie guys? They obviously just don't understand "good code", terrible book!

People pick up on what you do, including supplied examples, not what you say.

(And I say this as someone who likes K&R.)

Take, for example, the declaration parsing example and honestly tell me that this isn't crappy code by any modern standard:

    enum { NAME, PARENS, BRACKETS };
    void dcl(void);
    void dirdcl(void);
    int gettoken(void);
    int tokentype;
    char token[MAXTOKEN];
    char name[MAXTOKEN];
    char datatype[MAXTOKEN]; /* data type = char, int, etc. */
    char out[1000];

    main()  /* convert declaration to words */
        while (gettoken() != EOF) {   /* 1st token on line */
            strcpy(datatype, token);  /* is the datatype */
            out[0] = '\0';
            dcl();       /* parse rest of line */
            if (tokentype != '\n')
                printf("syntax error\n");
            printf("%s: %s %s\n", name, out, datatype);
        return 0; 

Pg 124 from 2nd edition where this code is pulled (emphasis mine):

"[T]he programs are intended to be illustrative, not bullet-proof, there are significant restrictions on dcl."

In context this code illustrates the points they're trying to convey about complicated declarations quite well and is a precursor to understanding typedef later in the book.

Also, you left the nice inline comments off the variable declaration block.

I would like to see where typedef could have been used?

"Modern standard" or not, I find this piece of code highly readable, and, in general, very good for what it does, at the level (of simplicity, perhaps even "primitivism") at which it was intended to do that.

This piece of code is crap - it's not functional, keeps implicit state.

Not all quality code is functional.

Could someone point out the craps in the code ? I only noticed excess use of global variables , and out array(can't figure out why it was index only at zero)

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact