1. Use meaningful names, that are build in a standard manner, and as long as necessary. For example, InptScr (inpatient serum creatinine), MaxInptScrDt (date when maximal inpatient serum creatinine was reached). Why: using short names saves on typing, but is much harder to understand in the future or when reading someone else's code. Longer names serve as comments.
  2. Separate chunk of codes that do separate things and comment them well. For example, if you need to graph something, but you also need to make calculations for this graph, it's a good idea first to calculate whatever you need, and then, in a separate chunk of code, to do the graph. The same holds for functions (with some exceptions). Function should do well defined and somewhat narrowly defined tasks. Example of what shouldn't be done is when a function returns some data, then spits out a table, then plots a graph, and boy, does it catch fish. Why: narrowly defined chunks of code or functions are easier to understand maintain and modify, they allow to avoid duplicated code.
  3. Avoid hard coding - instead of constants, use variables with names. For example, if number of visits in the study is 4 create a variable NumberOfVisits and set it to 4. Why: when we need to change this number instead of searching for "4", we can search only for NumberOfVisits and change it only in one place (where NumberOfVisits is defined). It also improves readability: we don't have to guess what 4 is, we know that this is a number of visits.
  4. Create constants dynamically. For example, if the study is still collecting data, and the number of collected visits is increasing, figure out a way to get this number out of the data, for example, if the data contains visit number per each non-missing record, NumberOfVisits can be figured out from it. Why: saves time that otherwise is spent on figuring out where the code crashed or why the report doesn't have a required visit, also improves readability.

-- SvetlanaEden - 25 Aug 2011
Topic revision: r2 - 24 Aug 2012, SvetlanaEden
This site is powered by FoswikiCopyright &© 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback