Lecture 3 - Functions, Scoping and Software Development Flashcards
Advantages of creating functions
Constructing - does only one thing
Testing - does only one thing
Debugging - does only one thing
Maintenance - less duplication, modificiations easy to make
Expansion/enhancement - improving a function makes improvement available to all code using function
Re-use - general functions can built into warehouse of tools
Disadvantages of creating functions
More planning required before coding
Slight execution time cost
What are environments?
Structures that organise objects in an R-programme
What do environments do?
Associates a set of names to a set of values, perhaps pointing to same object address
What are the two types of environments?
Global and current environments
What happens to the environment when packages are loaded?
They are added as parents of the global environment
What are the scoping rules?
Name making
Function making
Fresh start
Where to look for values associated with objects
What happens if a name isn’t defined in an environment?
R looks up one level in the environment structure
What happens to a functions environment for each new use?
It is wiped clean
When does a lookup of names happen?
At the time the code is executed, not created
Why should you test code?
Nobody writes perfect code
Testing can demonstrate sections of code working properly
Reduces possible suspects for further debugging
What are three different debugging methods?
Compile time
Batch debugging
Interactive debugging
Compile time debugging method
Only errors found here are syntax or for some languages, whether variables have been initialised for use.
What is batch debugging?
Peppering code with print( ) statements.
Run code, examine output to find unexpected results. Clumsy approach as code needs to be sanitised of print( ) after errors found)
What is interactive debugging?
Set breakpoints, step through code, examine and alter variables while code pauses.