Maintainable Code: Function Lifecycles
Standardized Intelligible Maintainable Contextualized
| Stage | Description |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
best practices, debugging, independent learning
2026-01-06
![]()
![]()
Questions > Answers
Knowing how to ask the right questions to solve problems and learn independently is legitimately, indisputably more valuable than the party trick of recalling lots of functions and language-specific processes.
The best way to troubleshoot is to avoid it.
![]()
Standardized
![]()
Intelligible
![]()
Maintainable
![]()
Contextualized
Standardized Intelligible Maintainable Contextualized
Standardized code is easier to read, understand, and maintain
R is pretty forgiving
Choose to be cautious
Style Guides
Pick one and stick to it
Standardized Intelligible Maintainable Contextualized

Standardized Intelligible Maintainable Contextualized
Keep Collaboration in Mind
Be kind not just to others who may work with you, but also to future-you.
Natural languages > programming languages
The language you speak is infinitely more intuitive, nuanced, specific, and adaptable. Take advantage of it.

Standardized Intelligible Maintainable Contextualized
![]()
Names should describe the named thing.
![]()
“There are only two hard things in Computer Science: cache invalidation and naming things.”
— Phil Karlton

Keep it simple, stupid – commitstrip.com
Standardized Intelligible Maintainable Contextualized
| Stage | Description |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Standardized Intelligible Maintainable Contextualized
⇄ How much could your data change?
⇆ How similar are your data to other data?
Standardized Intelligible Maintainable Contextualized
Avoid
Promote
Standardized Intelligible Maintainable Contextualized
When should you copy and paste?
Standardized Intelligible Maintainable Contextualized
## Only works on my computer no matter what
absolute_path <- "Users/Natalie/repos/d2mr/example-repo/images/barplot.jpg"
# Works on your machine if you clone it to this location on your computer
mixed_path <- "~/repos/d2mr/example-repo/images/barplot.jpg"
# Works when you clone the repo anywhere
relative_path <- "/images/barplot.jpg"Standardized Intelligible Maintainable Contextualized
# Always equals 3
my_mean <- (2 + 4)/2
# Output will change to reflect changes to 2 input variables
x <- 2; y <- 4 # or...
x <- 3; y <- 10
the_mean <- (x + y)/2
# Output will change no matter how many input numerals are averaged
number_list <- c(2,3) # or...
number_list <- 2:8 # or...
number_list <- c(1,2,8,100)
a_mean <- sum(number_list)/length(number_list)Standardized Intelligible Maintainable Contextualized
Know your community and follow its conventions.
Standardized Intelligible Maintainable Contextualized
The best code is the code that works.
Best practices may not be practical in practice.
Your project’s goals come first.
Standardized Intelligible Maintainable Contextualized




Standardized Intelligible Maintainable Contextualized
![]()
The quality of output of any system is determined by the quality of the input.
Immaculate code can’t make up for horrendous data.
Neither code nor data can fix terrible human decisions.
![]()
Your data are bad because they are:
Your decisions are bad because you have:
Your workflow:
Commit little & commit lots

Thou shalt:
Remember the whole point of GitHub is version control!
Each assignment has a unique repo. Each repo is a unique files.
Never create multiple copies of the same files!!!
Use comments liberally…
<!-- This is a comment that readr and quarto will ignore --># This will be ignored by R when it runs the chunks while knittingChunks should…
![]()
Do not use the visual editor!!
![]()
Name your chunks to minimize human error. They should be:
fig-gesture-stacked-bar not gesture-graph1 Chunk 1 Thing
Unique and informative chunk names depend on each chunk doing 1 and only 1 thing. Can you explain what the point of the chunk is in 1-3 words? If not, you’ve probably got 1 chunk doing too many things.
Step 1 for any problem: Look it up.
Looking up documentation for any of the *_join() functions in dplyr brings you to a shared page for all of them.
Function name: here inner_join()
Required arguments: here x and y; listed first; no assigned default value; omitting results in an error message
Optional arguments: here by = NULL, copy = FALSE, suffix = c(".x", ".y"), keep = NULL; listed after required arguments; omitting will use the assigned default value
Other parameters: always ...; accepts extra arguments not explicitly listed
Related reference: here left_join() ; functions that share a basic structure and may have helpful documentation
Mutating joins add columns from y to x, matching observations based on the keys. There are four mutating joins: the inner join, and the three outer joins.
Inner join An inner_join() only keeps observations from x that have a matching key in y.
…text omitted…
No one will help you until you at least try.

Artwork by @allison_horst
Errors starting with these phrases are super common and have predictable causes:
could not find function: the function’s package is not loaded (or more commonly, typos)Error in if: non-logical data or missing values passed to R’s if conditional statementError in eval: references to objects that don’t existcannot open: attempts to read a file that doesn’t exist or can’t be accessedno applicable method: using an object-oriented function on a data type it doesn’t supportsubscript out of bounds: trying to access an element or dimension that doesn’t existpackage errors: R cannot install or load a package due to missing dependencies or conflictsBut my error message isn’t common!
My error is common but the solution isn’t working!
I have literally no idea what this error message means!
GOOGLE IT.
List from How to: Interpret Common Errors in R
Errors must be addressed
R says: “Something has gone wrong. I cannot and will not move forward.”
Warnings should usually be addressed
R says: “This doesn’t look right but I’m just a robot what do I know.”
Messages are informational only
R says: “No problem, done! But just FYI…”
Not all alarming words and big red Xs are problems.



There is a problem, but where is the problem?
RStudio Debugging Shortcut
Cmd + ReturnCmd + Option + CShift + Cmd + ReturnWindows: Cmd=Ctrl, Option=Alt, Return=Enter

Step 1:
Beg, borrow, steal, buy, fabricate or otherwise obtain a rubber duck* (bathtub variety).
Step 2:
Place rubber duck on desk and inform it you are just going to go over some code with it, if that’s all right.
Step 3:
Explain to the duck what your code is supposed to do, consider that the duck may need some specifics, and then go into detail and explain your code line by line.
Step 4:
At some point you will tell the duck what you are doing next and then realize that is not in fact what you are actually doing.
Step 5:
Enjoy as the duck sit there serenely, happy in the knowledge that it has helped you on your way.
![]()
In a pinch a colleague or trusted human might be able to substitute for the duck, however, it is often preferred to confide mistakes to the duck instead of the human.
I think the problem, to be quite honest with you, is that you’ve never actually known what the question is.
Douglas Adams
How do I ask a good question?
How to Ask Questions the Smart Way
How do I get someone to answer?
MRE Ingredients
data() for more).How do I get started with really complex packages, like ggplot2, stats, and psych?
How do I find an existing function or package that I need when I don’t even know whether such a thing exists?
You keep throwing around words like I’m supposed to know it already, and then you say if I don’t know it, I should do some other thing I don’t know. Not fair.
Know the outcome you want and work backwards. You don’t need to fully understand everything. What do you need to know? What are the questions to ask to get those answers? Where can you turn with those particular questions?
Read relevant documentation.
Identify any error messages or warnings and attempt to resolve them. ️
Confirm that your data and code are playing together.
Review your code at a granular level (line-by-line, rubber ducking).
Identify as specific issues and generate specific questions.
Use community resources (e.g., Slack, StackOverflow, Reddit) to find answers.
Email your TA. ️
Email your professor. ️
D2M-R I | Week 1