Sunday, March 27, 2016

Module 11 : Debugging!

This module is perhaps one of the most helpful to a new programmer. At least for me it is, because my code is usually filled with bugs, and I spend a lot of time working through the code to resolve the errors and get it up and running. The study materials explored a variety of different ways to conduct sound debugging, and I appreciate that because it's good to have options. That said, nothing really seemed to work on the code we were presented in this assignment. It's highly probable that this is due to the fact that i'm new at R and wasn't entirely successful implementing those debugging steps!

As you're well aware, we started out with the following code:

tukey_multiple <- function(x) { 
   outliers <- array(TRUE,dim=dim(x)) 
   for (j in 1:ncol(x)) 
    { 
    outliers[,j] <- outliers[,j] && tukey.outlier(x[,j]) 
    } 
outlier.vec <- vector(length=nrow(x)) 
    for (i in 1:nrow(x)) 
    { outlier.vec[i] <- all(outliers[i,]) } return(outlier.vec) }

I set X equal to 5 and input the code into R Studio. This is the error message I received:

Error: unexpected symbol in:
"    for (i in 1:nrow(x)) 
    { outlier.vec[i] <- all(outliers[i,]) } return"

How disappointing! I actually did a lot of research at this point into the error itself, looking over several entirely unhelpful posts from Stack Overflow. The good news is that in doing that kind of digging/browsing for information, you often learn about other aspects that maybe weren't part of the assignment. I don't mind this phase of research because it sparks my interest and keeps me engaged.

After awhile I decided to clean up my code:

tukey_multiple <- function(x) { 
   outliers <- array(TRUE,dim=dim(x)) 
   for (j in 1:ncol(x)) 
    { 
    outliers[,j] <- outliers[,j] && tukey.outlier(x[,j]) 
    } 
outlier.vec <- vector(length=nrow(x)) 
    for (i in 1:nrow(x)) 
    { 
outlier.vec[i] <- all(outliers[i,])

return(outlier.vec)

This code yields no errors in R Studio and so I can only assume the bug was resolved by making sure everything was on its own line where appropriate. The brackets were crowding other lines of code and I think this is what may have been causing the error in the first place. If there is something more that should have been done, please let me know! I was sort of expecting to see some kind of result in R Studio other than a simple return without an error message, but I'm not sure what else I may be missing.

No comments:

Post a Comment