Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Â
Code readability in r
1. 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)
2. #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
3. #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
4. #3 When B is better than A?
coeff[[1]][[1]]= coeff[[1]][[2]]=rep(0,20)
coeff[[1]][[1]][4]=1
coeff[[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
5. #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
9. #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
10. #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
11. #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))