R Coding Style Guide and Naming Conventions
Sources:
- **
Overview
Object | Notation | Length | Plural | Prefix | Suffix | Abbreviation | Mask | Example |
---|---|---|---|---|---|---|---|---|
File Name | lowercase | 30 | No | No | .R | No | [a-z_]+.R | predict_ad_revenue.R |
Function Name | PascalCase | 30 | No | No | No | No | [A-z]+ | CalculateAvgClicks |
Variable Name | camelCase | 30 | No | No | No | No | [A-z]+ | avgClicks |
Data Frame | PascalCase | 30 | No | df_ | No | No | df_[A-z]+ | df_AvgClicks |
Factor Variable Name | PascalCase | 30 | No | f | No | No | f[A-z]+ | fAvgClicks |
Notation and Naming
File Names
File names should end in uppercase .R
, word delimeter '_'
, lowercase notation and, of course, be meaningful.
GOOD: predict_ad_revenue.R
BAD: foo.R
Identifiers
Donât use underscores ( _ ) or hyphens ( - ) in identifiers. Identifiers should be named according to the following conventions. Variable names have initial lower case letters. If variable name consists of several words, then they arenât separated, but all words except for the first one begin with capital letters. Function names are formed like variable names but with initial capital letter (FunctionName); constants are named like functions but with an initial k.
-
variableName
GOOD: avgClicks
BAD: avg_Clicks
-
FunctionName
GOOD: CalculateAvgClicks
BAD: calculate_avg_clicks , calculateAvgClicks
Make function names verbs.
Exception: When creating a classed object, the function name (constructor) and class should match (e.g., lm).
- kConstantName
Syntax
Line Length
The maximum line length is 80 characters.
Indentation
When indenting your code, use two spaces. Never use tabs or mix tabs and spaces.
Exception: When a line break occurs inside parentheses, align the wrapped line with the first character inside the parenthesis.
Spacing
Place spaces around all binary operators (=, +, -, â, etc.).
Exception: Spaces around =âs are optional when passing parameters in a function call.
Do not place a space before a comma, but always place one after a comma.
GOOD:
BAD:
Place a space before left parenthesis, except in a function call.
GOOD:
BAD:
Extra spacing (i.e., more than one space in a row) is okay if it improves alignment of equals signs or arrows (â).
Do not place spaces around code in parentheses or square brackets.
Exception: Always place a space after a comma.
GOOD:
BAD:
Curly Braces
An opening curly brace should never go on its own line; a closing curly brace should always go on its own line. You may omit curly braces when a block consists of a single statement; however, you must consistently either use or not use curly braces for single statement blocks.
GOOD:
Always begin the body of a block on a new line.
BAD:
Surround âelseâ with braces.
An âelseâ statement should always be surrounded on the same line by curly braces.
GOOD:
BAD:
BAD:
Assignment
Use <-
, not =
, for assignment.
GOOD:
x <- 5
BAD:
x = 5
Semicolons
Do not terminate your lines with semicolons or use semicolons to put more than one command on the same line. (Semicolons are not necessary, and are omitted for consistency with other Google style guides.)
Organization
General Layout and Ordering
1.Copyright statement comment 2.Author comment 3.File description comment, including purpose of program, inputs, and outputs 4.source() and library() statements 5.Function definitions 6.Executed statements, if applicable (e.g., print, plot)
Unit tests should go in a separate file named âoriginalfilename_test.Râ.
Commenting Guidelines
Comment your code. Entire commented lines should begin with â#â and one space.
Short comments can be placed after code preceded by two spaces, â#â, and then one space.
Function Definitions and Calls
Function definitions should first list arguments without default values, followed by those with default values.
In both function definitions and function calls, multiple arguments per line are allowed; line breaks are only allowed between assignments.
GOOD:
BAD:
Ideally, unit tests should serve as sample function calls (for shared library routines).
Function Documentation
Functions should contain a comments section immediately below the function definition line. These comments should consist of a one-sentence description of the function; a list of the functionâs arguments, denoted by Args:, with a description of each (including the data type); and a description of the return value, denoted by Returns:. The comments should be descriptive enough that a caller can use the function without reading any of the functionâs code.
Example Function
TODO Style
Use a consistent style for TODOs throughout your code.
Attach
The possibilities for creating errors when using âattachâ are numerous. Avoid it.
Functions
Errors should be raised using stop().
Objects and Methods
The S language has two object systems, S3 and S4, both of which are available in R. S3 methods are more interactive and flexible, whereas S4 methods are more formal and rigorous. (For an illustration of the two systems, see Thomas Lumleyâs âProgrammerâs Niche: A Simple Class, in S3 and S4â in R News 4/1, 2004, pgs. 33 - 36: http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf.)
Use S3 objects and methods unless there is a strong reason to use S4 objects or methods. A primary justification for an S4 object would be to use objects directly in C++ code. A primary justification for an S4 generic/method would be to dispatch on two arguments.
Avoid mixing S3 and S4: S4 methods ignore S3 inheritance and vice-versa.
Resources
Appendix
Note created on 2024-05-09 and last modified on 2024-05-09.
Backlinks
(c) No Clocks, LLC | 2024