code readability in r

11
Code Readability in R Kim Man Lui, PhD Code readability is actually for more yourself than others. Note: R guys generally use “<-” in R; however, many developers have used “=“ in programming for decades. Does this really matter? (see #8)

Upload: hzhbcl

Post on 28-Nov-2014

226 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Code readability in r

Code Readability in RKim Man Lui, PhD

Code readability is actually for more yourself than others.

Note: R guys generally use “<-” in R; however, many developers have used “=“ in

programming for decades. Does this really matter? (see #8)

Page 2: Code readability in r

#1 Which one is precise?

if (0==colCon) { pv=0 } else { pv=(colCon * 4.5 - 10 )/100 }

pv = if (0==colCon) 0 else (colCon * 4.5 - 10 )/100

A

B

Page 3: Code readability in r

#2 Which is easier to understand?

pointVal = if (0==colConDex) 3 else 6 bheadtmp = bhead + pointVal

bheadtmp = bhead + if (0==colConDex) 3 else 6

A

B

Page 4: Code readability in r

#3 When B is better than A?

coeff[[1]][[1]]= coeff[[1]][[2]]=rep(0,20)coeff[[1]][[1]][4]=1coeff[[1]][[1]][6]=1… … …

coeff[[1]][[1]]=c(0,0,0,1,0, 1,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0) coeff[[1]][[2]]=c(0,0,0,0,0, 0,0,1,0,0, 0,0,0,0,0, 0,0,0,0,0)

A

B

Page 5: Code readability in r

#4 Which is simpler? d100=performanceCheck(100)d50 =performanceCheck(50)d21 =performanceCheck(21)d14 =performanceCheck(14)… … … … …

perf=sapply( data.frame(d100=100, d50=50, d21=21, d14=14), performanceCheck)

A

B

Page 6: Code readability in r

#5 Power of Style

A

B

Page 7: Code readability in r

#6 Rank the following statements!

A

B

C

Page 8: Code readability in r

A

B

#7 Which is easier to understand?

Page 9: Code readability in r

#8 Which one is incorrect?

demo=function ( x , y=2) {return (x+y)}

demo=function ( x , y <- 2) {return (x+y)}

Y <- 2

Y = 2A

B

C

D

Page 10: Code readability in r

#9 Which one is intuitive?

s=subset(s, (as.Date(today)-30) <= time(s) & time(s)<=today & 0<s[,5])

s=subset(s, time(s)>=(as.Date(today)-30) & time(s)<=today & s[,5]>0)

A

B

General Principle : when assigning a number to a variable, write as var=1 when doing logical operations, try to write as 1==var This way helps our eyes to judge assignment or comparison quickly

Page 11: Code readability in r

#10 Should we avoid a long way to passing parameters? fa=function(x1) { a1=mean(x1) ; return (a1) }

fb=function(x2,y2, isLog=TRUE) { a2=x2-y2 ; b2=fc(a2, isLog=isLog) ; return (b2) }

fc=function(x3, isLog=TRUE) { a3=x3*x3; b3=fd(a3, isLog=isLog) return (b3) }

fd=function(x4, isLog=TRUE) { a4= sum(sqrt(x4)) if (isLog==TRUE) print(paste(x4)) return (a4) }

dat=c(2,3,4,5)fb(dat, fa(dat), isLog=FALSE)

A B

fa=function(x1) { a1=mean(x1) ; return (a1) }

fb=function(x2,y2) { a2=x2-y2 ; b2=fc(a2) ; return (b2) }

fc=function(x3) { a3=x3*x3; b3=fd(a3) return (b3) }

fd=function(x4) { a4= sum(sqrt(x4)) isLog=getPara(“isLog”) if (isLog==TRUE) print(paste(x4)) return (a4) }

dat=c(2,3,4,5)setPara(“isLog”, FALSE)fb(dat, fa(dat))