coptimization

Should we disable compiler optimization until the program is bug free?


Sometimes compiler optimization hides errors; for exemple:

double val = sin(1.5);

If the compiler optimization is enabled although the math library is not linked this will compile since the compiler will calculate the value of sin(1.5) and replace it.

Is it a good practice to disable compiler optimization until the program is bug free?


Solution

  • Since nobody wrote it up as an answer, I take a shot.

    Is it a good practice to disable compiler optimization until the program is bug free?

    I wouldn't recommend it. Instead, I would regularly test my code in release mode as well (with optimizations enabled). I have had some bugs personally and I have seen many cases where the code worked beautifully in debug mode but crashed or produced weird things in release mode. (Some of the latter bugs were stack corruption related.)

    The sooner you realize that you have such a bug, the better. You will probably have an easier time finding it while your memories of the code are still fresh.

    Another thing I have seen is bugs due to side effects in code snippets that only run in debug mode. These are clearly the developer's mistake but the sooner you notice it, the more likely that you will have an easier time fixing it.

    Developing in release mode unless I need the debugger seems a little over the top to me. In debug mode you may get many useful checks in your third party libraries which in turn reduces your time spent on debugging.

    In short: develop in debug mode but test regularly in release mode.