7 Most Common Programming Errors Every Programmer Should Know

Posted in

7 Most Common Programming Errors Every Programmer Should Know
sameekshamedewar

Sameeksha Medewar
Last updated on November 21, 2024

    Every programmer encounters programming errors while writing and dealing with computer code. They may seem frustrating for novice programmers. Meanwhile, seasoned programmers are very quick at locating and fixing them.

    Edsger W. Dijkstra, a well-known computer scientist, said:

    "If debugging is the process of removing bugs, then programming should be the process of putting them in."

    But what is an error? In computer programming, it is a flaw resulting in the abnormal behavior of a program. In other words, it is a mistake in a program that causes it to produce an unexpected outcome.

    Encountering and dealing with errors are common aspects of the development process.

    Even professional developers go wrong while writing the source code. Yet, they become comfortable and excel in locating and fixing those errors. We refer to the process as debugging .

    Well, there are different types of programming errors. If you are a novice programmer, you must be aware of the common ones. If you are not, you have landed in the right place.

    This article helps you understand and learn about some common programming errors.

    7 Common Programming Errors and How to Avoid Them

    • Syntax Errors

    Human languages have rules that define the way of speaking and writing. We refer to the rules as grammar. The same applies to programming languages . But we refer to those rules as the syntax in the programming context.

    Syntax is the set of rules defining a programming language's structure.

    However, humans can communicate with less strict grammar. This is not the case with computer languages. They strictly follow their syntaxes. A single mistake in the syntax results in a syntax error.

    Let us understand it with an example. We will print 'Hello World!' in Python.

    print("Hello World!") 

    Output

    Hello World!

    We have the output as we have used the correct Python syntax.

    Consider you used a curly bracket at the end.

    print("Hello World!"}

    Output

    SyntaxError: closing parenthesis '}' does not match opening parenthesis '('

    Instead of the expected output, you receive the above sentence as your output.

    The best way to avoid this is to learn the language’s syntax carefully. Even if you know the language's syntax, they may occur in haste while writing a program. The better way is to adopt a modern text editor that warns you about syntax errors as you write code.

    • Logic Errors

    Imagine you wrote a syntactically correct code. But, when you run it, you get a different output than expected. It is very tricky to track down what has gone wrong. Isn't it? This is what a logical error is.

    It is a kind of runtime error that forces a specific program to produce unanticipated output. Sometimes, it may result in the crashing of a program while executing.

    Logical errors in a program are tricky to identify. Though you have written the correct syntax, your program behaves differently. It means you have programmed a computer to perform the wrong things. It happens when you don't know the requirements beforehand.

    Let us understand with one example.

    Consider we need to calculate the average of two numbers.

    We have a formula - (number1 + number1)/(Total numbers)

    Let us write a Python program for the same.

    x = float(5)
    y = float(10)
    z = x+y/2
    print z

    Output

    10.0

    Is the output correct? Is the average of 5 and 10 10?

    Obviously, no. The correct answer is 7.5. But our output is 10.0.

    Though your program is correct according to the syntax, it is logically incorrect.

    The logic for calculating the average is wrong. The Python interpreter performs operations based on the operator precedence. It gives the first preference to the '/' operator and performs 'y/2' (10/2), which yields 5. Later, it performs 'x + (y/2)', i.e., 5+5 = 10.

    The correct logic is

    x = float(5)
    y = float(10)
    z = (x+y)/2
    print z

    Output

    7.5

    So, make sure to understand what you want as an output and write the program accordingly. After writing, ensure to recheck it. Also, you can ask your mentor or senior to review your code if you are working on a sensitive project.

    NASA lost its spacecraft due to miscomputations between English and American units. NASA engineers were expecting the Mars Climate Orbiter to return in September 1999. They sent it to Mars 10 months ago and were expecting to return. But it burned and broke into pieces. It is a famous example of a real-world logical error.

    • Compilation Errors

    Compilation is the process of converting high-level programming language into machine code. Computers do not understand the syntax of high-level programming languages. They can only interpret low-level code or binary code. So, there exists the compilation step.

    A software program called a compiler converts high-level code into machine-readable code. Sometimes, a compiler does not understand how to convert your source code into low-level code. It results in a compilation error.

    #include <iostream>
    usingnamespacestd;
    intmain() {
    int x = 5;
    int y = 10;
    int z = x + y;
    std::cout << "The addition is: "<<z
    return0;
    }

    Output

    main.cpp: In function 'int main()':
    main.cpp:7:40: error: expected ';' before 'return'
    7 | std::cout << "The addition is: "<<z
    | ^
    | ;
    8 | return0;
    | ~~~~~~

    I have added a screenshot of the program and the corresponding output. You can see that the compiler has indicated a failure in the compilation process. This is due to the missing semicolon in the print statement (std::cout).

    As a novice programmer, you will receive compilation errors often. You get better at avoiding them as you practice more. Frequently compile your program to get errors and warnings. If you compile a large program at once, you get a pile of errors at once. It can be daunting to resolve.

    • Runtime Errors

    Often referred to as bugs, they prevent a program from executing completely. This means they prevent you from using a specific application or computer. The logical error is a variant of it.

    A program looks perfect syntax-wise but highlights errors only during the execution or runtime. You do not receive them at the compilation step.

    The best example is division by zero. Let us understand it with an example.

    #include <iostream>
    usingnamespacestd;
    intmain()
    {
    int n = 9;
    div = n/0;
    cout<<"Result: "<<div;
    return0;
    }

    Output

    main.cpp: In function 'int main()':
    main.cpp:6:12: warning: division by zero [-Wdiv-by-zero]
    6 | div = n/0;
    | ~^~
    main.cpp:6:13: error: overloaded function with no contextual type information
    6 | div = n/0;
    | ^
    main.cpp:7:21: error: no match for'operator<<' (operand types are 'std::basic_ostream'and'')
    7 | cout<<"Result: "<<div;
    | ~~~~~~~~~~~~~~~~^~~~~

    The best way to deal with this is to have a better error reporting system in place. Also, learn from each runtime error you encounter to protect your code in the future. Use a framework for application development, as they provide a ready-to-use basic structure.

    • Arithmetic Errors

    They are logical errors but with mathematics. When your program includes the wrong arithmetic operation, you receive an arithmetic error. The best example is, again, the division by zero. It is not possible to divide any number by zero. I have explained it with an example above.

    Another example is getting a non-terminating long decimal after dividing one number by another.

    Let us take a Java example to understand this.

    import java.math.BigDecimal;
    publicclassArithmeticException1
    {
    publicstaticvoidmain(String[] argvs)
    {
    BigDecimal n1 = new BigDecimal(10);
    BigDecimal n2 = new BigDecimal(15);
    n1 = n1.divide(n2);
    System.out.println(n1.toString());
    }
    }

    Ensure to save the above program in a file with the same name as the class name.

    Output

    Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
    at java.base/java.math.BigDecimal.divide(BigDecimal.java:1722)
    at Main.main(Main.java:9)

    The problem here is a non-terminating digital expansion. You must limit the decimal number count in the output by rounding up to solve this problem.

    Here is a solution:

    import java.math.BigDecimal;
    publicclassArithmeticException1
    {
    publicstaticvoidmain(String[] argvs)
    {
    BigDecimal n1 = new BigDecimal(10);
    BigDecimal n2 = new BigDecimal(15);
    n1 = n1.divide(n2, 5, BigDecimal.ROUND_DOWN);
    System.out.println(n1.toString());
    }
    }

    Output

    0.66666
    • Resource Errors

    Your computer allocates specified resources to a particular program for execution. Sometimes, your program may need more resources. As a result, it forces the computer to allocate more. This results in a resource error.

    The best example is when you write a loop that iterates continuously without exiting. Your code runs out of resources in such a scenario.

    Here is a C++ example of a loop running infinitely:

    #include <iostream>
    usingnamespacestd;
    intmain() {
    for (; true; ) {
    cout << "Hello World!" << endl;
    }
    }

    Output

    The program prints “Hello World!” countless times. You can stop the execution by pressing Ctrl + C if working on a terminal or command prompt.

    For large programs and applications, the solution is to use load-testing solutions. They help you check how many users can simultaneously use an application. Hence, you can avoid the failure of the application before releasing it to the market.

    • Interface Errors

    They are the difference between expectations and reality. It indicates the differences between how the expected program output will look and how it is displayed.

    Most software follows specific standards. You get interface errors if the source code does not meet the standards.

    For instance, consider an API, which requires specific parameters to be set. You receive interface errors if those parameters are not set.

    Errors are Part of Programming!

    Software engineering is tricky, requirements are ambiguous sometimes, and code changes frequently. Programming errors are unavoidable. The best you can do is to learn to spot and fix them earlier. Practice is the only way to excel in fixing coding mistakes. The more you encounter errors, the more you gain knowledge to solve them.

    I hope this article helped you understand common errors in computer programming. If you have any queries, feel free to share them in the comments section below.

    People are also reading:

    FAQs


    Runtime, Logical, Syntax, Semantic, and Linker errors are the most common programming errors in C.

    Errors are faults in a program that causes it to behave abnormally or produce unexpected output.

    A semantic error is an erroneous logic that causes a program to produce an unexpected outcome.

    It is a runtime and arithmetic error. Anything divided by zero leads to infinity. There is no data structure in programming to store an infinite amount of data.

    Leave a Comment on this Post

    0 Comments