How to Debug your Code

CMPC :: Computing :: Programming :: C++

Go down

How to Debug your Code

Post by Admin on Fri Dec 25, 2009 12:19 am

As time goes by and your code seems to get better and better you'll learn to debug your code better as well. For example you may find that you keep making the same mistake in your coding practice over and over and over... so you'll soon learn to check if you made that mistake as one of the first things you'll do when fixing up your code.

All code is created equal? HA everyone makes their own weird mistakes but generally there are a few ways to start checking your code for any errors. If you have a debugger (... almost EVERY compiler has one these days) it will tell you all of your errors but these errors are only syntax errors. The compiler doesn't know what you want the program to do so as long as it can compile the program, it will not tell you that you have a memory leak somewhere in your code (... damn memory leaks, it kept me stuck in my rpg game for months until paul found it in a few seconds of looking at the code).

When you're seriously stuck with your code after an hour of looking at it and repeatedly having this as your facial expression Mad then ask someone for help. Chances are they'll look at your code in a different fashion and find your mistakes!

A lot of compilers these days have a debugger as I've said before and this debugger will allow you to execute your program step by step and tell you which line of code it executes by showing you on your source file with an arrow (my quincy compiler has a fun arrow that points from line to line where the program is at the moment and also switches to different source files if it must). This allows you to see what is happening at every step and helps you see wtf is going wrong and why.


Also don't forget clean up your code every now and then so it is easier to read your code.

What looks easier to debug, a program with indentations for every if statement or a program without indentations and a huge tree of if statements.. hmm yeah I believe it's the one WITH indentations. ALWAYS REMEMBER TO INDENT YOUR CODE. It's for the benefit of the reader and yourself!

Comments are.... usually unnecessary if you know what you're doing but if you have a huge block of confusing and cryptic code that you finally finished, don't just say that you'll remember what you wrote!COMMENT IT!
Nobody will understand what you wrote without a good hour of staring at it with a blank face No

Skip lines. Compilers don't read blank lines so it's ok to leave a couple lines of code between each function or class. Separating functions makes it easier to navigate across your files and dammit it's not hard to press Enter!

Oh also I find this to be the case in studying either Quantum Mechanics or Computer Science.





In the end you'll have atleast 10 wikipedia pages open at the same time and a few notes as txt files saved on your Desktop.
avatar
Admin
Pirate King

Posts : 559

Back to top Go down

Re: How to Debug your Code

Post by Unchained on Fri Dec 25, 2009 1:06 am

I used that pic for my CS project...

Also:
1. Trace output.
2. Pseudo-code.
3. Find a better programmer you can ask for help.
avatar
Unchained
Mod

Posts : 448

Back to top Go down

Re: How to Debug your Code

Post by Corey on Fri Dec 25, 2009 9:53 pm

A GeV is a Giga-electron volt.

1 eV = 1.602176487×10^-19 Joules
1 GeV = 1,000,000,000 eV's (I hate when people put commas in numbers, but...)

To give you an idea, the LHC at CERN has about... 1.42 TeV (I think...) or 1420 GeV.
The kinetic energy is a average size mosquito is around 1 TeV, or 1000 GeV

I didn't have to look that up! (except the conversion from eV to J)

_________________
-Corey Hoard

(Yes, I totally copied Paul's avatar. Or, at least attempted to.)
avatar
Corey
Mod

Posts : 462

Back to top Go down

Re: How to Debug your Code

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

CMPC :: Computing :: Programming :: C++

 
Permissions in this forum:
You cannot reply to topics in this forum