Question

The more I program the more I realize that most of my time is spent writing scaffolding for programs so that I can debug them and then strip away the scaffolding for production.

The problem is that I haven't found a language where the idea of scaffolding is a built-in notion. Ideally I want the scaffolding to be part of the actual program that gets stripped away for production deployments instead of being sections of code that are commented and un-commented whenever I'm debugging a problem.

An example of what I'm talking about in some made-up pseudo-language

def scaffold(f, *args)
  # ... pre condition verifications and state logging
  f(*args)
  # ... post condition verifications and state logging
end

@scaffold
def buggy_function(a, b, c)
  # ... some buggy code
end

This looks a little bit like python decorators but the semantics is different. When running in production @scaffold literally dissolves from the AST and is nowhere to be found. Some of you might argue that I've created a very bloated and ad-hoc kind of type system but it's not quite that. I'll give an example to demonstrate.

I had written a parser for single and double quoted strings and it was failing somewhere in the middle of parsing another expression. The reason it was failing was because I wasn't handling the empty case ('') properly and I'm not aware of any type system that would have been able to verify the validity of the state-machine for the string parser to verify that the empty case was handled properly. The only way to uncover this would have been to write tests and even then getting a reduced test case would have required the kind of scaffolding I had to put in place to pinpoint the problem. Plus, you can easily imagine other scenarios where neither the type system nor tests would have hit the bug.

I haven't figured out a good way to manage the kind of scaffolding I need during debugging and an easy way to get rid of it once I'm done debugging. How do people usually handle it?

Was it helpful?

Solution

You are by far not the first person to have this need; many others have had it before you, so there has been quite a bit of research on the subject, and a solution in almost every decent language out there.

For a theoretical discussion, you might want to look at Programming by Contract, preconditions, postconditions and invariants. It includes a list of languages with native support for that stuff, and also a list of third-party support libraries for languages that do not have native support.

As far as rolling your own is concerned:

(Because what good is it if it is "not invented here"?)

  • in C and C++ you have #ifdef;
  • in C# you have #if and also the [Conditional] attribute;
  • in java you have the built-in assertion keyword and also plain old if() which, when controlled by a constant, supposedly gets optimized out.

All these mechanisms can be used to achieve what you want to achieve.

OTHER TIPS

Some languages (Including Delphi where I used it for this very purpose) have the concept of setting variables that can be checked at compile time so you can use an "IFDEF debug" directive to surround your scaffolding code and define / undefine debug appropriately.

IIRC you would set the variables in a dialog at the program level.

If your language is a bit more manual you should still be able to define a "debug" variable early on and do an IFDEF check for it throughout your code.

Licensed under: CC-BY-SA with attribution
scroll top