Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Make Each Class and Method Easy to Understand

Sure, you’re the programmer—you’re going to understand. But how about the poor guy six months from now who gets assigned a modification to your program? Do you want him to have to come to you for explanations? Or, how about you yourself six months from now? If you’re anything like me, in six months you’ve worked on so many other things that you’ve forgotten many of the details regarding how the project works.

Here’s how you can avoid the problem: make every class, every method, and every variable easy to understand. Just how are you going to do that? Here’s a list of suggestions:

  Name methods according to exactly what they do. A method that performs screen updates should be named updateScreen(). If it does another thing such as make a beep sound, rename the method to updateScreenAndBeep().
  Name variables according to exactly what they’re for. A variable that holds the count of the number of times the user has clicked the left mouse button should be named leftMouseButtonClicks. If it’s an integer (which it should be), it should be named nLeftMouseButtonClicks. If it’s a member variable, it should be named m_ nLeftMouseButtonClicks.
  Add source-code comments whenever it’s necessary to understand the code. I don’t mean only if it’s necessary for you to understand the code right after you’ve written it. I mean if you think that anyone who looks at your code would have trouble understanding it. Furthermore, I’ve had many instances in which I was working on a difficult method that I just couldn’t get right. I’ve found that if I started adding source-code comments as I went, my thinking process cleared up and the method was much easier to write.
  Indent your source code so that it’s easy to see how the logic flows. For instance, after an if statement, all source code should be indented an additional tab until the end of what’s contained in the if clause.

Read Your Source Code and Simulate It Mentally

Before you run the debugger, run your mental debugger. It’s far better for you to discover the problem by following the program’s execution. You can simulate what the program does in your mind and follow the code just as if you were running the debugger.

Why would this approach be better than running the debugger? Because you gain more value as the developer by mentally simulating the execution of the program. You can better understand exactly how the code works when you follow the code through in your mind.

The next thing you must do is watch the variables. Be aware of their initial values and where their values change. Keep tabs on the more important ones, especially the ones you suspect are involved in the unwanted behavior.

Summary

In this chapter, you’ve learned what a debugger is and why it’s so important to software development. You’ve learned how the Visual J++ debugger works.

The navigation commands such as Step Into and Step Over are important things to know when you use the debugger, and they were all covered. The debug windows are important too, and familiarity with them is essential to good debugging.

Effective debugging techniques were discussed. Besides knowing how to use the debugger, you need to know how to think through the processes.

Q&A

Q How can you make sure that a project has been compiled for debugging?

A Go to the Build, Build Configuration menu item. Make sure that Debug is selected. If you’re not sure whether all the modules have been compiled for debugging, select Build, Rebuild so that all the modules are recompiled.

Q How can you step through a program with the debugger?

A You can use several different commands. The Step Into command (F11) lets you step through source code one line at a time. The Step Over command (F10) lets you step through source code while stepping over any methods that are called.

Q How can you view the contents of variables while debugging?

A The best way is with the Watch window. Class variables and local variables can be added to this window. Variables you’ve added will display their values (contents) and their types. Local variables are best viewed in the Locals window.

Q What is the Immediate window for?

A This window allows you to evaluate expressions in immediate mode. For example, you might want to see the value of a variable or evaluate an expression with variables. Things such as nMyVariable * 5 can be evaluated.

Q What’s in the Call Stack window?

A The Call Stack window displays a list of all active procedures or stack frames for the current thread of execution.

Q How are breakpoints used?

A Breakpoints can be set anywhere in your program. When the debugger gets to a line of source code at which a breakpoint is set, it pauses execution of the program until you stop debugging or continue execution.

Q Why is debugging so important to Visual J++ development?

A Even though you designed the software and wrote the code, you might have missed some important factors that will cause your program to malfunction. This happens to the best of software developers.

Q Are there additional approaches to debugging that haven’t been mentioned in this chapter?

A The are probably as many ways to approach debuggings as there are Visual J++ programmers. Although this chapter presented some systematic approaches to debugging, your best bet is to develop your own approach that fits your way of thinking.

Review Exercises

1.  Create a program. Declare some global variables and assign them values. Declare some local variables in the init() method and assign them some values. Start the debugger and step into the init() method. Use the Watch window to view the contents of the variables.
2.  Restart the program you created for the first exercise. Step into the init() method. In the Immediate window, obtain the value of one of your variables by typing the variable name and then pressing Enter. Then type another variable (making sure it’s a number) and multiply it by 5. The Immediate window should correctly evaluate the expression variable * 5.
3.  Open the LearnToDebug program. Set a breakpoint in the initForm() method. Start the program and watch it stop at the breakpoint you set.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.