Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Sep 16, 2019 11:25 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Debugging 101
PostPosted: Thu May 13, 2010 3:54 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
People often tell me, "Tim, you are such a programming stud!" Ok, so by "people" I mean "me" :) but I have learned a few things over the years that I thought might be of use to others.

There are generally 2 kinds of questions asked on the forums, "How do I do this?" and "Why doesn't this work?" The first can often be answered by perusing the fine manual. :) I know that sometimes it's easier to just ask, but you learn so much more by studying first, and then asking more specific questions where things don't make sense.

The second question is a little trickier, but the answer is often simple if you just take the time to look closely at what is going on.

The first and most important thing is, Question all your assumptions! Or to put it another way, Make no assumptions! Most of the time, your code isn't working because what you thought was going on just isn't true at all. I can't tell you how many times I have dismissed some piece of code because I just knew it couldn't be the problem, only to discover later that it was indeed the culprit. Check everything!

The corollary is, Look at the data! This is so critical to understanding what is going on, but so often people only look at the actual data after wasting much time tinkering with the code. Computer programs do one thing only: manipulate little bits of data. If the program isn't doing the right thing, there are only 2 possibilities. Either you told it to do the wrong thing to the data or you told it to do the right thing, but the data isn't what you expected it to be. Very often the problem is the latter and the whole issue could be sovled by simple inspection.

Learn to use the debugger. The IDE contains a very powerful tool. Set break points and step through your code. At each step, examine what the code did to the data you are working on. It can prove very enlightening. Over time, you will be able to more accurately envision what the code is doing to its data, and therefore more accurately predict whether the code will do what you intend. You'll become a better and more efficient coder.

Get a fence around the problem. Break it up into pieces and make sure you know which piece is failing. For example, if your code reads some data, manipulates it and writes it out, and you see that what it writes is wrong, where did it go wrong? Was the data good when you read it? (Garbage in, garbage out) Was it good before you wrote it? Or are you writing the wrong thing. Continue to segment the code into smaller and smaller pieces until you've pinpointed exactly where it is failing.

Keep it small. Write small methods that perform a single task. They are much easier to debug than long, complicated methods, if only because you can see the whole thing at once. Write small, well defined methods that you can test and prove out. Then you can depend on them. Large, complex programs are built out of small building blocks. Factor your code. Just like a mathematical expression can be factored into smaller expressions, code can be factored into smaller methods. The bonus is that they are often reusable.

Don't be afraid to write throw-away code. I frequently start a small project just to try something out, or to test out some method I'm working on. Write stubs and drivers for the method and you can put it through its paces on a variety of data that you have complete control of. Sometimes, I'll just throw an extra pushbutton onto a window in the big project to put test code in and then delete it later. Again, the goal is to create a more controlled environment so you can watch what happens to the data.

Most programming problems aren't difficult to solve. They just look that way on the outside. Most of them can be solved just by looking closely and stepping through the code.

Happy coding,
Tim


Top
 Profile  
Reply with quote  
 Post subject: Re: Debugging 101
PostPosted: Thu May 13, 2010 4:01 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
Very well said [written?]!

About the only thing I'd add is.

If you are "positive" the code is right, and your are "positive" the input data is correct. Then [if possible] have another set of eyeballs look at it. And if you don't have someone handy who is also a developer, there is another method that I have employed many times over the years. It has the moniker of "The Intelligent Doorknob" [that name was given to it by a business analyst I worked with many years ago]. The principle is very simple, find a patient somebody [friend, spouse, significant other] and explain the code to them. They don't have to know a darn thing about programming [hence the "doorknob" part of the name]..... but you will invariably find that as you are explaining the code on a line by line basis, you will all of a sudden go "OH SHUCKS!".... trust me it really really works.

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Debugging 101
PostPosted: Thu May 13, 2010 4:16 pm 
Offline

Joined: Sat Mar 25, 2006 1:39 pm
Posts: 894
Location: Morgan Hill, CA
I absolutely concur, Dave. I can't count the number of times over the years that, in trying to explain a piece of code to someone, I'd find a problem that I otherwise couldn't see.

_________________
While fairly new to RB, I've been programming ever since that darned mastadon stepped on my abacus. Using RB 2009r5.1 on XP and Windows 7.


Top
 Profile  
Reply with quote  
 Post subject: Re: Debugging 101
PostPosted: Thu May 13, 2010 4:24 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
Hey Bob....

I figure enough eons have passed, that you will probably forgive me by now.....

But that wasn't a "darned mastadon" that stepped on your abacus...... It was me....

I was jealous... :mrgreen: You had that new fangled TI.001 model... and all I had was my fingers and toes...:D

But trust me when I tell you, that "darned mastadon" got even with me for framing him.... he cr*apped in my cave

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Debugging 101
PostPosted: Thu May 13, 2010 6:22 pm 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
Good post. I would add that being a developer is all about 'persistence' in whatever task you're working on.

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
 Post subject: Re: Debugging 101
PostPosted: Mon May 17, 2010 1:07 pm 
Offline
User avatar

Joined: Sat Nov 11, 2006 2:43 pm
Posts: 1221
Location: This poster has left the forums
Nicely detailed Tim.


DaveS wrote:
The principle is very simple, find a patient somebody [friend, spouse, significant other]


That rules my spouse out then. I can imagine her eyes glazing over, and her trying to stifle a yawn, or perhaps yawning out loud in a slightly sarcastic albeit humorous manner.

I once had a presentation to make, the subject was our proposed use of SEO. One of the topics was pay-per-clicks. My audience was a non technical board of directors, so my non technical wife seemed like an ideal test audience. I thought I had nailed it, and she had taken in everything I had detailed, that was until she asked how on earth they got the paperclips on to your computer and what was the purpose :lol: my wife is a doctor and has had more schooling than I. Sometimes your target audience is important, you have to at least believe they are listening.

_________________
%Invalidforumsignatureexception% user signature not found


Top
 Profile  
Reply with quote  
 Post subject: Re: Debugging 101
PostPosted: Mon May 17, 2010 6:33 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
Not really... you spouse can be snoring, as long as it doesn't keep you from articulating out loud the steps and purpose of your code. Because you will at some point say "and then it does XYZ..." and realize... "Oh Shucks... no it doesn't.... thats my problem!"

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Debugging 101
PostPosted: Tue May 18, 2010 1:12 pm 
Offline
User avatar

Joined: Thu Mar 01, 2007 2:02 pm
Posts: 237
Location: Sunny (generally!) Southern California
Dave,

You're right, the person does not have to be a programmer since the object is really to make you, the developer, actually THINK about the code and what it is doing instead of what it should be doing.

Years ago (don't ask how many since dinosaurs can't read calendars), before I switched from audio hardware to computer software, I was asked by a programmer to look at a Pascal routine that he couldn't get working. He walked me through it and I understood just enough to ask "What are the semicolons for at the end of each line?"

He started to explain, pointing to a few statements, that they indicated the end of the executable statement or block. "Uhh...shit," he said, "except for that one after the 'then' in that 'if' statement."

Hey, what did I know? :D

_________________
-----
Real Studio 2012r1 on Windows 7 (64 bit)
-----
It has been said that politics is the second oldest profession.
I have learned that it bears a striking resemblance to the first.
- Ronald Reagan


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


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

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group