Syntax Errors
Every programming language has its own syntactical quirks. The C family loves braces. Python loves indentation. BASIC loves words. And Perl loves #%@!{}]&.
This is part 1 of a 7-part series comparing different kinds of software bugs.
What is a Syntax Error?
A syntax error is any invalid sequence of characters in your code that cannot be parsed.
Parsing (a.k.a. syntax analysis) is a rather deep topic in computer science. The first step in parsing most languages is to convert the individual characters into lexical tokens. Most syntax errors arise from improper ordering of these lexical tokens.
For example, if you forget to add the enclosing parenthesis when declaring a procedure, that is a syntax error:
In VBA, syntax errors are shown in red text by default. You can change this setting by going to Tools > Options > _| Editor Format |_
Syntax errors are the easiest bugs to fix because they are so hard to miss. In fact, by default, VBA throws up a message box any time you move your cursor off of a line that contains a syntax error.
I find this default behavior quite obnoxious. It's one of the first options I change on a new installation of VBA. To disable the behavior, go to Tools > Options and then uncheck the box next to Auto Syntax Check.
Causes of Syntax Errors
There are two main causes of syntax errors in programming:
- Learning a new language
- Typos
Learning a new language
Every programming language has its own syntactical quirks. The C family loves braces. Python loves indentation. BASIC loves words. And Perl loves #%@!{}]&.
Every language comes with a learning curve. The leading edge of that curve is understanding the syntax. I find it takes me about two weeks of working in a language every day to become completely comfortable with the syntax.
During this two-week learning period, leaving the Auto Syntax Check option enabled might make sense. Once you become fluent in VBA, though, you'll want to turn that option off before you throw your computer through the window*.
* Yes, I said "through" the window, not "out" the window. I find the Auto Syntax Check so obnoxious that–if forced to use it–I would not even take the time to open the window prior to the defenestration. I would hurl my device with such force that it would shatter the glass into a million pieces. The gentle tinkling of the shards as they tumbled to the ground would soothe my savage state.
I may have an irrational hatred of the Auto Syntax Check feature.
Typos
After the first two weeks of writing code–in any language–the main source of syntax errors will be typos: accidentally hitting Enter before adding a closing parenthesis; using a single-quote in place of a double-quote because your right pinky got to the quote key before your left pinky could hold down the Shift key; bumping the keyboard with your face as you reach down to pull bits of broken glass from between your toes (it was still worth it); etc.
Consequences of Syntax Errors
Annoyance. That's pretty much it.
It's close to impossible for a syntax error to slip through the cracks and end up in your production code, especially if you are explicitly compiling your code on a regular basis (Debug > Compile or [Alt] + [D], [L]).
External references
Referenced articles
Image by Vladan Rajkovic from Pixabay