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;
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 instead of $States.
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.