Book Section
-----
TY JavaScript 3rd Ed.
Teach Yourself JS 1.5
Teach Yourself DHTML
Teach Yourself JS 1.3
LLWW: JavaScript

General Section
-----
Discussion Forum
Articles / Tips
JavaScript Links
About the Author
Privacy Policy
Contact Me



Other Sites
-----
Website Workshop
JavaScript Weblog

JavaScript Workshop Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Hints on: Debugging logic errors

 
Post new topic   Reply to topic    JSWorkshop Forum Index -> Articles
View previous topic :: View next topic  
Author Message
phil karras
Senior Member
Senior Member


Joined: 15 Jul 2002
Posts: 1750
Location: MD

PostPosted: Thu Oct 03, 2002 12:45 pm    Post subject: Hints on: Debugging logic errors Reply with quote

So, you've cleared up all the syntax errors and your program is running. Only problem is, it is not doing what you think it should be doing.

Computers have this really big problem, they only do what you tell them to do, PERIOD.

One of the best ways to debug a logic error is to include comments for each function, sometimes each line, so you can see your intent (the comments) and then look again at the code to see if it does in fact follow your intent.

Once you have the entire program documented to the extent that someone else can now understand what you are trying to do, it's time to put alert type statements into the code in such a way as to test what you think a section should be doing.

Example:
Code:

var tot = 0
// Adding all numbers 1 - 100
for(var i1=1; i1<100; ++i1) {
  tot += i1;
}


To debug we could do this:
Code:

// Adding all numbers 1 - 100
for(var i1=1; i1<100; ++i1) {
  tot += i1;
  alert("Adding: "+i1);
}


After 100 times we'll see that we never got to 100. Or, you can be a bit smarter and do:
Code:

// Adding all numbers 1 - 100
// Just test up to 10
for(var i1=1; i1<10; ++i1) {
  tot += i1;
  alert("Adding: "+i1);
}

the problem shows up much quicker this way.

This is just a small sample, after all, if you are the one writing the program you should be the best one to know what each and every section should be doing. If that is so, then you should also be the best person to know how to test that each section is doing what you designed it to do.

One word of warning, in JS we can create infinite loops which are just too fast for us to be able to put an alert in and then find a way to stop the process. I developed a new alert function that allows even these infinite loops to stop, the best way I've found to do this is to cause a castistrophic error in the function when you enter a "stop" value. This causes the browser to die and that stops the process. In this case neither alert nor confirm can be used by themselves to get out of the loop.

Hope that helps!
_________________
Phil K
Circle Software Consulting
Test website: http://cs.yrex.com/
Guidelines for Posting: http://jsworkshop.com/posting.html
IHBAAA = It Has Been Asked And Answered
KISS: http://jsworkshop.com/bb/viewtopic.php?t=508
Back to top
View user's profile Send private message Visit poster's website
phil karras
Senior Member
Senior Member


Joined: 15 Jul 2002
Posts: 1750
Location: MD

PostPosted: Thu Oct 03, 2002 1:01 pm    Post subject: Reply with quote

Oh, it's not your code?

In this case you REALLY need to do step one and two in the article above:
Step 1. Comment each function, sections, and/or line to the best of your understanding as to what it is doing.

Step 2. Test all functions, sections, or lines that need testing to figure out just what they are doing.

the next step is:
Step 3. Modify any comments to reflect your new understanding of the code, function, section, or line.

You then continue with steps 3, 1, and 2 until you figure it out. This is a time consuming process but unless the person has doumented the code properly (meaning you can understand from reading the comments exactly what the program is intended to do) you are the one who needs to do it, after all, you are the one trying to save time and effort by not writing your own code to do the job.

Good-luck!
p.s. I am a software consultant right now and I am frequently asked to add a function or fix a bug in a program where comments are almost non-existant. These are the methods I use to learn what the program does and how to fix it or add that new feature. (As the language and program allows. This means that "C" doesn't have an alert function, and imbeded code doesn't usually allow this type of debugging anyway.)
_________________
Phil K
Circle Software Consulting
Test website: http://cs.yrex.com/
Guidelines for Posting: http://jsworkshop.com/posting.html
IHBAAA = It Has Been Asked And Answered
KISS: http://jsworkshop.com/bb/viewtopic.php?t=508
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    JSWorkshop Forum Index -> Articles All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group
(c) 1997-2002 Starling Technologies and Michael Moncur. Portions (c) Sams Publishing.