SlideShare uma empresa Scribd logo
1 de 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))

Mais conteúdo relacionado

Mais procurados

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
 
Introduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationIntroduction to cython: example of GCoptimization
Introduction to cython: example of GCoptimizationKevin Keraudren
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringSri Harsha Pamu
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaN Masahiro
 

Mais procurados (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
 

Semelhante a 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
 
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
 

Semelhante a 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)
 

Último

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 

Último (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 

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))