[Swan-dev] another style hint for making dataflow clearer

D. Hugh Redelmeier hugh at mimosa.com
Thu Sep 20 05:16:21 UTC 2018


- don't initialize a variable unnecessarily.

  + this confuses the human reader -- she thinks the initialization has 
    some meaning, but it does not

  + this prevents automatic tools from discovering paths were the variable 
    will not be set before being used.  (Technically the spurious 
    initialization prevents this bug but logically it is covering it up.)

This complements previous hints:

- declare auto variables as late as possible.  Especially nice if this can 
  be combined with the first assignment (often the only assignment)

  + this reduces the region of code in which the variable is undefined 
    (i.e. can be mentioned but is meaningless)

- declare auto variables as deep within nested blocks as is convenient

  + this reduces the scope of the variable and thus the amount of
    code that must be read to understand the variables meaning

Similarly:

- as many globals as possible should be made file-static.

  + this reduces the scope and thus makes it easier to understand the
    object

- as many file statics as possible should be made autos or
  function-statics

  + same reason

- as many pointers as possible should be made to be pointers to const.

  + makes understanding dataflow a lot easier


More information about the Swan-dev mailing list