11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 29: Ten Ways to Avoid Adding Bugs to Your Program 381Now an out-of-range index will be caught by the check. (Making indexunsigned precludes the necessity of adding a check for negative indexvalues.)Commenting Your CodeWhile You Write ItYou can avoid errors if you comment your code as you write it rather thanwaiting until everything works and then go back and add comments. I canunderstand not taking the time to write voluminous headers and functiondescriptions until later, but you always have time to add short commentswhile writing the code.Short comments should be enlightening. If they’re not, they aren’t worthmuch. You need all the enlightenment you can get while you’re trying to makeyour program work. When you look at a piece of code you wrote a few daysago, comments that are short, descriptive, and to the point can make a dramaticcontribution to helping you figure out exactly what it was you weretrying to do.In addition, consistent code indentation and naming conventions make thecode easier to understand. It’s all very nice when the code is easy to readafter you’re finished with it, but it’s just as important that the code be easy toread while you’re writing it. That’s when you need the help.Single-Stepping EveryPath at Least OnceIt may seem like an obvious statement, but I’ll say it anyway: As a programmer,it’s important for you to understand what your program is doing.Nothing gives you a better feel for what’s going on under the hood thansingle-stepping the program with a good debugger. (The debugger in bothDev-C++ and Visual Studio.NET work just fine.)Beyond that, as you write a program, you sometimes need raw material inorder to figure out some bizarre behavior. Nothing gives you that materialbetter than single-stepping new functions as they come into service.Finally, when a function is finished and ready to be added to the program,every logical path needs to be traveled at least once. Bugs are much easier to

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!