What is syntax and what does it mean for somebody learning a new programming language?
Syntax really is just the basic rules of how the language is structured. Does it use special characters to end each line or each expression? Does it use indentation levels and empty spaces to mean something special? Does it reserve special words for its own use? These are called keywords and most programming languages have them.
Programming languages need these special and very specific rules in order to avoid any misunderstanding about what we want the computer to do.
Languages also provide extra capability in the form of standard libraries and frameworks. Because these libraries are so useful and common, it’s easy to confuse them with the syntax of the language itself.
Listen to the full episode or you can also read the full transcript below.
Thank you for your review and comments. The thing I liked about this review was the encouragement to others to learn how to program. It really is becoming a basic skill that we’re all going to need. If you know anybody who would like to learn how to program, then let them know about this podcast. I put a lot of effort into producing each episode so that you can get the most benefit from an organized and information packed 10 to 15 minutes.
Okay on to the question this week. This actually came up in the live class last week. One of the new students was thinking that the language syntax included a lot more than it really does.
I don’t always know what you’re struggling to understand. There’s something called the curse of knowledge that makes it very difficult for any of us to remember the things we struggled with once we finally understand a topic. Given enough time, those early challenges will be completely forgotten. If you’re having difficulty with something, then others are probably having the same trouble. Taking the time to ask a question is a great way to get an answer to your situation and help others too.
Let’s start with the reason why syntax is important. Computers are very precise and do exactly what we tell them even if that’s not at all what we meant. This means we need a way of expressing our intentions that’s just as precise. Each programming language is different and best suited for some tasks more than others but they all share one thing in common – they are precise.
Imagine coming home one day with your hands full and asking a family member, “Can you help me for a second?” The other person just smiles and says, “Yes.” You get irritated and revise your statement to, “Would you please help me for a second?” The other person says , “Sure,” and starts walking toward you, then stops and says, “Sorry, the second’s over.” You may be getting quite mad by now. We’ve all encountered this situation and probably from both sides at some point. What I’m trying to say is that our spoken language is so full of opportunities to misunderstand each other accidentally or on purpose that it’s a wonder we can communicate at all. There’s no way a computer will ever understand this without some major advancements that still have a long way to go before being widely available.
Until then, we have to be extra careful when programming computers and that means that we have to follow a language that’s been carefully designed to remove all traces of misunderstanding. Learning a language like this can sometimes be more difficult than learning a new spoken language because we’re not used to needing to be so specific.
Programming languages have all developed rules for exactly what characters are allowed and what they mean. This is the language syntax.
I’ll explain more about this and another topic that’s often confused with syntax right after this message from our sponsor.
( Message from Sponsor )
Programming languages are different but also share some basic abilities. For example, most programming languages offer the ability to the following four basic tasks:
◦ #1 You need to be able to make basic decisions. Should you go left? Or right? Or maybe stay where you are?
◦ #2 You need to be able to keep track of values and read and modify them when needed. This could be as simple as basic numeric values or as complicated as hurricane simulation data.
◦ #3 You need to be able to perform some steps again and again either by going directly to some spot in the code or entering a loop or by calling a method that wraps up those steps.
◦ #4 And you need some way to start and stop your program as well as do nothing. Yes, doing nothing is sometimes important.
There’s a concept called Turing Completeness that really only needs the first two abilities that I just listed. Some common languages that are not Turing complete include XML, HTML, and JSON because these languages only allow you to describe data or markup information. There’s no behavior and no decision making involved.
But even though HTML, for example, is not Turing complete, it still has its own special syntax. You’re not going to avoid the need to be absolutely specific with computers just because you remove behavior.
So syntax really is just the basic rules of how the language is structured. Does it use special characters to end each line or each expression? Does it use indentation levels and empty spaces to mean something special? Does it reserve special words for its own use? These are called keywords and most programming languages have them.
If all we have to get started when writing an application is the language and its syntax, then we’d all be in big trouble. You need a lot more than that and this is where most languages provide extra functionality that you can make use of.
This is called a library or sometimes a framework. The difference between them is usually a matter of scope and integration. Frameworks tend to be bigger and encompass multiple libraries and give a common theme to the whole system. Although some libraries can be quite large too. There’s no solid line that separates a library from a framework, just a general guideline.
Regardless of what this extra functionality is called, most languages provide you with code that you can use in your application that will help you perform basic tasks. Maybe you need to open and read information from a file on the hard drive. Or open a communication channel with another computer. Or draw some text or graphics on a display screen. Or organize your data into a binary tree. These are all useful and necessary to writing applications.
Because they’re so necessary and almost all programs make use of this additional functionality, it’s easy to think of this as part of the language syntax. But it’s not. You could if you wanted to write your own versions of all this. It’s just code that’s already been written to help you get started.
That doesn’t mean that is not standardized? Most languages follow a specification that defines how the language works and how to use it. These libraries and frameworks that come with the language are usually part of this standard too. That’s really important if you want to write a C++ program, for example, and expect it to run on different platforms. You can do this because C++ follows its standard language syntax as well as its standard libraries.