SlideShare ist ein Scribd-Unternehmen logo
1 von 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)
#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
#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
#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
#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
#5 Power of Style
A
B
#6 Rank the following statements!
A
B
C
A
B
#7 Which is easier to understand?
#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
#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
#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))

Weitere ähnliche Inhalte

Was ist angesagt?

Concurrency in Python4k
Concurrency in Python4kConcurrency in Python4k
Concurrency in Python4kRodolfo Carvalho
 
C mcq practice test 2
C mcq practice test 2C mcq practice test 2
C mcq practice test 2Aman Kamboj
 
A peek on numerical programming in perl and python e christopher dyken 2005
A peek on numerical programming in perl and python  e christopher dyken  2005A peek on numerical programming in perl and python  e christopher dyken  2005
A peek on numerical programming in perl and python e christopher dyken 2005Jules Krdenas
 
Brief Introduction to Cython
Brief Introduction to CythonBrief Introduction to Cython
Brief Introduction to CythonAleksandar Jelenak
 
Introduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationIntroduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationKevin Keraudren
 
Python OpenCV Real Time projects
Python OpenCV Real Time projectsPython OpenCV Real Time projects
Python OpenCV Real Time projectsAmarjeetsingh Thakur
 
The low level awesomeness of Go
The low level awesomeness of GoThe low level awesomeness of Go
The low level awesomeness of GoJean-Bernard Jansen
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringSri Harsha Pamu
 
defense
defensedefense
defenseQing Dou
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaN Masahiro
 
Android Refactoring
Android RefactoringAndroid Refactoring
Android RefactoringGodfrey Nolan
 

Was ist angesagt? (12)

Concurrency in Python4k
Concurrency in Python4kConcurrency in Python4k
Concurrency in Python4k
 
C mcq practice test 2
C mcq practice test 2C mcq practice test 2
C mcq practice test 2
 
A peek on numerical programming in perl and python e christopher dyken 2005
A peek on numerical programming in perl and python  e christopher dyken  2005A peek on numerical programming in perl and python  e christopher dyken  2005
A peek on numerical programming in perl and python e christopher dyken 2005
 
Brief Introduction to Cython
Brief Introduction to CythonBrief Introduction to Cython
Brief Introduction to Cython
 
Introduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationIntroduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimization
 
Python OpenCV Real Time projects
Python OpenCV Real Time projectsPython OpenCV Real Time projects
Python OpenCV Real Time projects
 
project_2
project_2project_2
project_2
 
The low level awesomeness of Go
The low level awesomeness of GoThe low level awesomeness of Go
The low level awesomeness of Go
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational Engineering
 
defense
defensedefense
defense
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoya
 
Android Refactoring
Android RefactoringAndroid Refactoring
Android Refactoring
 

Ă„hnlich wie Code readability in r

The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project AnalyzedPVS-Studio
 
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingThe Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingAndrey Karpov
 
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingThe Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingPVS-Studio
 
C multiple choice questions and answers pdf
C multiple choice questions and answers pdfC multiple choice questions and answers pdf
C multiple choice questions and answers pdfchoconyeuquy
 
How to avoid bugs using modern C++
How to avoid bugs using modern C++How to avoid bugs using modern C++
How to avoid bugs using modern C++PVS-Studio
 
C# console applications.docx
C# console applications.docxC# console applications.docx
C# console applications.docxMehwishKanwal14
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio
 
introduction to python in english presentation file
introduction to python in english presentation fileintroduction to python in english presentation file
introduction to python in english presentation fileRujanTimsina1
 
The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++Alexander Granin
 
COCOMO MODEL
COCOMO MODELCOCOMO MODEL
COCOMO MODELmovie_2009
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs ChromiumAndrey Karpov
 
Operator C# - Lec3 (Workshop on C# Programming: Learn to Build)
Operator  C# - Lec3 (Workshop on C# Programming: Learn to Build)Operator  C# - Lec3 (Workshop on C# Programming: Learn to Build)
Operator C# - Lec3 (Workshop on C# Programming: Learn to Build)Jannat Ruma
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Languagesatvirsandhu9
 
Analysis Of Open Positions In Data Science
Analysis Of Open Positions In Data ScienceAnalysis Of Open Positions In Data Science
Analysis Of Open Positions In Data ScienceDaniel Cuneo
 
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptxINDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptxAbhimanyuChaure
 
C++ Certified Associate Programmer CPA
C++ Certified Associate Programmer CPAC++ Certified Associate Programmer CPA
C++ Certified Associate Programmer CPAIsabella789
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)ExcellenceAcadmy
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)ExcellenceAcadmy
 

Ă„hnlich wie Code readability in r (20)

C++ training day01
C++ training day01C++ training day01
C++ training day01
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project Analyzed
 
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingThe Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and Everything
 
The Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and EverythingThe Ultimate Question of Programming, Refactoring, and Everything
The Ultimate Question of Programming, Refactoring, and Everything
 
C multiple choice questions and answers pdf
C multiple choice questions and answers pdfC multiple choice questions and answers pdf
C multiple choice questions and answers pdf
 
How to avoid bugs using modern C++
How to avoid bugs using modern C++How to avoid bugs using modern C++
How to avoid bugs using modern C++
 
C# console applications.docx
C# console applications.docxC# console applications.docx
C# console applications.docx
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's code
 
introduction to python in english presentation file
introduction to python in english presentation fileintroduction to python in english presentation file
introduction to python in english presentation file
 
The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++
 
COCOMO MODEL
COCOMO MODELCOCOMO MODEL
COCOMO MODEL
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs Chromium
 
PVS-Studio vs Chromium
PVS-Studio vs ChromiumPVS-Studio vs Chromium
PVS-Studio vs Chromium
 
Operator C# - Lec3 (Workshop on C# Programming: Learn to Build)
Operator  C# - Lec3 (Workshop on C# Programming: Learn to Build)Operator  C# - Lec3 (Workshop on C# Programming: Learn to Build)
Operator C# - Lec3 (Workshop on C# Programming: Learn to Build)
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Analysis Of Open Positions In Data Science
Analysis Of Open Positions In Data ScienceAnalysis Of Open Positions In Data Science
Analysis Of Open Positions In Data Science
 
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptxINDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
INDIAN INSTITUTE OF TECHNOLOGY OF KANPURESC 111M Lec03.pptx
 
C++ Certified Associate Programmer CPA
C++ Certified Associate Programmer CPAC++ Certified Associate Programmer CPA
C++ Certified Associate Programmer CPA
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)
 

KĂĽrzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 

KĂĽrzlich hochgeladen (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
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
  • 6. #5 Power of Style A B
  • 7. #6 Rank the following statements! A B C
  • 8. A B #7 Which is easier to understand?
  • 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))