Best practice for managing debug/instrumentation code

3 views (last 30 days)
As I develop Matlab code I typically put in a lot of data traces, debug code, state variable tracking etc, so I can observe how the thing behaves and what's working and what isn't yet. That can slow things down a lot, but in development I typically don't care. Once the project is finished, all that stuff should not get executed anymore and ideally removed from the code.
I currenty set some 'debugTag' or 'debugLevel' variable and all debug code is wrapped in if statements that trigger off a specific debug tag or debug evel. That works ok, but it's quite messy since these if statements are all over the place and hard to rip out again.
I wonder if there is a better way . Rough requirements:
  1. Should allow the conditional execution of certain code based on a global control mechanism
  2. As much seperated from the "actual" code as possible, ideally all encapsualted in a different module/file
  3. Can easily be removed from the final product for delivery but also restored if a change request or bug fix is required
Any tips & tricks are appreciated.

Answers (1)

Steven Lord
Steven Lord on 17 Feb 2021
You could write a displayDiagnosticInfo function that you call throughout your code that displays its input(s). When you're at the point in development where that diagnostic information is no longer required to be displayed every time, comment out the body of displayDiagnosticInfo (leaving it a no-op.)
But IMO a better solution is to use the debugging tools included in MATLAB rather than the equivalent of disp statements along with testing as you go (test-driven development.) The TODO/FIXME report and/or a MATLAB Project may also be useful to you.

Categories

Find more on Software Development Tools in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!