There’s one common misconception I want to clear up. You see, programming languages are very strict but they are not as complex and substantial as spoken or written human languages. If you're a native English speaker and you wanted to learn Japanese, Norwegian or Mandarin, you'd expect to learn hundreds or even thousands of words before you'd be considered fluent.
Programming languages are very much smaller than that. Take Java for example. It has about 50 keywords. A keyword is a word that when you write it in your source code, the language itself says, okay, that word is mine. I know what that means. I know what it does. A language like Swift or C++ has somewhere between 50 and 70 keywords. Python, a famously concise programming language only has about 33 keywords.
Fig 2.7.1: A list of Python keywords
Now, the exact number is not very important, and I don't mean to suggest that if you learn 33 keywords you are now a Python expert. Not at all. It's not just the keywords. Although keywords are the words that belong to the language, they aren't the only words we need in our code.
We also bring our own words to the table. Here's what I mean. When I'm writing a program, whether a business app, a game or anything else, I could pick a programming language almost at random, whatever it is. I would expect that language to already include a bunch of keywords and also symbols that I can write in my source code to allow me to do things like add one number to another.
Sometimes we'd use a word like “add” in COBOL or “set” in Applescript. Sometimes it might be just symbols (what we call operators), such as the + or = signs. I can also expect a way to ask a question such as are two values the same, or is this number greater than that number?
Most often it's the keyword “if” we use to do this, when we're asking a question. We can tell the program to do one thing. If it's true and another thing, if it isn't. I also expect to find a way to write a message to whoever is running the program.
Of course, there are other keywords, particularly for managing the flow of a program. So, we can choose to execute different pieces of our code moment by moment. We will get more into this later.
Fig 2.7.2: How we use some keywords in a few programming languages
While there might be different names and different keywords, we still find all this basic generic ability. It's already there in every language. All these words were there before we came along. But we'll need more than this because if I'm writing say a game, I might want a word in my source code that represents the spaceship of my lead character. I could then write code to move it, rotate it or, in certain circumstances, make it explode.
Here’s another example. If I'm writing a business application for my company, I might want a word that means the list of our current international office locations grouped into which ones do or do not have kitchen facilities.
But you can search as much as you want. The keywords in Java, C++ or Python do not include the word “spaceship”. Likewise, there is no “office locations” keyword in Ruby, JavaScript or Swift. The thing is I still want those words in my source code.
Fig 2.7.3: A list of Java keywords
If I'm writing a game, I want to be able to write code to make the spaceship explode even if these words don't naturally exist in the language. If I'm writing a business app, spaceships unimportant, but instead I might want to write code to change the pay grade of an employee or generate a report.
I wouldn't expect these exact words to already exist in a programming language because what this means in my program is not the same as what this would mean to somebody else in a different organization. So, when the words we want aren’t in the language, we can still have them in our source code, but we will define them ourselves. We will do this all the time.
Here’s an example. Fig 2.7.4 is a little bit of Swift that might be found somewhere inside the source code of a game. The word if is part of the Swift language, it is a keyword. It was already there and we cannot change what it means. It’s a keyword you will find in virtually every programming language.
Fig 2.7.4: A sample Swift code highlighting a keyword (if) and the programmer’s words currentScore and highScore.
But words like currentScore and highScore are not keywords. They are not part of the Swift language. These are my words, my choices for two pieces of data or information I wanted to keep track of in the imaginary program in Fig 2.7.4. The fact that these exist is up to me and I could have called them something else.
Now you should understand what I mean about the difference between a keyword which has a predefined fixed meaning and a programming language which uses many of the other words that we define ourselves within each program.
But they do have to be defined somewhere else so I could use them at those spot in Fig 2.7.4. Now this brings the question, how do we say what these new words mean? How do we tell the computer whether these are numbers or texts or pieces of audio? How do we name them? What data, what information are we going to put into our program and what information are we expecting to get out of it? You’ll learn all that in the next section.