Navigating Errors in Programming: From Bugs to Brilliance
Vinay Kumar Sharma
AI & Data Enthusiast | Python & TypeScript | Full-Stack SSE | Seasoned Professional in SDLC | Experienced in SAFe? Practices | Laminas, Laravel, Angular, Elasticsearch | Relational & NoSQL Databases
Addressing errors is an integral facet of web development, transforming seasoned programmers into adept bug navigators. Edsger W. Dijkstra's wisdom encapsulates the sentiment: “if debugging is the process of removing bugs, then programming must be the process of putting them in.”
Today, we’re going to talk about the 7 most common types of programming errors & how you can avoid them.
1. Syntax Errors
Similar to human languages, computer languages adhere to grammar rules. Unlike humans, computers are intolerant of imperfections—syntax errors demand precision.
Consider a scenario: the correct syntax for printing is print('hello'). If, during coding, a parenthesis is omitted inadvertently, a syntax error surfaces, halting program execution.
As programming expertise grows, syntax errors occur less frequently. Early awareness is key to prevention. Many text editors or IDEs provide real-time syntax error alerts during code composition.
2. Logic Errors
Logic errors pose a formidable challenge in tracking them down. Everything appears operational, yet the programmed instructions yield the wrong outcomes. The program might be technically correct, but the results don't align with expectations.
Imagine this: Without verifying prerequisites, you write code to fetch the oldest user instead of the newest from your system—a classic logic error.
The infamous 1999 NASA incident exemplifies logic errors' impact. Miscalculations between English and American units led to a spacecraft loss. The software was coded correctly, yet its context required a different approach.
I've recently encountered one as well:
"Logical Exception - Please ensure the PHP Redis extension is installed & enabled."
When crafting tests, it's prudent to involve the product manager or owner to validate your logic. In the aforementioned example, a business-savvy individual might have highlighted the omission of specifying the need for the newest user.
Logic errors demand vigilance, collaboration, and meticulous testing to unveil their subtle missteps.
3. Compilation Errors
Certain programming languages involve a compilation phase—a transformative process where your high-level code transmutes into a lower-level counterpart that computers comprehend more adeptly. A compilation error, occurring during compilation or compile-time, emerges when the compiler grapples with translating your code to the lower-level language.
Consider the scenario from our syntax error example: while compiling print('hello', the compiler halts, expressing its inability to translate it due to the anticipated ) following the '.
When a compile-time error surfaces within your software, testing and launch become unattainable.
Similar to syntax errors, mastery in evading compile-time errors comes with experience. Yet, procuring early feedback remains the most efficacious strategy.
Compilation spans all files across your project simultaneously. When numerous alterations lead to an influx of compiler warnings or errors, the situation can seem overwhelming. Regular compiler runs provide swift feedback, guiding you to pinpoint and resolve issues more efficiently.
4. 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?params[:first_name].capitalize, 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.
Make sure you have good error reporting in place to capture any runtime errors and automatically open up new bugs in your?ticketing system. Try and learn from each bug report so that in future you can guard against this type of error.
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.
5. Arithmetic Errors
An arithmetic error is a type of logic error but involves mathematics. A typical example when performing a division equation is that you cannot divide by zero without causing an issue. Very few people would write 5 / 0, but you might not think that the size of something in your system might sometimes be zero, which would lead to this type of error.
ages.max / ages.min?could return an error if either?ages.max?or?ages.min?were zero.
Arithmetic errors can generate logic errors as we’ve discussed, or even run-time errors in the case of divide by zero.
Having functional tests that always include edge-cases like zero, or negative numbers is an excellent way to stop these arithmetic errors in their tracks.
6. 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.
while(true)
??my_array << 'new array element'
end
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.
Resource errors are an example of a type of error in programming that might be something for the operations team to fix rather than developers.
There are lots of load-testing applications and services that you can use to test what happens when multiple people try and run your code at once. Then, you can tune the testing to suit what is realistic for your application.
7. Interface Errors
Interface errors occur when there is a disconnect between how you meant your program to be used and how it is actually used. Most things in software follow standards. If input your program receives doesn’t conform to the standards, you might get an interface error.
For example, an interface error might happen if you have an API that requires that specific parameters are set and those parameters are not set.
Unless handled correctly, interface errors will look like an error on your side when it is an error on the caller’s side. This can lead to frustration from both sides.
Having clear documentation and catching these errors to pass back to the caller in a useful way is the best way of saying, “Hey, you haven’t given us what we need to process this request.” This will help keep support costs down and keep your clients happy because they know what they need to fix.
If you don’t catch these errors and pass them back to the caller, they will end up appearing like runtime errors in your reporting, and you will end up guarding against things excessively.
Errors Are Inevitable
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. Get better at spotting them early, but know you will never be perfect.
I believe this article 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!
Logistics & Supply Chain Professional
2 年Keep it up ?? Vinay Kumar Sharma
Founder @ Anakramy INC | Building Anakramy Guard ???
2 年Great article.. ??