C & C++: however, amuzed that my line might be considered

  • william_l_bahn / 200 / Wed, 24 Jun 2009 08:27:00 GMT / Comments (0)
  • shut up. I am, however, amuzed that my line might be considered OK,
    sin 2) putting a constnat on the left side of a conditional. In English we say "if the number read equals 2", and C is primarily designed to communicate information to a human programmer. I disagree with argumentthat it prevents an accidental if nread = 2 - a programmer can get out bugslike this as he writes. It is much more difficult to get bugs out further down the line, and harder still if the code isn't easy to read.

    And harder still if the code compiles with a logic error that may or may not produce an obvious (and easy to track down) runtime problem. Why would you pass up the opportunity to turn a subtle logic error into a glaring syntax error? I can't begin to count the number of times I have seen people write the following:

    fp = fopen("foo.txt", "r");
    if(fp = NULL)
    exit(1);
    c = fgetc(fp);

    Presto - instant Tinker Bell code (one way ticket to Never-Never Land)
    I had one gal that, when I suggested she write the test as (NULL == fp) and explained why basically made the exact argument you did - because it "looks awkward". Two hours later she wanted help because her program had been doing nothing but crashing for two hours and guess what? And this was with a situation where the runtime behavior was anything but subtle!

    sin 5) trailing opening curly brace. This is a most annoying habit,because it makes it difficult to see where one block beings and the matching brace ends.
    This one I heartily agree with - but it is a style preference and the trailing brace seems to be the overwhelming choice of most texts. I much prefer to place matching braces directly in line vertically with nothing in between them (i.e., all text in the block, including comments, indented more than the braces). It makes blocking errors much easier to avoid and much, much easier to correct when they are made.
  • Keywords:

    however, amuzed, line, might, considered, c++

  • http://programming.itags.org/c-c++/199415/«« Last Thread - Next Thread »»