Debugging and Defensive Programming in R

In this week’s assignment, I explored debugging techniques using a function with a deliberate bug. The function tukey_multiple() was designed to detect outliers across matrix columns using a helper function that didn’t exist (tukey.outlier()), and it also used the wrong logical operator (&& instead of &). I followed the debugging strategies suggested in Module 11, including debug(), traceback(), and interactive step-by-step testing. These tools helped me identify the problem and correct the logic and the missing function definition.

After locating the issues, I rewrote the function with a clear and practical solution, adding a defensive programming check to alert the user if tukey.outlier() hasn’t been defined. This helped reinforce the importance of writing code that works and handles errors gracefully. Debugging this way taught me to break down a problem, stay focused, and think ahead while building reliable R functions.

Please 📂Check out my complete code on GitHub!

Thank you for visiting my blog!

References:
Matloff, N. (2011). The art of R programming: A tour of statistical software design. No Starch Press. Chapter 13.

R-Bloggers. (2014, October 17). Functions and debugging in R. https://www.r-bloggers.com/2014/10/functions-and-debugging-in-r/

Posted by Sardys Avile-Martinez | April 5, 2025
For more reflections on R programming, data science, and debugging with style, follow along at Stat and Analytics

One thought on “Debugging and Defensive Programming in R

Leave a comment