Amazon.com Widgets Print, print and when in doubt.... print. (troubleshooting tip)
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 31, 2014, 08:42:35 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Tips & Tricks
| | |-+ Print, print and when in doubt.... print. (troubleshooting tip)
Pages: [1] Go Down
Print
Author Topic: Print, print and when in doubt.... print. (troubleshooting tip) (Read 5704 times)
gwsounddsg
Newbie
*
Posts: 46






on: October 25, 2011, 03:44:09 PM

As someone that works in a technical field, I have mastered the art of troubleshooting.  I have applied a basic methodology of troubleshooting to programming: If information flows linearly like water through a pipe, then if you check for water halfway though the pipe and get nothing, your issue is upstream of that point, if you do get water then the issue is downstream of that point.

Lets say I have a method like this:
Code: (Objective-C)
-(bool) isMouseCloseToLine (NSPoint mouseLocation, NSPoint pt1, NSPoint pt2, float distance) {
    // find line and mouse vector
    myVector a, b; // this is just a struct with 2 double variables
   
    a.x = mouseLocation.x - pt1.x;
    a.y = mouseLocation.y - pt1.y;
   
    b.x = pt2.x - pt1.x;
    b.y = pt2.y - pt1.y;
   
    // get vector e
    double e = mouseDistanceLength(&a, &b);
       
    // check condition of (+/-)e to check
    if (e<=distance || e<=-distance) return YES;
    else return NO; 
}

There's a lot happening here, its asking a question: is the mouse distance to a line in between two points less than or equal to a specified distance?

Let say I set the mouse distance to be checked at or under is 10 and I know the mouse is at a distance of 5.  If my method returns NO, that would mean something is wrong with my method.  I could try rereading though my code to find the bug; but if someone else wrote this algorithm or if I wrote it a week ago, that could take too much time/thought/energy.

What I could do is add some NSLog's to print out the variables to narrow down the location of the bug:
Code: (Objective-C)
 
bool isMouseCloseToLine (NSPoint mouseLocation, NSPoint pt1, NSPoint pt2, float distance) {
    // find line and mouse vector
    myVector a, b;
   
    a.x = mouseLocation.x - pt1.x;
    a.y = mouseLocation.y - pt1.y;
   
    b.x = pt2.x - pt1.x;
    b.y = pt2.y - pt1.y;

    // get vector e
    double e = mouseDistanceLength(&a, &b);

    NSLog(@"distance e: %.2f", e); // this will print out the value for e
       
    // check condition of (+/-)e to check
    if (e<=distance || e<=-distance) return YES;
    else return NO;   
}

If my value for e is NOT 5 then the issue with my method is at/before the setting the value of e.  If my value for e IS 5 then my issue is down stream with my if statement that returns the YES or NO.

I can add NSLog() to find the values for variables, including strings/arrays/dictionaries, to find an inconsistency that the complier wont be able to pick up.  I have used this technique to solve over %50 of bugs in my code, especially when dealing with methods that are over 100 lines of code.

Its a quick tip way to debug.

GW
Logged
JackRabbit
Jr. Member
**
Posts: 54



Email




Reply #1 on: November 07, 2011, 09:38:58 AM

Hello gwsounddsg,

I followed what you said and I think that is a very good way to check for errors. I will remember this when I run into trouble.

Thanks.

JR
Logged
briannalumen
Newbie
*
Posts: 1


pablocarman@mail.com briannalumen briannalumen
Email




Reply #2 on: June 24, 2012, 10:10:30 PM

I too use the same for finding any exception, for finding the output values. This is the best way for tracking your code correctly. This is the most simple and the most useful method for tracking errors.
Logged

brandylrichard
Newbie
*
Posts: 1

kristenrichardit@gmail.com brandylrichard brandylrichard
Email




Reply #3 on: February 19, 2013, 10:56:39 AM

Completely agree with you briann, that's the best and reliable way to track bug and execution of our program.
Logged

Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.