User Tools

Site Tools


development:ohdsi_code_style_for_r

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
development:ohdsi_code_style_for_r [2015/02/06 04:20]
schuemie
development:ohdsi_code_style_for_r [2018/11/13 07:57]
schuemie [Indentation]
Line 42: Line 42:
  
 **Good** **Good**
 +<​code>​
 +if (debug) {
 +  do(x)
 +}
  
-''​if (debug) do(x)''​ +plot(x, y) 
- +</​code>​
-''​plot(x, y)''​+
  
 **Bad** **Bad**
  
-''​if(debug)do(x)''​+<​code>​ 
 +if(debug)
 +  ​do(x) 
 +}
  
-''​plot (x, y)''​+plot (x, y) 
 +</​code>​
  
 Extra spacing (i.e., more than one space in a row) is ok if it improves alignment of equal signs or assignments (<-). Extra spacing (i.e., more than one space in a row) is ok if it improves alignment of equal signs or assignments (<-).
Line 59: Line 66:
 **Good** **Good**
  
-''​if (debug) do(x)''​+<​code>​ 
 +if (debug) ​
 +  ​do(x) 
 +}
  
-''​diamonds[5, ]''​+diamonds[5, ] 
 +</​code>​
  
 **Bad** **Bad**
 +<​code>​
 +if ( debug ) {  # No spaces around debug
 +  do(x)
 +}
  
-''​if ( debug ) do(x)  # No spaces around debug''​ +x[1,]   # Needs a space after the comma 
- +x[1 ,]  # Space goes after comma not beforeCurly braces 
-''​x[1,]   # Needs a space after the comma''​ +</​code>​
- +
-''​x[1 ,]  # Space goes after comma not beforeCurly braces''​+
  
 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. 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.
Line 77: Line 90:
 Always indent the code inside curly braces. It’s ok to leave very short statements on the same line: Always indent the code inside curly braces. It’s ok to leave very short statements on the same line:
  
-''​if (y < 0 && debug) message("​Y is negative"​)Line length''​+<​code>​ 
 +if (y < 0 && debug) ​
 +  ​message("​Y is negative"​) 
 +
 +</​code>​
  
 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. 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.
Line 97: Line 114:
 ''​x = 5''​ ''​x = 5''​
  
 +===== If-then-else =====
 +
 +If-then-else clauses should always use curly brackets, even if there'​s only one clause and it's one statement.
 +
 +**Good**
 +
 +<​code>​
 +if (a == b) {
 +  doSomething()
 +}
 +</​code>​
 +
 +**Bad**
 +
 +<​code>​
 +if (a == b) doSomething()
 +</​code>​
 +
 +===== Use named arguments =====
 +
 +When calling a function that has more than one argument, make sure to refer to each argument by name instead of relying on the order of arguments. ​
 +
 +**Good**
 +
 +''​translateSql(sql = "​COMMIT",​ targetDialect = "​PDW"​)''​
 +
 +**Bad**
 +
 +''​translateSql("​COMMIT",​ "​PDW"​)''​
  
 ===== Commenting guidelines ===== ===== Commenting guidelines =====
Line 107: Line 153:
  
 ''#​ Plot data ---------------------------''​ ''#​ Plot data ---------------------------''​
 +
 +===== Curly brackets and new line =====
 +Opening curly brackets should precede a new line. A closing curly bracket should be followed by a new line except when it is followed by ''​else''​ or a closing parenthesis.
 +
 +**Good**
 +<​code>​
 +if (a == b) {
 +  doSomething()
 +} else {
 +  doSomethingElse()
 +}
 +</​code>​
 +
 +**Bad**
 +<​code>​
 +if (a == b) 
 +{
 +  doSomething()
 +
 +else 
 +{
 +  doSomethingElse()
 +}
 +</​code>​
 +
development/ohdsi_code_style_for_r.txt · Last modified: 2020/04/06 13:39 by schuemie