Common Programming errors
The greatest enemy in programming to all programmers is getting errors, and everyone has experienced getting errors when they run the code. Even the well-experienced programmer is still facing an error. Programming is a tricky and delicate thing to do when you need to be more careful.
Why are we getting an error? Well, humans and computers speak in different languages, and we humans are practicing these various types of computing languages to command these machines to do work for us. So, there might be some misunderstanding when we are controlling them, like computers don’t understand what we are trying to do because we didn’t use the correct block of code or didn’t specify what to output.
Coding errors could be typos, flawed logic, or accidental oversight; even those little ones can manifest and impact the whole program, depending on the type of error. So, here are some common types of programming errors.
2. What are considered common programming errors?
In the good old days, way before our understanding of programming and coding as it is today, getting errors could be truly disastrous. Good thing for us that all modern coding approaches and debugging systems make it a lot easier for us to fix these errors whenever we get one. Seven common programming errors are encountered frequently in programming. They are Runtime errors, Logic errors, Compilation errors, Syntax errors, Interface errors, Interface errors, and Arithmetic errors.
2.1. Runtime errors
Runtime errors are coding errors that show themselves when a user tries to run the program. It got an error because something confused the program and caused it to crash. Runtime errors are also known as execution errors.
For example, user instruction could be in the wrong order, pointing to a step that hasn’t even been declared yet, or the user might have asked the computer to do something that doesn’t have to do with anything before that code. The simplest example here is telling the computer to divide by zero, which could result in an error. Another typical example that could happen is when users interact with a program unexpectedly. Runtime errors are the kind of error that can impact users from achieving what they want from the code. Here are some common mistakes that users made that led to getting runtime errors.
● Misspelled or incorrectly capitalized variable and function names.
● Attempts to perform operations, for example, math operations on the data in which data types are string values.
● Dividing by zero
● Try to use a type conversion function on a value that can’t be converted to an int
In the following program about multiplying tax to the subtotal that the user has input, an error has occurred saying, “NameError: name ‘subT123otal’ is not defined. Did you mean: ‘subtotal’?” meaning ‘subT123otal’ can’t be defined because it wasn’t declared at the top. So, in this case, the user misspelled the variable when he reused it.
The following link will explain more types of runtime error examples and how to fix them.
2.2. Logic errors
Logical errors can be extremely frustrating since everything is fine with the code. They are the most difficult to fix because the program runs without crashing but outputs the incorrect result. The programmer didn’t type the code as he intended, so the program output the result that the programmer didn’t want.
It is caused by a mistake in the program’s logic. It is the hardest to fix because you don’t get the error message, no syntax or runtime error to alert you that you did something wrong. You will have to find out by looking back at your code.
Sometimes it’s nothing wrong with the Python implementation of an algorithm, but the algorithm itself can be incorrect; however, most of the logic errors are caused by the carelessness of the programmer. Did you know a logical error caused by a miscalculation between American and English units caused NASA to lose a spaceship in 1999? These are some examples of mistakes that lead to logical errors:
● Programmers use the wrong variable name
● Indenting block to the wrong level
● Using integer division instead of floating-point division
● Using the wrong formula, which leads to the wrong result
Suppose you misspell an identifier name or use the wrong variable name. In that case, you may get the runtime error if that misspelled variable doesn’t exist else. You will get the logic error if the misused one already exists, but you won’t get the error message, making things tricky and hard to detect.
The example above calculates the average of the two numbers the user enters. Still, because of how the order of operation is typed, the division evaluates before the addition, which is different from what we want. It should be (x+y)/2. Since it doesn’t mean an error to a program, it didn’t show any error message.
2.3. Syntax errors
Syntax errors have their own specialized rules, and when these rules are not followed and the programmer runs the code, a syntax error prevents the program from running. Python will find this error and exit your program without running anything. They are analogous to spelling or grammar mistakes like in English: “Have you the movie? “Which doesn’t make sense and missing a verb. Some of the standard Python syntax errors are
● Leaving out the keyword
● Putting a keyword in the wrong location
● Leaving out symbols such as commas or semicolons, or brackets
● Empty block
In the following example, when the interpreter encounters invalid syntax in the program, it will raise a Syntax error and provide an error message with helpful information to the programmer, unlike a compilation error. In this case, you can see in line 4 a comma is missing at the end.
If you try to run the code, you will get the following error message saying invalid syntax and provide you with possible solutions like “Perhaps you forgot a comma?”
The following YouTube link has more examples of those three programming error types mentioned above. One of them is an example of syntax error.
Syntax, Runtime, and Logical Errors in Python
Can you spot the error? There are two errors you must fix for that code to run. Click the link and watch the video to see what the errors are.
2.4. Compilation errors
This error usually occurs when the code is built, but the program is failed to compile. If the code doesn’t compile, the program can’t be executed. The same as Logic errors and compile-time errors are a source of frustration to developers, especially when they have just started learning the language. Unlike Logic errors, you do get the error messages, but they are unclear and don’t mean anything useful.
Long story short, Compilation is converting a high-level coding language into a low-level coding language that is easier to understand by the system. While doing that process, an error occurs when the compiler can’t translate the high-level code into the lower-level one. Causing this prevents the program from being launched or tested.
These are the ten most common mistake happened in Java compile time errors:
● Java source file name mismatch
● Improper casting
● Mismatch brackets
● Missing semicolons
● Variable already defined
● Variable not initialized
● Type mismatch: cannot convert
● Return type required
● Unreachable code
One of them is mismatched brackets which is the cause of stress for programmers. We know that every open bracket in the code requires a matching closed frame, but sometimes a programmer needs to remember the closing brackets after writing the code inside, which leads to compile time error. For example, the picture below shows the opening bracket is missing at println.
So, when it compiles, it reports the following error message, which hardly tells what’s the problem.
Then when we put the missing open brackets to fix the problem, the error message goes away.
2.5. Interface errors
Most of the time, the program follows standards. If inputting of your program doesn’t conform to standards, you might get an interface error. These errors occur when there is a disconnection between what the programmer meant to be used and how it is used. An interface error might happen when you have an API that needs that specific parameter to be set, but those parameters are not established.
Besides being handled correctly, interface errors will look like an error from your side, but it’s from the caller’s side. This can lead to frustration from both parties. That’s why having clear documents and recording these errors to pass back to the caller is the best way to handle this. In this way, it will keep support costs down and keep your client happy.
If you didn’t record these errors and pass them back to the caller, they would end up like runtime errors in your error message.
2.6. Resource errors
A program can force the computer it’s running to attempt to allocate more processor power, ram, disk space, etc., than it has, which leads the program to crash or be bugged. Usually, the computer your program is running will allocate a certain number of resources. Still, sometimes your code forces the computer to try and allocate more resources than it has, resulting in resource errors.
If you accidentally write a loop that your code can never exit, like the one in the picture, you will eventually run out of resources on your computer. The loop will keep going for a while until it runs out of memory. Eventually, your computer will run out of memory. This type of error is hard to capture because the computer you are using can be high quality than the server running your codes, so it’s hard to replicate with your computer.
In this code in the picture above, a is set to 1 and put in the while loop where a is less than 10. Print that line and multiply a with 1, which will never be more than 1, and it keeps looping forever without exit.
After a certain amount of time and an infinite loop, your computer will have fewer memories and, eventually, run out.
2.7. Arithmetic errors
Arithmetic errors are just like logic errors but with mathematics. For example, some division equations may require the program to divide by 0, which is mathematically impossible. It results in an error that prevents the software from running correctly. As mentioned, arithmetic errors can generate logic or sometimes even runtime errors in the case of dividing by 0. To stop that, having functional tests that include edge cases like 0 or negative numbers is an excellent way to prevent these errors.
The above picture shows an example of how arithmetic errors happen. The program tries to divide five by 0, which is mathematically impossible, and the program was told to print the answer. It turned out with an error message saying, “ZeroDivisionError: division by zero.”
Ultimately, we can’t escape from errors as long as we continue programming. At some point, it is better to know these types of mistakes in advance so that when it appears in the future, you might learn a thing or two about them and can identify what type of error is, which saves you time looking through everything on the internet. These are the seven most common errors in programming languages and their examples.