development:ohdsi_code_style_for_r
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| development:ohdsi_code_style_for_r [2015/02/06 04:12] – created schuemie | development:ohdsi_code_style_for_r [2020/04/06 13:39] (current) – schuemie | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== OHDSI code style for R ====== | ====== OHDSI code style for R ====== | ||
| - | (Adapted from http://r-pkgs.had.co.nz/r.html#style) | + | This has moved here: https://ohdsi.github.io/ |
| - | + | ||
| - | ===== Case ===== | + | |
| - | We use camelCase in R. Function and variable names all start with lowercase. Package names start with uppercase. | + | |
| - | + | ||
| - | Examples: | + | |
| - | * '' | + | |
| - | * The '' | + | |
| - | + | ||
| - | ===== Naming conventions ===== | + | |
| - | Function names typically start with a verb. Variable names are typically nouns. | + | |
| - | + | ||
| - | + | ||
| - | ===== Spacing ===== | + | |
| - | Place spaces around all infix operators (=, +, -, <-, etc.). The same rule applies when using = in function calls. Always put a space after a comma, and never before (just like in regular English). | + | |
| - | + | ||
| - | ==== Good ==== | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | ==== Bad ==== | + | |
| - | '' | + | |
| - | + | ||
| - | There’s a small exception to this rule: :, :: and ::: don’t need spaces around them. | + | |
| - | + | ||
| - | ==== Good ==== | + | |
| - | '' | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | ==== Bad ==== | + | |
| - | '' | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | Place a space before left parentheses, | + | |
| - | + | ||
| - | ==== Good === | + | |
| - | '' | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | ==== Bad ==== | + | |
| - | '' | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | Extra spacing (i.e., more than one space in a row) is ok if it improves alignment of equal signs or assignments (<-). | + | |
| - | + | ||
| - | Do not place spaces around code in parentheses or square brackets (unless there’s a comma, in which case see above). | + | |
| - | + | ||
| - | ==== Good === | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | ==== Bad ==== | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | An opening curly brace should never go on its own line and should always be followed by a new line. A closing curly brace should always go on its own line, unless it’s followed by else. | + | |
| - | + | ||
| - | ===== Indentation ===== | + | |
| - | + | ||
| - | Always indent the code inside curly braces. It’s ok to leave very short statements on the same line: | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | Strive to limit your code to 80 characters per line. This fits comfortably on a printed page with a reasonably sized font. If you find yourself running out of room, this is a good indication that you should encapsulate some of the work in a separate function. | + | |
| - | + | ||
| - | When indenting your code, use two spaces. Never use tabs or mix tabs and spaces. Change these options in the code preferences pane. | + | |
| - | + | ||
| - | The only exception is if a function definition runs over multiple lines. In that case, indent the second line to where the definition starts. | + | |
| - | + | ||
| - | ===== Assignment ===== | + | |
| - | + | ||
| - | Use <-, not =, for assignment. | + | |
| - | + | ||
| - | ==== Good === | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | ==== Bad ==== | + | |
| - | + | ||
| - | '' | + | |
| - | + | ||
| - | + | ||
| - | ===== Commenting guidelines ===== | + | |
| - | + | ||
| - | Comment your code. Each line of a comment should begin with the comment symbol and a single space: #. Comments should explain the why, not the what. | + | |
| - | + | ||
| - | Use commented lines of - and = to break up your file into easily readable chunks. | + | |
| - | + | ||
| - | ''# | + | |
| - | + | ||
| - | ''# | + | |
development/ohdsi_code_style_for_r.1423195929.txt.gz · Last modified: by schuemie
