Skip to content

Stylistic and code consistency rules #309

@malcolmbarrett

Description

@malcolmbarrett

This is a sort of internal style guide for various decisions we need to make. All of these are open to discussion, but I want to be consistent for all of them. This issue also serves as a final check when the first edition is done.

Code

  • Use grkstyle::grk_style_dir() to style code
  • Use tibble() instead of data.frame()
  • Use summarize() instead of summarise()
  • Use if_else() instead of ifelse()
  • When grouping by a single variable, use group_by() over .by
  • When grouping by more than one variable when you don't need persistent grouping, use .by or group_by() with .groups = "drop" instead of ungroup(). Prefer .by unless group_by() is more readable for the code, e.g., a very long or complex summarize() statement
  • augment(data = ...) vs. augment(newdata = ...): For cases when we are supplying the original data frame (which we need to do to keep the variables that weren't in the model), use the data argument of augment() instead of newdata. newdata should only be used when it's actually a new data frame, as we do in g-computation cloning.
  • Use |> instead of %>%
  • Use slice_sample() instead of sample_n(), sample_frac() (because it supercedes these per the documentation)
  • For comments, use all lowercase (except proper nouns) and one # at beginning
  • Use explicit testing of 0 and 1, e.g. if x = 1, write if (x == 1) rather than if (x)
  • For case_when(), use .default = default_value rather than TRUE ~ default_value

Quarto

  • Prefer cross-references over writing that we discussed/will discuss something somewhere else in the book
  • For code we are not showing, prefer code-fold: true over echo: false. This way, we can de-emphasize the code while providing easy access for those interested
  • Use sentence breaks (a line break after every sentence) for easier git usage. You can do this automatically by changing your settings in RStudio to use sentence breaks and turning visual mode on and off.
  • For package names, write it as `{pkg}` (which will automatically link to the package website) the first time you mention it in a chapter; thereafter, don't use formatting. Just write: pkg.

Writing

  • Check that anytime we type "casual" we mean it. Common typo!
  • Defined terms are in bold; italics used for emphasis, not definitions.
  • Prefer "exposure" and "outcome" as generic terms for the cause and effect over e.g., treated vs untreated, but use your judgment
  • Write it as "Extra Magic Hours" and "Extra Magic Morning/Evening" (all caps)
  • Write times as 9 AM instead of 9am
  • "data frame" and "data set" (with a space between)
  • Consistency with "upweighting" and "downweighting" (one word with no space)

Figures and tables

  • All figures and tables have a thorough caption (what is actually in the fig/tbl, not just the idea behind it)
  • Use gt() for code-based tables instead of kable(). Otherwise, use markdown tables.
  • No time labels for EMM DAG since we don't actually know. Putting this here because we reuse this code in several places so we should check for consistency

I think we may have some other things to add for this one to make sure these are consistent

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions