tags:

views:

359

answers:

10

Few of us would deny the awesomeness of debuggers, but to make it more useful, some tricks can be used.

For example in Python, you can use pass to do absolutely nothing except to leave you room to put a break point and allow you to observe the values in the Watch window.

In C#, I used to do GC.Collect(), but now I use if (false){}

What's your most playful dummy line?

+11  A: 

In C#, you can use this:

System.Diagnostics.Debugger.Break();

It will force a breakpoint.

FlySwat
this is why i love s.o. :)
Haoest
+6  A: 

Debug.Assert(true);, which automatically gets compiled out of release builds.

Mark Brackett
A: 

in gcc/g++:

assert("breakpoint");

since any non-0/null value to an assert is taken as true.

or even

__asm__("nop");

at least I know there will be an instruction byte emitted for the breakpoint to occur on. ;)

Edward Kmett
+2  A: 

volatile int e = 9;

Volatile means the compiler won't remove it because I don't read the variable. '9' just so that it is non-zero. Zero is too common for my tastes.

Robert
+1 for using 9!
Gabriel Hurley
Many programmers would use 42
thepaulpage
@tehp - But that's another key press and I'm a lazy engineer! WHINE!
Robert
A: 

Not very sophisticated, but functional.

bool bp;

bp = true; //whereever I need to break.
Paul Nathan
A: 

In VB6, I use a colon, by itself. :

Brian
+1  A: 

I use

int x = 0;

I always make sure I remove this line when I'm done debugging.

Rossini
see my answer, but you should add 'volatile' to make sure the compiler doesn't remove it.
Robert
A: 

In c++ I use

Sleep(0)

sometimes, or just find a variable that's used and assign it to itself:

i = i;

+1  A: 

I've never written something specifically to break on, but if I did, I'd write

int moot;

-- Edit:

I use this one when there is some required 'out' parameter that I don't care about but must provide.

Noon Silk
A: 

True, it's strange that VS2010 debugger shifts breakpoints around if they are on an empty statement such as a semicolon. Despite what it looks like, in a debug build you can still generally put a breakpoint on an empty C# control block or semicolon, and it will get hit correctly. After being annoyed by this for a while, I just created this to use instead:

public static class Nop { public static void X() { } };

edit: To use it, put Nop.X() in your code...

Glenn Slayden
@NoonSilk, regarding the required 'out' parameter, I also use a helper function when I don't want to return a value: "return Failed(out fn_out_param);" where fn_out_param is the 'out' parameter of the containing function, and the 'Failed' function just sets the out parameter to nothing. This keeps the return statment in the function I care about to a single line.
Glenn Slayden