Comment Out Large Blocks of Code with Nested Comments

In working with a large chunk of code recently, I needed to comment out a sizeable block to verify something was working as expected. My first thought was to simply to embed the offending code within /* and */

/*
  // Detect touch anywhere
  UITouch *touch = [touches anyObject];
 
  // Where is the point touched
  CGPoint point = [touch locationInView:self.view]; 
  NSLog(@"pointx: %f pointy:%f", point.x, point.y);
 
  // Was a tab touched, if so, which one...
  if (CGRectContainsPoint(CGRectMake(1, 440, 106, 40), point))
	  NSLog(@"tab 1 touched");
  else if (...)
    ... more code here
  else if (...)
    ... more code here
 
*/

This approach works well, with one exception, nested comments in the form of /* … */ will generate a compiler error, see lines 9 – 10 below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
  // Detect touch anywhere
  UITouch *touch = [touches anyObject];
 
  // Where is the point touched
  CGPoint point = [touch locationInView:self.view]; 
  NSLog(@"pointx: %f pointy:%f", point.x, point.y);
 
  /* If you have an embedded comment in this style,
       you will get a compiler error */
  if (CGRectContainsPoint(CGRectMake(1, 440, 106, 40), point))
	  NSLog(@"tab 1 touched");
  else if (...)
    ... more code here
  else if (...)
    ... more code here
 
*/

To get around the problem with embedded comments, you can try this approach:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#if 0
  // Detect touch anywhere
  UITouch *touch = [touches anyObject];
 
  // Where is the point touched
  CGPoint point = [touch locationInView:self.view]; 
  NSLog(@"pointx: %f pointy:%f", point.x, point.y);
 
  /* If you have an embedded comment in this style,
       you will get a compiler error */
  if (CGRectContainsPoint(CGRectMake(1, 440, 106, 40), point))
	  NSLog(@"tab 1 touched");
  else if (...)
    ... more code here
  else if (...)
    ... more code here
 
#endif

Another option that is a little more descriptive:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifdef VISIBLE
  // Detect touch anywhere
  UITouch *touch = [touches anyObject];
 
  // Where is the point touched
  CGPoint point = [touch locationInView:self.view]; 
  NSLog(@"pointx: %f pointy:%f", point.x, point.y);
 
  /* If you have an embedded comment in this style,
       you will get a compiler error */
  if (CGRectContainsPoint(CGRectMake(1, 440, 106, 40), point))
	  NSLog(@"tab 1 touched");
  else if (...)
    ... more code here
  else if (...)
    ... more code here
 
#endif

This approach provides a means to quickly comment in/out any number of code blocks by simply modifying a compiler directive as shown here:

// Code above will be compiled
// Change to INVISIBLE and code will ~not~ be compiled
#define VISIBLE
  1. I’ve found occasion to do something like this:

    // defined in some common file for use in all modules
    #ifdef DEBUG_BUILD
    #define IGNORE_IN_DEBUG (0) // will cause the code to be ignored in debug build
    #else
    #define IGNORE_IN_DEBUG (1/0) // will cause a divide by zero error in a non-debug build
    #endif

    // to bracket the code you want to omit
    #if IGNORE_IN_DEBUG
    #endif

    That encourages you to clean it up before progressing to a release build, assuming that you do release builds only rarely.

    Alternatively/in addition, you could probably add a build step that looks for that symbol and issues a warning if it’s found. I’m not much of an expert on build customization, though, so maybe not.

  2. Don’t forget for quick commenting a selected section in Xcode. Command-/

Comments are closed.