When teaching students, the question of how to name things comes up.
There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.
— Jeff Atwood (@codinghorror) August 31, 2014
The rules naming things like variables and functions is simple:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter
- Names can also begin with $ and _ (but we will not use it in this class)
- Names are case sensitive (rose and Rose are different variables, even if they both smell just as sweet)
- Reserved words (like JavaScript keywords) cannot be used as names, but compound names may contain JS reserved words such as: myVar
- Names cannot contain spaces
You can’t enclose a variable name in quotes. JavaScript does not allow for interpolating variables like PHP does. Interpolating is fancy computer speak for putting a variable in a string, finding its value and inserting it into the text instead of the variable name.
Variables are declared with the keyword var. Once a variable has been declared, it can have its value reassigned.
// example: var rose; rose = "florabundas";
Then there are naming conventions, that while not required are expected, such as camel casing:
// good examples (camel casing) var hereIsOneExample; var hereIsAnotherExample; // bad examples var hereisoneexample; var here_is_another_example;
Why “camelCase”? Because there is a hump or two (or more) in the middle if the name is formed by more than one word. A camelCase name begins in lower case. If there’s more than one word in the name, each subsequent words gets an initial capitalization, thereby creating a “hump”. A one word variable name gets no hump, but a compound word would. The intention of using camelCase is to make your variable names stand out from the rest of the code. This way if another programmer comes along to read it, they will recognize it as a variable (or a function name).
You will also want to make your variable and function names representative of what they are. Nouns are good for variables as they represent specific values. Verbs or action words should make up your function names as functions do things.
// example variables: var score = 0; var lives = 3; var message = "hello world";
// example function names: doMath(); birdFlap(); animateNextSlide();
Also giving variable and function names two words can help you avoid reserved words that are used in the construction of that language. Most people wouldn’t name a function, function – but there are keywords in every language that may make sense to you to use if you don’t know any better. For example, when I was learning MySQL I created a table with a field name: desc – short for description. I did not know at the time that would interfere with my queries on that table due to the use of a key/reserved word: desc – short for descending. It is the same in every language. Additionally, in languages with loose data types, like JavaScript, you can in error name a function and a variable the same name and suddenly your function does not work because it is no longer a function – but what you reassigned it to. Using nouns for data and verbs for functions can also prevent you from making that mistake.