The profiler told me I wrote some useless code (An Example of Defensive Programming)


When your code has performance bottlenecks, you can use the profiler software to benchmark your code, which will reveal the potential problems in your code or modules (memory or time consuming)

Recently, my Profiler told me that I wrote a few lines of useless code.

aqtime-profiler The profiler told me I wrote some useless code (An Example of Defensive Programming) profiler refactoring software design software development

Recently, my Profiler told me that I wrote a few lines of useless code.

As seen in the screenshot, the leftmost numbers are calling counters, the middle columns are time in milliseconds, and the rightmost columns are also time but with the sub-routines. Those lines are safety checks to prevent the crash due to possibly out-of-range errors.

Obviously, the statistics show that these exit conditions are never met. Removing these save 2-3 seconds execution time. The method of adding safety checks (e.g. check object for null), are often seen as a way to handle the exception softly. Some might argue that it is a way to hide the errors. It is always better to throw the exceptions as early as you can instead of hiding them, because they will eventually come back to bite you in the ass.

You may also like: 性能评估软件说我写了几行无用的代码

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
283 words
Last Post: A Better SteemIt Voting Strategy with Back Tracking Algorithm
Next Post: Code Review - Bound Parameter

The Permanent URL is: The profiler told me I wrote some useless code (An Example of Defensive Programming)

Leave a Reply