Chill, you're not that bad its just an error

Chill, you're not that bad its just an error

You know when I started coding, I said to myself: I must have been very bad at English language because of my constant syntax errors even down to naming variables.

errorfirst.jpg

That feeling when you think you have written a clean code and it suddenly runs into so many errors and as you keep debugging ( finding the errors), you find new ones. We've all have those moments. The truth is, it never stops, you just get better at figuring out your errors.

errorsecond.jpg

In this article, I will discuss about:

  • the major types of errors in programming

  • some useful tips in reducing errors

The major errors in programming are:

  • Syntax errors

  • Logical and semantic errors

  • Runtime errors

  • Compiler errors

The examples I am going to be explaining with alongside each type are written in Python programming language.

Syntax errors: In relatable terms,I will call this grammatical errors. Syntax errors are born out of not following the rules as regards that programming language like naming a variable wrongly, not spelling a function well, not obeying the case sensitivity etc. Here's an example:

# No colon after the for statement so the code runs an error
numbers = [1,2,3,4,5]
for x in numbers
    print(x)
# Output: Syntax error

Logical and Semantic errors: Mr A said to Mr B, I actually want my code to return a dog but it outputs a cat, could it be that my computer is faulty? So here's what happens in logical errors, your code is actually correct and will run but it does not give the desired output. You have made a mistake in the program statement making use of a wrong formula or function (improper specification). Here's an example:

# Print a program that get the average of the two numbers
a = 3
b = 4
c = a+b/2
print(c)
# Output : 5 , which is not an average of 3 and 4
# Correct arithmetic formula is (a+b)/2 not a+b/2

Semantic errors occurs when the program statements are used incorrectly. Yes it is kinda similar to logical errors but while logical errors give you the wrong output, semantic errors produce nothing meaningful at all.

Then lastly,

Runtime and compiler errors: These errors basically occurs when the programs runs or compiles. It can be due to unexpected events like division by zero or lack of memory for the compiler to manipulate the data or even an infinite loop. Here's an example:

# Zero Division error
x = 5
y = 0
z = x/y
print(z)

# Output: Traceback (most recent call last):
  File "C:/Users/ASP/bit.py", line 3, in <module>
    z = x/y
ZeroDivisionError: division by zero

Runtime errors raise many built-in exceptions( something we expect might happen and want to catch). Here's a snippet for pythonistas to view all python's built-in exceptions and learn about them

print(dir(locals()['__builtins__']))

Some useful tips for reducing errors are:

  • Plan before you program

  • Comment your code

  • Test with data ( valid and invalid)

  • Make a habit of reading your errors so next time you'd try to avoid them.

Hey, so next time you run into an error, don't fret, just chill, you'd fix it !

error1.jpg

Thanks for reading! Feel free to share your thoughts!