English 中文(简体)
How to avoid debugger-only variables?
原标题:

I commonly place into variables values that are only used once after assignment. I do this to make debugging more convenient later, as I m able to hover the value on the one line where it s later used.

For example, this code doesn t let you hover the value of GetFoo():

return GetFoo();

But this code does:

var foo = GetFoo();
return foo; // your hover-foo is great

This smells very YAGNI-esque, as the functionality of the foo s assignment won t ever be used until someone needs to debug its value, which may never happen. If it weren t for the merely foreseen debugging session, the first code snippet above keeps the code simpler.

How would you write the code to best compromise between simplicity and ease of debugger use?

最佳回答

I don t know about other debuggers, but the integrated Visual Studio debugger will report what was returned from a function in the "Autos" window; once you step over the return statement, the return value shows up as "[function name] returned" with a value of whatever value was returned.

gdb supports the same functionality as well; the "finish" command executes the rest of the current function and prints the return value.

This being a very useful feature, I d be surprised if most other debuggers didn t support this capability.

As for the more general "problem" of "debugger-only variables," are they really debugger-only? I tend to think that the use of well-named temporary variables can significantly improve code readability as well.

问题回答

Another possibility is to learn enough assembly programming that you can read the code your compiler generates. With that skill, you can figure out where the value is being held (in a register, in memory) and see the value without having to store it in a variable.

This skill is very useful if you are ever need to debug an optimized executable. The optimizer can generate code that is significantly different from how you wrote it such that symbolic debugging is not helpful.

Another reason why you don t need intermediate variables in the Visual Studio debugger is that you can evaluate the function in the Watch Window and the Immediate window. For the watch window, just simply highlight the statement you want evaluated and drag it into the window.

I d argue that it s not worth worrying about. Given that there s no runtime overhead in the typical case, go nuts. I think that breaking down complex statements into multiple simple statements usually increases readability.

I would leave out the assignment until it is needed. If you never happen to be in that bit of code, wanting a look at that variable, you haven t cluttered up your code unnecessarily. When you run across the need, put it in (it should be a trivial Extract Variable refactoring). And when you re done with that debugging session, get rid of it (Inline Variable). If you find yourself debugging so much - and so much at that particular point - that you re weary of refactoring back and forth, then think about ways to avoid the need; maybe more unit tests would help.





相关问题
Eclipse: Hover broken in debug perspective

Since upgrading Eclipse (Galileo build 20090920-1017), hover in debug no longer displays a variable s value. Instead, hover behaves as if I were in normal Java perspective: alt text http://...

IIS 6.0 hangs when serving a web-service

I am having issues with one of our web-services. It works fine on my development machine (win XP) whether I host it as a separate application or using cassini from Visual studio. Once I deploy on the ...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Asp.Net MVC - Problem returning a model to the view

I ve recently started using the Areas functionality of the Preview2 and it was working ok until I needed to return a Model to the view. Controller: public ActionResult ForgotPassword() { return ...

Unable to generate PDB files in Visual Studio 2005

For most assemblies, I m able to generate their respective .pdb files. However, I ve got one project that isn t generating their .pdb files. I ve made sure that it on debug mode and that the project ...

热门标签