Debugging AutoLISP programs is "A Zen like Experience," said no one ever... Although, I have tried to convince a number of people that it is very a calming and soothing activity over the years. There are a number of different debugging techniques and tools that can be used to find and handle errors much easier, whether you are developing in AutoCAD on Windows or Mac OS.
Note: There are more debugging tools available on Windows than Mac OS at this time, which could change in the future. If you do plan on developing a number of AutoLISP programs, you might want to consider getting access to the Windows version of AutoCAD if you are a user of Mac OS. Just a simple suggestion, not something you must do.
The thought of debugging and error handling doesn't come naturally to new and even seasoned developers, as most are simply focused on solving the problem at hand and not all the extra baggage that often goes with well written code. Of course, well written code is in the eye of the developer that is look at it in the current moment. Also if it isn't broken, then why change or fix a process\approach to a solution? After many years of writing custom programs, I have learned that debugging and error handling need to be some of the very first things to consider when writing any custom program.
While I am just as guilty for jumping in (feet, hands, and compiler first) and writing code, this approach can get you to the solution faster but can lead to some bad habits and headaches down the road. One of the things that I have found useful in getting me to slowdown and think things through more clearly is the creation of a flowchart. If you are not already creating one today, I recommend creating a flowchart to help you visualize all the logical paths through your programs. Flowcharts make it easier to identify choices as well as potential failure points that need to be handled. A flowchart can also be a great way to communicate the design of a program with other that are less technical. Products like Microsoft Visio or Lucidchart are designed to create flowcharts, but the same can be accomplished with Microsoft Word, PowerPoint, and other word processor and presentation products.
Potential failure points can be introduced when a:
- User has an opportunity to provide input
- Global variable is used
- Program relies on a specific folder structure or an external data/support file, such as a drawing or text file or Windows registry entries
- Program utilizes an ActiveX/COM or external library
AutoLISP/Visual LISP offers a variety of built-in functions and features to help you track down problems and handle errors. You can utilize the following debugging and error handling functions and features when developing your custom programs:
- Basic debug messaging - AutoLISP functions
- Basic function tracing facilities - AutoLISP functions
- Visual LISP IDE (Windows only) - Breakpoints, watch window, inspect window, and trace stack
- Data validating functions - AutoLISP functions
- Error catching - AutoLISP functions
- Custom error handling - AutoLISP functions
- Undo grouping - AutoCAD UNDO command allows you to begin and end an undo group so that all changes made by your program can be reverted using the AutoCAD U command
Hope you find this overview topic helpful, I plan on following up with future articles that dive more into specifics about each debugging technique and tool that I mentioned.