1. Symbols and nomenclature
1. File naming
End the file with .R, and add as much information as possible to the file name, such as predicting advertising revenue, use lowercase words, and connect them with an underscore (_):
predict_ad_revenue.R
2. Identifier
Do not use underscore (_) or hyphen (-) identifiers. Identifiers should follow the following naming conventions:
the preferred form of variable names is all lowercase letters, with dots between words (variable.name), but variableName is also accepted;
function names are initially uppercase, without dots (FunctionName).
(1) Variable names
Note that in the R environment, case is sensitive. Variable names should all start with lowercase letters, and the rest of the words are capitalized: avgClicks
(2) Function name
The function name starts with each word with a capital letter, and does not need to be connected.
Function Name: CalculateAvgClicks
Name the function with the verb
(3) Constant item
The constant item is named the same as the function but starts with a small k.
Constant item: kConstantName
2. Grammar
1. Maximum length of each line
Each line can be up to 80 characters long.
2. Indent
When the code is indented, leave two spaces blank.
3. Spacing
(1) When using all binary operators (such as =, +, -, <, etc.) add spaces at both ends. Exception: When the symbol = is a function call, the passed parameters are not separated by spaces.
Do not separate with a space before the comma ",", but add a space after the ",".
tab.prior <- table(df[df$days.from.opt < 0, "campaign.id"])
total <- sum(x[, 1])
total <- sum(x[1, ])
This includes assignments, logical symbols, and comma separation.
(2) Use spaces to the left of parentheses, except when calling functions.
if (debug)
(3) There can be extra spaces (multiple spaces in a line) if this improves the alignment between symbols.
plot(x = x.coord
y = data.mat[, MakeColName(metric, ptiles[1])])
(4) No spaces are used in parentheses or square brackets, except after commas.
if (debug)
x[1, ]
4. Curly braces
(1) The opening bracket of a curly brace should not be on its own line; and a closing bracket should be on its own line. You can leave out the curly braces when a block of code is a single statement. However, other identical situations must be considered for consistency.
(2) Curly braces and else
An else statement should always be surrounded by curly braces on the same line.
5. Assignment
Use <-, not =
3. Function
1. Function definition and call
The function definition should list the non-default parameters in the function first, followed by the default parameters. When defining and calling a function, multiple statements per line are allowed, and a line break is generally only done when assigning a value.
2. Function description
Comments should be made on the next line of the function definition, including describing the function of the function; describing the parameters in the statement with Args: describing the data type; describing the return value with Returns:.
The comment section should be as detailed as possible so that the reader can apply the function without looking at the code. for example:
3. Error message
Errors should be notified with stop()
4. Notes
1. Line comments
Short comments can be placed after the code, separated by a space + # + space;
longer comments can be on a separate line.
2. Block comments
Use if statement
5. Layout and sorting
If everyone used the same general order, we were able to read and understand each other's scripts faster and easier. The general start should include: