Tuesday, April 11, 2017

Super Awesome Debugging Technique with Comments

Here is a trick I learned many many years ago and it remains one of the best tools in my debugging toolbox for all programming languages. This example just happens to be in C++.


#include <iostream>


int main(int argc, const char * argv[]) {
    std::cout << "Hello\n";
    
//*
    std::cout << "Hello, World!\n";
    
/*/
    std::cout << "A different Hello!\n";
//*/
    return 0;

}

now remove the first slash in the front of the first comment and you get:


#include <iostream>


int main(int argc, const char * argv[]) {
    std::cout << "Hello\n";
    
/*
    std::cout << "Hello, World!\n";
    
/*/
    std::cout << "A different Hello!\n";
//*/
    return 0;

}


This is such a handy debugging technique.

No comments:

Post a Comment