1.17 Neat coding

When you write code in the script editor, try to make it as neat as possible by lining things up. You (or someone else) will invariably need to read or debug your code sooner or later and having neat code will make that process much easier. In addition to having neat code, always include clear and concise comments.

Here is an example of messy code and comments:

8^(1/3) # Use the inverse of 3 to get the cube root
log(100) # Natural logarithm
log(100, base = 10) # You can change the base
log10(100) # Some bases have their own function
exp(1) # Exponentiate
x <- data.frame(outcome = c(1,0,1,1), exposure = c("yes", "yes", "no", "no"),
                age = c(24, 55, 39, 18))

Here is an example where things are all lined up – much easier to read!

8^(1/3)             # Use the inverse of 3 to get the cube root
log(100)            # Natural logarithm
log(100, base = 10) # You can change the base
log10(100)          # Some bases have their own function
exp(1)              # Exponentiate
x <- data.frame(outcome  = c(1, 0, 1, 1),
                exposure = c("yes", "yes", "no", "no"),
                age      = c(24, 55, 39, 18))