Programming essentially propels today’s technology forward.
Whether it’s used to make apps, software products, websites, or interfaces for smart appliances, programming is a skill that makes our lives easier.
However, even the most experienced and skilled programmers make errors or encounter them in a code.
And when they find themselves in those situations, it’s crucial that they know what type of error they are dealing with to debug it successfully.
In this article, we’ll look at five types of errors that developers need to debug.
Table of Contents
Programming languages are, as their name suggests, languages. And any language, be it human or programming, has rules that a person should follow if they want to be understood.
If someone doesn’t follow the rules of the language, communication is, in the best-case scenario, challenging or even impossible.
And while you can communicate using human languages even if you don’t follow every rule to the letter, programming languages don’t allow breaking the rules of their grammar.
Bug & crash reporting tool for mobile apps.
When those rules are broken, that’s called a syntax error.
If a computer system encounters a syntax error, it simply can’t run the program, and the developer needs to find the error and debug it.
Margaret Rouse, an experienced technical writer and content strategist, looks at syntax errors as a way to keep code clean.
Syntax errors can be so small that it’s very difficult to notice them, but they can still break the source code and cause a lot of trouble for developers.
How small can syntax errors be?
Take a look at the example below.
Experienced programmers have a trained eye for reading code and might spot a syntax error such as the one above immediately.
On the other hand, when a program won’t run, and you know that the error this small is hiding somewhere in hundreds if not thousands of lines, even experience might not help you to find it instantly.
As you can see, a syntax error can be as small as a parenthesis without a match.
And regardless of how insignificant that might seem, programming languages have strict grammar and don’t let errors like that slip through the cracks.
What else can be a syntax error? Here’s a brief list:
- Missing operators
- Unmatched parenthesis
- Missing semicolons
Or, for instance, it can be an error like this:
That’s an example from Python. If you run this, you’ll get a syntax error.
Because of the quotation marks, or more precisely, lack of them.
If you look at the second line, the second apostrophe closes the string, so the t is left on its own, and it causes a problem for the program.
A simple fix is to close the line with quotation marks instead of an apostrophe.
Luckily, many text editors or linters can pick up on syntax errors and highlight them, so they’re not hard to find.
Nevertheless, a developer needs to debug those errors because, simply put, the program won’t run if they exist in the code.
As we’ve mentioned earlier, it’s easy to know if your code has a syntax error because the program won’t run it. On the other hand, logical errors are harder to spot.
The reason is simple. A programming language doesn’t detect a logical error as something invalid. The code is still correct but doesn’t produce the desired output.
In other words, when it comes to the programming language’s rules, everything is fine, and the program runs successfully.
On the other hand, you won’t get the expected result.
Here’s how Toby Osbourn, a software developer and technical writer, explains it:
Everything looks like it is working; you have just programmed the computer to do the wrong thing.
Since it’s not an app-crashing error, developers need to dig deeper and focus on finding a logical error in the code.
Furthermore, unlike syntax errors, a text editor or linter can’t point out the logical error because, as we’ve mentioned, the programming language rules are still honored.
For instance, take a look at the example of PHP code below from web developer Per Christensson:
As he explains, both of those terms use proper syntax. The difference is that the first line has one equal sign, and the second line has a double equal sign.
The logical error here is that in PHP, == means “is equal to”, and the = means “becomes”.
So, the first line is illogical because the if statement will always return a TRUE value—the $i becomes 1.
On the other hand, in the second line, the if statement is TRUE only if the $i is equal to 1.
Simply put, the first line is correct in its syntax but has an error in logic because the if statement is basically useless.
It will return a TRUE value no matter what, and it’s hard to imagine a scenario where that’s helpful.
As Oluchi M. Obadoni, a developer, data scientist, and technical writer, explains, logical expressions like if/else are often the source of logical errors.
When they’re not properly placed, there’s every tendency to get an incorrect output. Most programs rely a lot on logical expressions, so you need to know how to use them.
However, even the most experienced programmers can make logical errors, so it’s vital to find them and debug them as soon as possible.
You can have flawless syntax in your code and triple-check that the logic behind it is sound, but that still doesn’t guarantee you won’t experience a runtime error.
A runtime error occurs when a program encounters an unexpected issue when executing. When that happens, the program stops working and crashes.
That can be particularly problematic if developers don’t find the runtime error on time and the app is released to users, who then experience a software crash every time the software gets to a specific place when running.
And that can easily happen because, as their name suggests, runtime errors occur only when the program is running.
Below, you can see what users get on their screen when encountering the latter.
The GitHub user posted that example which happened to them using the Microsoft Edge browser.
What could have happened?
Well, as you can see in a highlighted part of the screenshot, a null reference exception is a runtime error that happens when an object reference doesn’t refer to any object.
To simplify, it is null because the code points to a non-existing object and, therefore, can’t run, crashing instead.
Take a look at this example:
The code lets the users view high scores, which are stored in the highscores.txt file.
But what if that file isn’t where the code expects it to be or someone deleted it?
Despite correct syntax and logic, the runtime error will be inevitable.
As we’ve mentioned, if runtime errors happen, they’ll likely happen to the users of your software app. That’s why it’s important to have a tool that makes reporting bugs quick and easy.
Shake is an excellent solution for that purpose.
If a tester or a user encounters a runtime error or any other type of error, they can just shake the phone and let the eponymous app do its magic.
It will automatically create a comprehensive bug or crash report with dozens of pieces of data relevant for developers, so they can see where the bug occurred and fix it without time-wasting.
That way, you can ensure that runtime errors don’t ruin your users’ experience by locating and eliminating the bug quickly and efficiently.
If you use a compiler, a compilation error is something that you should watch out for and eliminate if it occurs.
First, it’s always good to refresh our memory, so let’s break down what a compiler is and what it does.
A compiler is, in a sense, a translator that translates the source code into code that a computer can read.
Here’s how Artturi Jalli, a founder of the tech blog Codinger and a programmer himself, explains why compilers are useful.
In other words, when programmers write code in, for instance, Java, they use words, expressions, commands, etc.
A machine can’t read that code. It needs translation into binary code to understand it.
That’s what compilers do. Over a series of steps, they analyze the source code, generate an intermediate representation of the code for easier translation, optimize it, and create an output.
Bug and crash reporting tool for apps. That gets you all the data.
So, where do compilation errors come in? They occur when a compiler doesn’t know how to translate that source code into a lower-level code that a machine can understand.
Let’s take a look at this example below:
As you can see, the compiler returned a CompileError09, and line 13 is marked as unreachable code.
Why? The reason is simple—because the if and else statements are where they are, and they basically block the compiler’s access to line 13, so it can’t translate it for the machine.
If developers want to fix that problem, they would have to place the content from line 13 somewhere where it can be executed, like in the example below:
As you can see, compilation errors can be challenging to notice.
Usually, the cause of a compilation error is a syntax error—one misplaced parenthesis or non-existing semicolon can prevent a compiler from fulfilling its purpose and produce errors instead.
However, if you run it frequently, you’ll be able to notice compilation errors in time and eliminate them before they cause bigger problems.
Memory leaks can cause significant problems to an app, so it’s a type of error that you and your team should undoubtedly take seriously.
That error occurs when an app repeatedly allocates memory but doesn’t release it.
For instance, you can see how that happens in the visual below.
In the middle part, there are unused objects that use memory.
Since the memory isn’t infinite, the more it is allocated to such objects and never released, the sooner the program will slow down and eventually crash.
Prateek Narang, an instructor and a former Google software engineer, details what exactly happens during a memory leak:
In the worst-case scenario, too much of the available memory may be allocated, causing the system or device to malfunction entirely or partially, the programs may crash, or the system may significantly slow down.
There can be various causes of such memory leaks, from bugs in the code to incorrect usage of memory management techniques.
For instance, a developer can accidentally write an infinite memory loop in the code.
The result would be that the loop never stops, it continues to allocate memory each time it runs until, eventually, the program crashes.
Here’s one simple example of an infinite loop:
The loop is infinite when it’s always true and/or has no exit condition.
In the case above, the result would be printing the same statement over and over again, as long as there is memory in the system to do so.
Whatever might be the reason for a memory leak, it’s crucial that you and your team identify the source of it and fix it.
Otherwise, the app will run slowly, and it might crash often. You’ll be hard-pressed to find users that would tolerate that and wouldn’t leave your app for a better-performing competitor.
Debugging programming errors can be a time-consuming and challenging process, but one that’s essential for apps to perform as intended.
Errors are inevitable in programming, so it’s vital to recognize them and deal with them quickly.
Understanding the types of programming errors can help developers to approach them and choose the most efficient way to fix them.
We hope that this article can be a guide to improve your debugging efforts.