Experiencing different types of errors in programming is a huge part of the development process. The best developers become comfortable navigating the bugs they create and quickly fixing them.
Today, we’re going to talk about the seven most common types of programming errors and how you can avoid them.
Types of Errors:
1). Syntax error:
Just like human languages, computer languages have grammar rules. But while humans are able to communicate with less-than-perfect grammar, computers can’t ignore mistakes, i.e. syntax errors.
For example, let’s say the correct syntax for printing something is <span style="font-size: 10.0pt;">print('Hi!How are you')</span>, and we accidentally forget one of the parentheses while coding. A syntax error will happen, and this will stop the program from running.
2). logic error
Logic errors can be the hardest to track down. Everything looks like it is working; you have just programmed the computer to do the wrong thing. Technically the program is correct, but the results won’t be what you expected.
If you didn’t check the requirements beforehand and wrote code to return the oldest user in your system when you needed the newest, you would have a logic error.
When writing your tests, show them to the product manager or product owner to confirm that the logic you’re about to write is correct. In the example above, someone closer to the business would have spotted that you aren’t mentioning the fact it is the newest user that is required.
3). Runtime Errors:
Runtime errors happen as a user is executing your program. The code might work correctly on your machine, but on the webserver, there might be a different configuration, or it might be interacted with in a way that could cause a runtime error.
If your system took the input from a form and tried to capitalize the first letter of a name by doing something like <span style="font-size: 10.0pt;">params[:first_name].capitalize</span>, this would break if the form was sent without a first name.
Runtime errors are particularly annoying because they directly impact your end user. A lot of these other errors will happen when you’re at your computer working on the code. These errors occur when the system is running and can stop someone from doing what they need to do.
Making use of frameworks and community maintained code is an excellent way of minimizing these types of errors because the code is in many different projects, so it will have already encountered and fixed many issues.
4). Resource Errors:
The computer that your program is on will allocate a fixed amount of resources to the running of it. If something in your code forces the computer to try and allocate more resources than it has, it can create a resource error.
If you accidentally wrote a loop that your code could never exit from, you would eventually run out of resources. In this example, the while loop will keep on adding new elements to an array. Eventually, you will run out of memory.
my_array << ‘new array element’
Resource errors can be hard to chase down because the machine you’re developing on can often be higher quality than the servers running your code. It is also hard to mimic real-world use from your local computer.
Having good reporting on resource usage on your web servers will flag code that is consuming too much of any type of resource over time.
Errors Are Inevitable
We are, thankfully, decades past needing to have perfectly placed punch-cards appropriately done the first time. Software engineering is hard, requirements are often fuzzy, and the code changes often. Try not to beat yourself up and know that we all make mistakes.
Programming errors are inevitable. Get better at spotting them early, but know you will never be perfect.
Hopefully, this guide has prepared you for the different types of errors in programming and made sense of some of the most common error messages for you.
If you’ve been writing code for a long time, please comment below with some errors you’ve made recently, to help serve as reassurance for people who haven’t been writing code as long!