Home arrow HTML arrow Page 2 - Combating Coding Errors

Combating Coding Errors

Coding errors can get even the best of us down, but they don't have to be seen as the "be all, end all" of coding, as Steve shows us in this article.

Author Info:
By: Steve Adcock
Rating: 5 stars5 stars5 stars5 stars5 stars / 13
November 28, 2002
  1. · Combating Coding Errors
  2. · The 4 types of Programming Errors
  3. · PHP Specific Error Messages and Reporting
  4. · Conclusion

print this article

Combating Coding Errors - The 4 types of Programming Errors
(Page 2 of 4 )

Usually, errors on the Internet can be broken down into 4 categories. Let's take a look at each in stride and consider an example of each.

1. Syntax errors
These are mistypings or other errors in code that prevent the execution of a script. A syntax error is the only error type that prevents the script from any execution. Let's take a look at an example using PHP:

$x = 1
echo $x;

The code above produces an error because every executable line within PHP must be terminated with a semicolon. The second line is correct, but the first line is not.

Other syntax errors include unbalanced parenthesis within mathematical equations, mistyped language keywords and extra curly braces -- among many others. Many times, syntax errors are the easiest to diagnose because error messages display the problem and also the problem's location.

2. Semantic errors
Semantic errors involve technically correct code which has fundamental problems with the meaning of the code. Since compilers do often point to the error, semantic errors can be found and diagnosed in many cases without too much heartache. The following example:


... would be a semantic error if file.txt did not exist, and therefore could not be included within the document. Please note that although, on the surface, semantic errors can closely resemble syntax errors, a script can be executed until the point of the semantic error, unlike scripts that include syntax errors. This is because semantic errors contain correct code, like in our example above. It just so happens that file.txt does not exist, but the compiler has no way of knowing until it reaches that specific line of code.

3. Logic errors
The most difficult to diagnose, logical errors contain syntactically and semantically correct code, but do not execute the way the programmer has intended. This is often what drives programmers up the wall, especially with large and complex Internet applications.

Logic errors can be as simple as incorrect operator usage (+ instead of -, / instead of *). Logic errors can also be as complex as database record calls and elaborate SQL statements. A logic error in PHP might look something like this:

$States = array ("Alabama", "Alaska", "Arizona",
"Arkansas", "California");
echo "The first record in the States array is " . $States[1];

The 5 states listed in alphabetical order are saved to an array called States. The logic error comes in the 2nd statement, when we intend to output the first record in the array, or Alabama. Experienced programmers will pick this error up immediately, but for those without much knowledge in programming, it may seem cryptic. Simply put, array positions begin with 0, not 1. The 2nd statement will output Alaska, not Alabama like we intended to do. To fix this, we would simply modify the output to $States[0] instead of $States[1].

Another logic error might be including the wrong file, adding wrong variables or simply forgetting to manipulate data. Compilers cannot find logic errors, as it has no knowledge of what the programmer intends to do. In the above snippet of PHP code, there is nothing wrong with outputting cell position 1 in the array, so the compiler did it without any problem. Therefore, logic errors can only be determined and remedied by the programmer.

4. Environment errors
Environment errors are often forgotten, but are very real within the Internet community. Environment errors are errors beyond the programmer's control, and can include anything from absent language components within ASP to register_globals being turned off within PHP. It can be said, however, that environment errors occur least often out of the aforementioned 3 error types.
blog comments powered by Disqus

- Does HTML5 Need a Main Element?
- Revisiting the HTML5 vs. Native Debate
- HTML5: Not for Phone Apps?
- HTML5 or Native?
- Job Hunting? Freelancer.com Lists This Quart...
- HTML5 in the News
- Report: HTML5 Mobile Performance Lags
- The Top HTML5 Audio Players
- Top HTML5 Video Tutorials
- HTML5: Reasons to Learn and Use It
- More of the Top Tutorials for HTML5 Forms
- MobileAppWizard Releases HTML5 App Builder
- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials