hardcoded constants

Rahul Dhesi dhesi at bsu-cs.UUCP
Thu Dec 15 02:51:28 AEST 1988


In article <1988Dec13.172306.16195 at utzoo.uucp> henry at utzoo.uucp (Henry
Spencer) writes:
>The policy we try to follow is that if you must hard-code a constant,
>then it must be accompanied by a comment explaining why that particular
>number is there.

A suggestion:  If you want to hard-code a constant, use a #define
anyway:

     check_break() {
     #define   CTRL_C    3        /* ASCII control C */
        if (keyscan() == CTRL_C)
           ...
     }

A well-chosen name will make the code understandable.  Any additional
information about the hard-coded value can be in a comment to the
#define itself.  Scanning the source for all #defines will let you
locate all hard-coded constants with some confidence.

So I find the above code fragment preferable to the following:

     check_break() {
        if (keyscan() == 3)       /* check for ASCII control C */
           ...
     }
-- 
Rahul Dhesi         UUCP:  <backbones>!{iuvax,pur-ee}!bsu-cs!dhesi



More information about the Comp.lang.c mailing list