Stepping Through Your Code

Sometimes the only way to figure out a problem in your code is to actually run it line-by-line until you see where it goes wrong. You can use any of the methods above to stop your code, but there's nothing like getting your mind right into the logic by stepping through the code one line at a time.

The basic way to step through code is to use Debug..Step Into (F8). This debug command is the most common one to use because it's so basic. It runs the line of code that is highlighted, displays the next line that will be run, and awaits your next command. The Step Into (and other Step commands) is shown in Figure 8-11.

Sometimes the basic nature of Step Into is a problem. If the highlighted line of code is a call to another procedure (either a Function or a Sub), Step Into will do just that—it will dive into that procedure and highlight its first line.

Now, maybe this is just what you want. But if you are following good programming practices, like the coupling and cohesion guidelines earlier in this chapter, you have lots of small, fully tested functions that will be supremely boring and laborious to step through. After all, you know the error isn't in one of those, right?

The answer to this little problem is to use a cousin of Step Into, called Step Over (Shift-F8). This name isn't quite accurate, because when you use it the highlighted line of code isn't really stepped over, it's actually stepped through. The line of code that's highlighted will run, even if it is a call to one of your functions or subs, and then the next line will be highlighted. The entire function or sub will run without stopping, so you don't have to step through all that boring code.

Also note that Step Over works exactly the same as Step Into for a normal line of code (not a call to another procedure). This means that you can get into the habit of leaning on the Shift key when you use F8, and you'll never need to step through called procedures unless you want to.

What if you forget to use Step Over and use Step Into by accident? Hope is not lost. By using the often-forgotten Step Out (Ctrl-Shift-F8), you can run the remainder of the current procedure without stopping, and automatically stop on the very next line after your code returns to the calling procedure.

Figure 8-11
0 0

Post a comment