Syntax errors are a common issue for beginners. That’s why I will give you some tips on how to fix and avoid them.
It is worth mentioning that once you have some experience, this type of error won’t be an issue at all for you. However, at the beginning of your programming studies, it can hold you back and be one of the main reasons for demotivation.
As you should know by now, demotivation can be devastating in any person life. So, let’s address this issue of syntax error so you keep motivated to go deeper into this amazing world of programming.
What is a syntax error?
According to Wikipedia “In computer science, a syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in compile-time. A program will not compile until all syntax errors are corrected. For interpreted languages, however, a syntax error may be detected during program execution, and an interpreter’s error messages might not differentiate syntax errors from errors of other kinds.”
Here we have two situations in which we will have to fix syntax errors:
- When we use a compiled programming language, like Java, C#, C, C++, etc.
- When we use interpreted languages, like Python, PHP, etc.
In the first case, you won’t be able to execute the program while in the second case, the program will run, but at a certain point you will get the syntax error and the program will halt.
The procedure to fix this type of error is similar in both cases.
How do we know when there is a syntax error?
It is actually very easy to know when the error you have is a syntactic error.
Programming languages are formal languages. This means that there are strict definitions to decide whether a “sentence” belongs to the language or not.
In natural languages (English, Spanish, etc.) it works similarly. Natural languages have grammar rules that you have to follow. However, the rules are not too strict like in formal languages (all programming languages are formal languages).
Why is it like that?
Here you have one reason: when you speak in English to someone, it does not matter if you made a small (sometimes big) grammar mistake, what matters is that the other person understands you. This usually happens because the other person can identify what you tried to say, even though you didn’t say it in the right way, considering the grammar rules for the language.
In programming, this won’t work. The first reason for that is that computer programs are not good at understanding your intent.
Nowadays, programs get better and better and can understand people intentions to a certain level. This level is very small when compared to the level a person can understand the intent of another person.
So, in the case of the computer program, the syntax (the use of the grammar) must be 100% correct.
Example of syntax errors
In English, the following text will have a grammar error: “this type of error won’t be an issue at all for your”. Can you identify the error?
If you write that sentence in Microsoft Word, the software will notice the error and will give you a tip on how to fix it. See the picture below.
In programming, it works exactly in the same way. See the example below.
In the previous example, I intended to print on the screen the text “My name is Rafael”. However, I can see that there is an error (red text).
If you hover the mouse on top of the red text, you will something like the picture below.
There, you can see what the problem is. Modern IDEs will give you options to fix this problem, as you can see below.
For this example, I’m using IntelliJ IDEA from google, but most of them work similarly. You put the mouse on top of the error, and on the left, you will see the red bulb, once you click it, you will see recommended solutions.
However, it is not always good to use these suggestions, especially if you are a beginner.
The reason is simple, computers are not good at understanding the intent of the person/programmer. The recommendations are based on the grammar of the language. Therefore, the better way is first, understand the message, then, we think on the best way to comply with the grammar of the language keeping our intent. In other words, writing the same thing we want to do, but in another way.
How to understand syntax errors?
The answer is straightforward, check the language documentation. Sometimes it can be difficult to find this type of error in the language documentation or even understand all the technicisms in this type of specification.
As you should be thinking right now, the quickest way is to “ask Google”. Syntax errors are common and easy to fix. Because they are common, it is highly probable that someone else wrote about how to fix them.
My advice is this one: don’t try to fix the error until you fully understand why the error is happening. Once you search for the error, read the whole explanation, and understand why it is happening in your code. Only after that, you should start trying to fix it.
Example
The error of our example above is “Cannot resolve symbol ‘name'”.
This means that there is a symbol (variable, constant, identifier) that you are using (‘name’ in this case) and the compiler (Java) cannot find it.
In Java (and compiled programming languages), you should define a variable before you can use it. So, the suggestion the IDE will give you is to create a variable ‘name’.
However, that is not what intended to do. You just want to print text on the screen. Therefore, creating a new variable called ‘name’ is not what you want.
In this case, I just missed what tells the compiler that I wanted to print a text. If we use the programming jargon, I wanted to print the string “My name is Rafael”. Strings are always specified between quotes, and that is exactly what I missed.
So, the solution is to write the text we want to print between quotes.
As you can see, in this analysis to fix a syntax error we always must think about the intent of the code we wrote.
I recommend you, if you are starting in the world of programming, to read this short introductory course to Object Oriented Programming. You will find there the basic knowledge you need to start in this awesome world of programming.
If you have problems with some concepts, you can also check this illustrated guide of Object-Oriented Programming main concepts.
H@ppy coding!