a list of rules that every c programmer should follow

Upload: kgoulas

Post on 04-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 A List of Rules That Every C Programmer Should Follow

    1/4

  • 8/13/2019 A List of Rules That Every C Programmer Should Follow

    2/4

    )ever call a function without chec-ing its error return. *f the error is

    impossible, at least call assert!, as in this e0ample3 if "close"fd# ??

    assert"485#6

    Chec- null pointers against ):44, never against ' or anything else. (o not

    cast ):443 if "flurb"0, y# ?? "struct 9cd =#):44# = bad = D

    ;hen you have an initialiEation, a test, and an incrementation, use a for! loop

    instead of a while! loop. This ma-es the loop control more e0plicit and

    reduces the possibility of forgetting to increment.

    ;hen calling a function that returns a useful value or an error return, use this

    assign and chec-! paradigm3

    if ""p ? fcn"0, y## ?? ):44#

    D handle error D

    ;hile ordinarily * would recommend against doing assignment in the middle

    of an e0pression, this is an e0ception. *t occurs so often that its immediately

    recogniEable to the e0perienced reader, re

  • 8/13/2019 A List of Rules That Every C Programmer Should Follow

    3/4

    length e0clusive of the ):4 byte, and then add one for dimensioning. The

    alternative is, of course, also OK.

    5very switch! statement should have a default! case, to reduce debugging.

    *f it cant ever occur, use assert!3

    switch "0# LD

    default3

    assert"485#6

    *f your compiler supports function prototypes, always use them, both for

    library functions and for your own. They are the greatest contribution to C

    programming since the curly brace. *ve found this paradigm useful for static

    functions, as it ma-es modifications easier3

    static 2OO45) fcn"int, float#6static 2OO45) fcn"count, val#

    int count6

    float val6

    L

    D

    )ote that come compilers are set up to ignore library prototypes by default. or

    e0ample, with Microsoft C you have to define the symbol 4*)TN98!.

    ind out what compiler options generate the most warnings and the strictest

    error chec-ing, and turn them on. Ma-e sure all of your files compile with no

    warnings at all, or else youll miss a new warning because its mi0ed in with

    the old ones. (oing this will often mean that youll use lots of casts. 2ut

    dont +ust blindly put in a cast to ma-e a warning go away, or you might

    overloo- an actual bug.

    How you lay out your programs is a matter of personal preference. 2ut at

    least be consistent, or you ris- telling the reader that you dont -now what

    youre doing. 5ither put a statement on the same line as the if!, or on the

    ne0t line, but dont do it sometimes one way and sometimes the other. /utblan-s between a function name and the left parenthesis, or dont, but always

    do it the same way. 8ince all bugs are introduced during te0t editing, thats

    when you need a consistent style. 8o, dont use a C beautifier, unless its to fi0

    up a program someone else gave you.

    Call assert! as often as you have the energy to. (ont define )(52:! to

    remove assertions when your program is ready for delivery, because thats

    when undiscovered bugs have their worst conse

  • 8/13/2019 A List of Rules That Every C Programmer Should Follow

    4/4

    Marc P. 9och-ind

    ebruary J, &QJ

    Compu8erve J%JR%,&ISS

    Copyright &QJ dvanced /rogramming *nstitute, 4td. May be freely reproduced and

    distributed, but only in its entirety, with no editing whatsoever, e0cept thatmisspellings may have the mar-ing sicU! inserted after them, li-e thas sicU. The

    authors name and this Copyright notice must be included.