Dealing with dead C code

Browsing the OpenBSD source (yes, I use git for that because CVS is horrible – don’t deny it!), I find a lot of spots doing things like the following, which is just a minor variation to the “#if 0” idea :

void 	something(void);

int
main(void)
{
#ifdef notyet
	something();
#endif

	return (0);
}

What both of these constructs do is make the preprocessor skip these lines of code, which is ok for the heat of the moment. But in said OpenBSD examples, code can be forgotten or changed at any time, which makes it hard to track outside changes. If we rename something() to something_else(), the code breaks silently. If the code was complicated and the changes are massive, anyone working on reviving that dead piece of code will have a very hard time.

To pass the code to the compiler, while maintaining its integrity but not modifying the code behaviour or the resulting binary code, we can do the following:

void	something_else(void);

int
main(void)
{
	if (0) { /* XXX put useful comment here */
		something();
	}

	return (0);
}

Be careful if you use tools like indent(1). And, of course, this only works in functions, but it keeps you from wrecking the code too much. The semantics can still change and make the dead code do wrong stuff, but it’s better than nothing. If you need “notyet” outside of functions, you will probably need do stick it in different places and files simultaneously as headers normally harbour prototypes and structures, but source files the actual code.

Frankly, don’t ever add code you don’t want to do run yet. Use comments with TODO, FIXME, or XXX to state what you want to do verbally. And don’t comment out code you don’t need for now. You will most likely never need it again after it’s going into the repository that way. Trust your repository and learn its capabilities. It doesn’t forget. It helps you to remember. And also don’t flood it with useless history. It doesn’t like that very much.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.