Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

How to Approach Debugging Code

Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
A lot of people on the Internet today (new coders) cry:
"It's Not Working!!" with no knowledge as to why.

They visit one or two of countless forums on the net to find their answer, (often find it), and return for the next bug.

Well here, I'm going to attempt to explain to you how to catch bugs on your own.

There are two types of errors I'll be going over here:

Syntax Errors: Errors in the code causing it to break or malfunction; caused by improper syntax.

Semantic Errors: Writing code with invalid logic, often causing it to malfunction, but with syntax the compiler/interpreter can understand.

If you have a syntax error, you'll often get an error message. More often than not, that error message will guide you in the direction of the syntax error in the code. They're pretty easy to debug. Find the error, fix it. Find the error, fix it, repeat. If you can't find the error, really look at the error message closely. Still can't find the problem? Search the error message on google, with your language in the front of the search. Still can't find the problem? By now I imagine you're looking in a certain realm of the code. Look at the code as a hole, skim through it piece by piece, discount large clusters of code 1 by 1 and (if you haven't found the bug), return to the realm you were at before. Do you see the bug now? Return to google. Still haven't got any closer to finding your bug? Re-indent your code (if necessary), making it easier to read. If ALL ELSE FAILs, ask for help.

Here's a syntax error in PHP:
PHP:
$name = 'Fred;

//..

function list_pairs($assoc_arr) {
    foreach($assoc_arr as $name=>$value){
        echo $name.': '.$value;
    }
}
(For this particular error, a code highlighter could really help. Get an editor with a highlighter, or get a highlighter for your editor if possible.)



With semantic errors, you don't get an error message. You're left in the dark, often not even knowing the error is there.
Here's an example of a semantic error in JavaScript:

PHP:
var name = "Fred";
//...
function list_pairs(assoc_arr){
    for(name in assoc_arr){
        document.write(name+": "+assoc_arr[name])
    }
}
This one's quite a bit harder to find at first glance. (Sorry for the PHP highlighter, there's no JavaScript highlighter, and I have to point out- the syntax is just fine.

The variable name in the function, list_pairs changes the value of the global variable name. Because the variable exists, there's no error in JavaScript syntax. However, we accidentally lost Fred's name!

If this function was in the center of 25-100 functions, it could take us ages to find out how Fred's name is getting changed in some circumstances.

Challenge:
Is this a syntax error or a semantic error?
PHP:
#name = "Fred";

Debugging semantic errors is much tougher. One day you might notice that when viewing the categories section of your site, the user menu changes the username into "Fruit". It could've been weeks when the error actually started happening (when you wrote the list_pairs function).

Now: Don't be scared. Let's assume the function is in the center of 100 other functions- big and small like this one..

The term name could be SEVERAL places on a content-populated HTML web-page. Copy the content just between the <head> tags, if the JavaScript is in the (worst case scenario, 5000-line, staticly created HTML file). Paste the contents in a blank new file. Remove the css.
Now find "name" Hit next, look closely, repeat- until you find your answer. If you don't find it, look for scripts included into that file. etc, etc. Now, other languages there's no clutter like HTML and CSS to sort through, (hopefully you don't code JavaScript that way either- use external files), I just wanted to do a poor-case scenario.

Now, there are many other semantic errors- that's just one example. The best way to catch them to pay attention to what you're doing. No matter who you are, sometimes these bugs come up, and catching them is really fun (and sometimes frustrating), but gets quicker as you get the hang of whatever language/style of code you're doing. (yes, new styles of coding often brings a new taste of semantic (often as well as syntax) errors.)

Just realise if your code crashes, it's not "PHP's doing weird things..", It's your code, or the circumstances with your settings causing your code to be interpreted differently (semantic error).

So before you come asking for help, experiment with your code (well don't mess it up), practice good syntax, practice good indenting structure (clean code, so it's easy to find both semantic AND syntax errors.. Dirty code makes hunting for errors a nightmare... Yuck!) Don't be lazy, learn to debug your selected language(s) or you'll never learn your selected language(s).

Hope that helps,

There's no reason to rely on ppl on the net to hold your hand through everything, get some confidence and learn to debug your problems on your own :thumbup:
 

Zen

Custom Title Activated
Loyal Member
Joined
Dec 2, 2006
Messages
1,621
Reaction score
152
Good introduction to debugging, some languages are more intuitive as to debugging, like for example PHP, but some leave you to your own devices alot more... Like JS.

Good writeup.
 
Infraction Baɴɴed
Loyal Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
i always thought that '#' was an invalid way to comment out a line but hey it works for me.

EDIT:
one way i get around the semantic errors is to use
Code:
echo "<pre>";
var_dump($variable);
echo "</pre>";
 
Last edited:
ex visor
Loyal Member
Joined
May 17, 2007
Messages
2,741
Reaction score
937
i always thought that '#' was an invalid way to comment out a line but hey it works for me.

EDIT:
one way i get around the semantic errors is to use
Code:
echo "<pre>";
var_dump($variable);
echo "</pre>";

I find myself doing the samething.
Nice tutorial spence. :thumbup:
 
Mother effin' clouds
Loyal Member
Joined
Apr 13, 2008
Messages
1,534
Reaction score
448
Excellent guide! However, in regards to JavaScript, rather than playing Tom and Jerry with semantic errors that arise from conflicting variables, you are better off avoiding the use of globals in the first place - e.g. Namespacing your JavaScript (private properties, methods, etc). Also, by doing so, you can encapsulate and identify any errors more readily (in my experience).
 
Back
Top