Best practice for managing debug/instrumentation code

조회 수: 1 (최근 30일)
Hilmar Lehnert
Hilmar Lehnert 2021년 2월 17일
답변: Steven Lord 2021년 2월 17일
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.

답변 (1개)

Steven Lord
Steven Lord 2021년 2월 17일
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.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by