Programming: Syntax and Semantics

    Programming: Syntax and Semantics

      Things you probably take for granted: syntax and semantics.

      Things you probably shouldn't take for granted: syntax and semantics. Both are an essential part of language—no matter which one you're speaking in. Semantics is the art of being able to translate a complex web of thoughts into something understandable to other people.

      In a perfect world, we'd all talk like Spock and be very precise in our language. But we're usually too tired, hungry, or busy to do all those things. Or we don't realize how ambiguous our language is.

      Say your best friend Tony came to visit after a long hard day at the baseball-testing factory. You're thirsty and ask:

      “Hey, Tony, pass me that water bottle, please.”

      If Tony's pass of choice is an overhand throw and his target of choice is that antique lamp on the table, you may need to be a little more specific about how Tony should carefully hand the water bottle to you. The fate of the lamp isn't either you or Tony's fault, though: it's the imprecise language. (Yeah, we know, try arguing that one to your mom.)

      Unlike Tony, who picked the wrong interpretation of your words, a computer can't handle ambiguous language. It's built on "yes" or "no" answers; it has no idea how to handle "maybes" or "sort-ofs." That's why we need programming languages in the first place. We need a way to translate the inherently ambiguous into incredibly precise. Otherwise, we're left with a broken lamp and six months of no TV.

      Some of the most common semantic code words in programming are

      • FOR
      • IF-ELSE
      • WHILE
      • any type declaration (like int, boolean, String, char, etc.)

      But just as much as having the right words, putting the words in the right order is important. Even with the right words and the right symbols, instructions don't make sense if they're out of order.

      That's why we need the syntax in this "Syntax and Semantics" combo. Syntax tells us what order the right words need to go in so that they make sense—both to a person and a computer. “Tony, pass hey. bottle please water me that” might spare the lamp from being destroyed, but probably won't solve your thirst issue.

      Punctuation

      Using the right punctuation is about as important as using good syntax. In written human languages, we use punctutation to help us separate ideas into sentences. Just how important is punctuation, really?

      Your friend Tony brings Thor (his pet honey badger) to your house on movie night. Let's just say it doesn’t like the movie.

      Two hours, more screaming than you care to admit, four destroyed couch cushions, and one honey-badger-on-the-loose later, you decide to text your mother to explain what happened. What do you say?

      Option One: We tried to watch the movie Thor, went crazy, ate most of the couch cushions, and tried to eat the neighbor’s cat.

      Option Two: We tried to watch the movie. Thor went crazy, ate most of the couch cushions, and tried to eat the neighbor’s cat.

      Punctuation won’t get you out of paying for more cushions, but it just might save your mother from a heart attack and you from some quality time on a psychiatrist’s couch. Programming will almost never involve crazed honey badgers, but it will involve punctuation. Loads of it.

      In programming, statements—the lines of code—are separated by terminators. Terminators tell the computer when one line of instructions ends and another begins.

      Terminators are typically semicolons in most programming languages, but they can sometimes be whitespace (we're looking at you, Python), so ask your programming doctor today which terminator is right for you.

      Outside of statements, you're going to need to tell the programming language about blocks of code: lines that all run at the same time. These can be for conditionals, iterations, or functions, but they're always going to need a way for the program to know that all of the code between a specific number of lines should be run together. They're most often found as braces({}) or indentation. Mainly braces.

      Hopefully, with all these things, your friend Tony will stop destroying your house. Or maybe you should stop inviting him over.