Upcoming SlideShare
×

129

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
129
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide

1. 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. 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. 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. 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. 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
6. 6. #5 Power of Style A B
7. 7. #6 Rank the following statements! A B C
8. 8. A B #7 Which is easier to understand?
9. 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. 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. 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))
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.