Monday, 20 August 2012

Programming: Handling Code Errors

Encountering code errors is essentially a part of the daily reality of programming. The more programming you do, the more you begin to realise that the activity of coding is in large part about finding and fixing the mistakes you've made. You can forget about not making mistakes, unless you're a robot. Accepting the fact of code errors is actually a positive step in your development as a coder, as programming is primarily a problem solving task, which is ultimately why it's a rewarding one - honest!

Even if you could write an entire application without a single syntax error, you're always going to come up against errors or unforeseen problems with your code logic and implementation so it's vital to be able to handle them.

Approaches

Whether you're struggling with syntax errors (referred to as compile time errors if they prevent program compilation from occurring successfully), runtime errors, or logic errors preventing your programs doing what you need them to, there are a number of key helpful techniques.

These methods can help resolve code errors more quickly, and in some cases prevent them from arising in the first place. Effective error handling involves getting into good habits with your routine coding practises, as well as what you do when problems crop up, i.e. your debugging skills.

Planning

It may not be the sexiest part of programming, but taking the time to plan your development activities properly will inevitably reduce the amount of debugging you end up having to do. It's always tempting when you've just started working on an application or project to get stuck in start coding straight away, but it is genuinely the case that spending even a small amount of preparation time makes the development process less error prone and stressful.

Use a Code Highlighting Editor

For many developers this one goes without saying, but if you're perhaps just getting started as a coder, you might be amazed by how much easier it is to work with code that is highlighted. Depending on the language or technology you're working with, you may want to simply opt for a decent text editor that can highlight source code, such as Notepad++ on Windows or Gedit on Linux.

Use an IDE (Integrated Development Environment)

Depending on which language or platform you're working in, there may be one or more IDEs you can use for it. Integrated Development Environments come with a number of different tools, for example to help with debugging, visualising your applications and with the development process generally. At the very least, an IDE will highlight your code reliably, and in most cases will flag up any syntax errors so that you will typically catch them before you even attempt to compile and run your program.

Most IDEs also have some sort of error or output console which will indicate information about any runtime errors that occur. You can also use these output consoles for trace statements, as explained in the below section.

Use Comments and Documentation

It's something many programmers don't have the best habits for, but effective use of comments is one of the key defences against errors. Ideally, if you outline what the different elements in a program are going to do with comments before you even start writing code, as well as continuing as you write the code, this will provide a clear picture of what's going on throughout the development cycle.

Comments aren't just to help other people read and understand your code, as the more code you write the more you'll understand the regular nightmare of looking back at your own code and being totally baffled by it, sometimes only a short time after writing it in the first place.

Depending on the platform you're working in, and the size of application you're developing, you may want to consider documentation as part of your daily coding routine as well. For a language such as Java, the process of generating documentation is easily automated using tools such Javadoc, which again make the development process less frustrating.

Use Indentation and Whitespace

Effective use of indentation and whitespace in your source code files is another simple but often neglected practise that makes development much easier. Indenting your code makes the overall structures within it visible at a glance, particularly where structures such as conditionals, loops and class declarations are being used. If you're using an IDE, it may automate the process of correcting your indentation, particularly useful if it's in a bit of a mess.

Develop Incrementally

Sometimes you have to go against your instincts with programming to spare yourself some headaches down the line. If you've just started on a project or have just worked out how you want to approach part of it, it's tempting to get a load of code down quickly and without stopping. However, the more code you write in between testing, the more difficulty you'll have finding the syntax and logical errors within it.

Writing small amounts of code one at a time and testing regularly makes for programs that have fewer bugs, and in which the bugs are easier to locate and fix. If you're worried about remembering the details of an algorithm or solution to some problem you've been having, i.e. if you've just had a brainwave and want to get the code down before you forget the details, you can insert an overview as a note in a comment first, then set about implementing it in code.

Use Development Tools

Whether or not you're using an IDE, there will likely be development tools available for the language you're working with. If you're developing for the Web, there are a lot of browser tools that can help to find and fix errors. Tools such as Firebug can prove indispensable, particularly if you're working on large or complex applications.

If you're developing in an IDE, check out whatever debugging tools it has installed, or indeed whether there are any you can install as plugins. IDEs like Eclipse typically have a lot of optional extras you can install as well as the already extensive set of tools provided as standard.

Use Trace Statements

To fix code errors, you first have to locate them, and this can be the hardest part of the process. As well as developing in short bursts, testing each time you add some new aspect of functionality, you can give yourself some helpful clues as to what's going wrong using trace statements. A trace statement is generally something that you output to indicate what is happening at a specific point in your program.

Where your trace statements are output depends on the platform and IDE you're using, if appropriate. For IDEs there will typically be an output console, which you can write to directly, for example in this Java statement outputting the value of some variable at a particular point:

System.out.println("Data value: "+someVariable);

For Web development, you can output similar information from server side scripts to the browser, as in this PHP example:

echo "Data value: ".$someVariable."
";

In this case the value will simply appear within the webpage HTML, although this may not be ideal if you're working on a site that is already live - in these cases you can opt for something that will not be seen by site users, for example using server side tools such as writing to the PHP error log.

For client side scripting such as JavaScript, you can output information either to an alert dialog in the crudest example, which is again only suitable if you are working on a site that is not live:

alert("Data value: "+someVariable);

Alternatively, you can write your information to the log, which is also accessible using tools such as Firebug:

console.log("Data value: "+someVariable);

As well as using trace statements to check variable values as your code executes, you can use them to simply check whether the code is reaching a particular stage, for example to see if a function is definitely being called.

Isolate Error Prone Code Excerpts

If you're working on a large or complex project, it may help if you can separate any sections of code that are causing particular difficulties. This can help to locate the problem areas and can also make the testing process a little less cumbersome.

An alternative to copying chunks of source code into separate scripts, to be run independently, is to temporarily comment out those parts of code you do not need running to complete your debugging, letting you focus on the parts you're trying to troubleshoot.

Take Regular Breaks

It's yet another thing most of us know we should do but regularly fail to, but taking regular breaks can make the difference between success and failure when it comes to fixing code errors, as well as potentially stopping you going totally off your head! Sometimes I feel I've hit a complete dead end with a code problem, but after a short break I manage to find it relatively quickly and easily.

Links

No comments:

Post a Comment