Setting a variable when a breakpoint hits

Occasionally I run into a problem difficult to reproduce in that it takes a laborious amount of tinkering.  Sometimes I’ve gone through the work of plugging away at the user interface or forcefully moving the current statement manually around if statements. In these situations,  I become very attached to my debugging session (no pun intended).

I am secure in my nerdiness enough to admit that there have been instances where I have changed code for testing purposes and forgotten to revert those changes before checkin time. Because of this I’ve developed an aversion to making such changes to the code. I recently discovered one additional tool in my arsenal to futz with the running code without changing it.

Abuse a breakpoint condition!

Breakpoint

You would usually use a condition to specify when the breakpoint should hit, but you can enter any valid code as the condition. In my case, I used this:

     (addressState = "AL") == null

The null check was just to ensure that the breakpoint would not actually break the debugger. You could use “!= null” if you still wanted the breakpoint to hit.

Ultimately this aids the same benefit you get when setting values in local or watch windows. Using this trick, you can “automate” these actions if you need to.

This article is part of the GWB Archives. Original Author: jkauffman

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.