What is your preferred style for naming variables

2019-01-20 21:47发布

Which conventions for naming variables and functions do you favor in R code?

As far as I can tell, there are several different conventions, all of which coexist in cacophonous harmony:

1. Use of period separator, e.g.

  stock.prices <- c(12.01, 10.12)
  col.names    <- c('symbol','price')

Pros: Has historical precedence in the R community, prevalent throughout the R core, and recommended by Google's R Style Guide.

Cons: Rife with object-oriented connotations, and confusing to R newbies

2. Use of underscores

  stock_prices <- c(12.01, 10.12)
  col_names    <- c('symbol','price')

Pros: A common convention in many programming langs; favored by Hadley Wickham's Style Guide, and used in ggplot2 and plyr packages.

Cons: Not historically used by R programmers; is annoyingly mapped to '<-' operator in Emacs-Speaks-Statistics (alterable with 'ess-toggle-underscore').

3. Use of mixed capitalization (camelCase)

  stockPrices <- c(12.01, 10.12)
  colNames    <- c('symbol','price')

Pros: Appears to have wide adoption in several language communities.

Cons: Has recent precedent, but not historically used (in either R base or its documentation).

Finally, as if it weren't confusing enough, I ought to point out that the Google Style Guide argues for dot notation for variables, but mixed capitalization for functions.

The lack of consistent style across R packages is problematic on several levels. From a developer standpoint, it makes maintaining and extending other's code difficult (esp. where its style is inconsistent with your own). From a R user standpoint, the inconsistent syntax steepens R's learning curve, by multiplying the ways a concept might be expressed (e.g. is that date casting function asDate(), as.date(), or as_date()? No, it's as.Date()).

9条回答
女痞
2楼-- · 2019-01-20 22:13

Good previous answers so just a little to add here:

  • underscores are really annoying for ESS users; given that ESS is pretty widely used you won't see many underscores in code authored by ESS users (and that set includes a bunch of R Core as well as CRAN authors, excptions like Hadley notwithstanding);

  • dots are evil too because they can get mixed up in simple method dispatch; I believe I once read comments to this effect on one of the R list: dots are a historical artifact and no longer encouraged;

  • so we have a clear winner still standing in the last round: camelCase. I am also not sure if I really agree with the assertion of 'lacking precendent in the R community'.

And yes: pragmatism and consistency trump dogma. So whatever works and is used by colleagues and co-authors. After all, we still have white-space and braces to argue about :)

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-20 22:13

Usually I rename my variables using a ix of underscores and a mixed capitalization (camelCase). Simple variables are naming using underscores, example:

PSOE_votes -> number of votes for the PSOE (political group of Spain).

PSOE_states -> Categorical, indicates the state where PSOE wins {Aragon, Andalucia, ...)

PSOE_political_force -> Categorial, indicates the position between political groups of PSOE {first, second, third)

PSOE_07 -> Union of PSOE_votes + PSOE_states + PSOE_political_force at 2007 (header -> votes, states, position)

If my variable is a result of to applied function in one/two Variables I using a mixed capitalization.

Example:

positionXstates <- xtabs(~states+position, PSOE_07)

Not the answer you're looking for? Browse other questions tagged or ask your own question.

查看更多
聊天终结者
4楼-- · 2019-01-20 22:19

I did a survey of what naming conventions that are actually used on CRAN that got accepted to the R Journal :) Here is a graph summarizing the results:

enter image description here

Turns out (no surprises perhaps) that lowerCamelCase was most often used for function names and period.separated names most often used for parameters. To use UpperCamelCase, as advocated by Google's R style guide is really rare however, and it is a bit strange that they advocate using that naming convention.

The full paper is here:

http://journal.r-project.org/archive/2012-2/RJournal_2012-2_Baaaath.pdf

查看更多
登录 后发表回答
相关问题
查看全部
相关文章
查看全部
收藏的人(4)