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

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
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
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
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
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
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
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
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
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Último (20)

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
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
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
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
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
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
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

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