Hi everyone.
From times to times I read that debugging with print calls is evil.
I know about basic debugging functions like debug
, debugonce
, browser
, traceback
, trace
, but in the end I'm often just more comfortable using print
.
The problem I have with this strategy is that my code can end up cluttered with a mess of commented print calls that I have to hunt and remove when I'm done, and the one I forget to delete on time clutter the console too. I assume this is in good part why this approach is frown upon.
I've built this to get around it, which allows me to specify which category of debugging calls I want to execute (it doesn't have to be print):
options(dbg.ids = c("here","there")) # specify categories of calls
dbg <- function(id,expr){
if(id %in% getOption("dbg.ids")){
eval.parent(substitute(expr))
}
}
dbg("here", print(1)) # [1] 1
dbg("foo", print(2)) # nothing happens
I'm thinking about coding some cleanup
function that would use regex to remove all the calls from my project.
Is this a bad idea ? Are there some tools or habits that I should investigate before developing an exotic alternative ?