Google で働きたい ①

から、Google's R Style Guideに従おうと思う。

File Names

File names should end in .R and, of course, be meaningful.

ファイル名は.R にしましょう。
自分のRの師匠は.r使ってました。
彼はgoogle入れませんね。

Identifiers

Don't use underscores ( _ ) or hyphens ( - ) in identifiers. Identifiers should be named according to the following conventions. The preferred form for variable names is all lower case letters and words separated with dots (variable.name), but variableName is also accepted; function names have initial capital letters and no dots (FunctionName); constants are named like functions but with an initial k.

アンダースコア、ハイフンは使わないようにしましょう。
python慣れしている身からするとついつい使いがちです。
変数名はドットで区切り(variable.name)、関数名はキャメルケースにしましょう(FunctionName)。

参考: キャメルケースよりスネークケースで。 - 偏見プログラマの語り!

Syntax

Line Length

The maximum line length is 80 characters.

1行80文字までにしましょう。

Indentation

When indenting your code, use two spaces. Never use tabs or mix tabs and spaces.

インデントは2文字のスペースにしましょう。タブを使うのはやめましょう。
やばい、4文字スペースにしてた。。

Spacing

Place spaces around all binary operators (=, +, -, <-, etc.).

二項演算子の後にはスペースを置きましょう。

Extra spacing (i.e., more than one space in a row) is okay if it improves alignment of equals signs or arrows (<-).

=<-で値を代入する際にそろえる目的でスペースを使うことはOKです。

plot(x    = x.coord,
     y    = data.mat[, MakeColName(metric, ptiles[1], "roiOpt")],
     ylim = ylim,
     xlab = "dates",
     ylab = metric,
     main = (paste(metric, " for 3 samples ", sep = "")))

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.

1行のコードブロックに対し、
こう書くか

if (is.null(ylim)) {
  ylim <- c(0, 0.06)
}

こう書くか

if (is.null(ylim))
  ylim <- c(0, 0.06)

は、揃えましょう。

Surround else with braces

An else statement should always be surrounded on the same line by curly braces.

else}{に囲まれるように書きましょう。

if (condition) {
  one or more lines
} else {
  one or more lines
}

Assignment

Use <-, not =, for assignment.

代入には矢印使いましょう。

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.)

コードの終わりにセミコロンは付けるのはやめましょう。必要ないです。

長い

とりあえずここまでにしよ。。 続きはまた書きます。

参考

Google's R Style Guide