1.3 Documentation and Coding Conventions
SLO 1
Describe the software development life-cycle.
Documentation and coding conventions are the discipline that keeps software maintainable across the life-cycle. Choosing descriptive names, following team conventions, and avoiding variable reuse make construction readable and deployment/support phases sustainable — the habits the life-cycle depends on.
SLO 2
Describe the principles of structured programming.
Structured programming is code that reads clearly: one variable, one purpose, names that communicate intent. This section teaches exactly those habits, so the student learns the structured-programming principles as concrete naming and reuse rules before control flow is added.
Learning Objectives
After this section, you will be able to:
- Choose clear, descriptive names for variables that communicate their purpose.
- Explain why reusing variables for different purposes makes code harder to debug.
- Follow team naming conventions to keep code consistent and readable.
- Write comments that explain why code does what it does, and recognize when a comment is doing more harm than none at all.
A variable name should have a clean, obvious meaning. It should describe the data it stores. Variable naming is one of the most important skills in programming. Looking at variable names can often tell you whether the code was written by a beginner or by someone with experience.
1.3.1 Naming Variables Well
Good naming is not about following rigid rules -- it is about communicating intent. When you read let totalPrice = 29.99;, you know immediately what that variable holds. When you read let x = 29.99;, you have to guess. Code is read far more often than it is written, so spending a few extra seconds on a good name saves minutes of confusion later.
Definition 1.3.1 — Variable naming rules: use human-readable names, avoid bare abbreviations, keep names descriptive but concise, and agree on terms.
Good variable names follow these guidelines:
- Use human-readable names like
userNameorshoppingCart. Someone reading your code should understand what the variable holds without needing a comment. - Avoid abbreviations and short names like
a,b, andc, unless you have a very good reason. A single letter tells the reader nothing. - Make names descriptive but concise. Names like
dataandvalueare too vague. They say nothing about what the data or value actually is. Only use them when the surrounding code makes the meaning completely obvious. - Agree on terms. If your team calls a site visitor a "user," then name your variables
currentUserornewUser, notcurrentVisitorornewManInTown. Consistent terminology prevents confusion.
Compare these two snippets. Both do the same thing, but one is much easier to understand.
Hard to read:
let a = "John"; let b = 90; let c = a + " scored " + b + "%";
Now write the same three lines with names that explain themselves -- each comment is one line to write:
▶ Press Run to see the output…
Solution
▶ Press Run to see the output…
The second version tells you what each variable means without any extra explanation. Nothing about how it runs has changed -- only how fast a reader understands it.
The Context Pause above is about what a name does for the person reading your code. This one is about what it does for the computer, which is nothing at all.
A name is a label you attach to a value. JavaScript stores the label and the value and never once checks that they agree. Predict what this prints:
▶ Press Run to see the output…
Solution
3 10 banana
Every line is accepted without complaint. pi is 3, because you said so. It is
not 3.14159; JavaScript has never heard of the number pi and has no opinion
about what a variable called pi ought to hold. average is 10 without
anything having been averaged. total holds the word banana.
The names promised a mathematical constant, a computed result, and a sum. The program delivered a wrong number, an unearned number, and a fruit, and ran perfectly while doing it.
This is why a wrong name is worse than a vague one. A variable called x tells the next reader nothing, and they know it — they will go and look. A variable called total that does not hold a total tells them something false, and they will believe it, because there is no reason not to.
The computer will never correct a misleading name for you.
No error appears, no warning, nothing turns red — the name and the value simply
disagree, quietly, for as long as the code exists. Naming is one of the few
things in programming with no safety net at all, which is exactly why it is
worth the extra few seconds.
Look at these variable names. Which ones are good? Which ones are bad? Why?
let x = 5; let numberOfStudents = 5; let d = "Monday"; let currentDay = "Monday"; let temp = 98.6; let bodyTemperature = 98.6;
Type it into the editor and run it:
▶ Press Run to see the output…
Solution
x-- bad. What is x? A count? A price? A score? No way to tell.numberOfStudents-- good. Clear and descriptive.d-- bad. Could be anything: a day, a distance, a date.currentDay-- good. Tells us exactly what it holds.temp-- borderline. Short for "temperature" is common, butbodyTemperatureis clearer.bodyTemperature-- good. No ambiguity.
1.3.2 Reuse or Create?
Some programmers try to save effort by reusing the same variable for different purposes. Instead of declaring a new variable, they change the value of an existing one to hold something completely different.
This is a bad habit. Imagine a box labeled "books" that you keep using to store shoes, then dishes, then toys, without changing the label. After a while, nobody knows what is actually inside.
▶ Press Run to see the output…
▶ Press Run to see the output…
Reusing variables saves a tiny bit of typing but costs much more time in debugging. When you reuse a variable, you have to keep track of what it holds at every point in the program. One wrong assumption and you introduce a bug that is hard to find. Modern JavaScript tools (minifiers and browsers) optimize code so well that using extra variables does not hurt performance. In fact, using separate variables for different values can even help the engine run your code faster.
The following code reuses a variable. What is confusing about it? Rewrite it using separate variables with clear names.
let x = 100; console.log(x); x = "Alice"; console.log(x); x = true; console.log(x);
Type it into the editor and run it:
▶ Press Run to see the output…
Solution
The problem: x holds a number, then a string, then a boolean. By the time we reach the third console.log, we have no idea what x is supposed to represent.
Better version:
▶ Press Run to see the output…
Each variable has one clear purpose, and the names tell us what that purpose is.
1.3.3 Comments
A comment is text inside your program that JavaScript ignores completely. It is there for people.
JavaScript has two ways to write one:
▶ Press Run to see the output…
Only 4.75 is printed. Every commented line was read by you and skipped by JavaScript.
Comment the why, not the what
The commonest mistake is a comment that says what the next line already says:
▶ Press Run to see the output…
That comment costs a line and adds nothing. Anyone who can read score + 5 can see that 5 is being added. Worse, it now has to be maintained: change the 5 to a 7 and the comment is wrong.
A useful comment answers the question the code cannot:
▶ Press Run to see the output…
The code says what happens. The comment says why it should, and that is the part no reader can recover by staring harder at the line.
The same rule catches the other common case, a value that looks arbitrary:
▶ Press Run to see the output…
Without the comment, the next person has to guess whether 35 is a rule they may change or a number somebody else decided.
A comment can lie
Section 1.3.1 ended on a name being a promise the computer never checks. A comment is that same bargain, one step further. It is a whole sentence, and nothing in JavaScript reads it, tests it, or notices when the code beneath it changes.
▶ Press Run to see the output…
The comment says the price includes tax. The code adds 8% tax to it. One of those is wrong, and the program runs perfectly either way, at full speed, forever. A reader who trusts the comment will be confidently wrong about what this code does.
That is why a stale comment is worse than no comment. Missing documentation leaves a reader knowing they have to work something out. Wrong documentation leaves them believing they already have.
Commenting out code
Comments have a second job. Putting // in front of a working line switches it off without deleting it, which is how you test whether a line is the one causing trouble:
Two habits keep comments honest. Write them to explain a
decision rather than to narrate a line, because decisions change far less
often than code does. And when you change a line, look immediately above it
before you move on — that is the moment a comment goes stale, and the only
moment anyone is in a position to notice.
▶ Press Run to see the output…
The discount line is still there, still readable, and not running. Turn it back on by deleting the two slashes.
This is a tool for while you are working, not a way to store old code. Code commented out and left behind becomes a puzzle for the next reader: they cannot tell whether it is a mistake, a half-finished feature, or something that must never run again. Delete it once you know the answer.
The comments below are all the unhelpful kind — each one restates its line. Rewrite the three that are worth keeping so they explain a decision instead, and delete the one that should simply not exist.
▶ Press Run to see the output…
Solution
One reasonable answer:
▶ Press Run to see the output…
cartTotal needed no comment at all. The name already says what it holds, and
// set cartTotal to 120 says nothing the line does not.
The other three each carry something the code cannot: where a policy came from, what a bare number means and when it goes out of date, and what a half-finished value is waiting for. None of them describes the syntax, and none of them will be wrong tomorrow because a number changed.
Problem Set 1.3
1.3.1 Why is let a = 5; a poor variable name? What would be a better name?
Solution
Step 1 — Reading the code:
let a = 5; creates a variable named a and stores the number 5 in it. The code works fine — JavaScript doesn't care what you name a variable.
Step 2 — Why the name is poor:
The name a tells you nothing about what the value 5 means. Is it a score? A price? A temperature? A person's age? When you (or a teammate) read this line later, you have to hunt through the rest of the code to figure out what a is for. A good variable name should describe the value it holds.
Answer: a is a poor name because it gives no information about what the value means. A better name describes the value, for example let score = 5; or let age = 5; — whatever the 5 actually represents.
1.3.2 What is the problem with reusing a variable to hold different kinds of values?
Solution
Step 1 — What "reusing a variable" means: Reusing a variable means storing one kind of value in it, then later storing a different kind of value in the same variable — for example, first a number, then a string, then a boolean.
Step 2 — Why it causes problems: When you read code that reuses a variable, you can never be sure what kind of value it holds at any given moment. That makes the code confusing and easy to break: a line that expects a number might get a string, and the program produces a wrong result or crashes. It also hides the program's meaning — a variable that holds "a name" and then "a price" doesn't represent either one clearly.
Answer: Reusing a variable for different kinds of values is a problem because it makes code confusing and error-prone — readers can't tell what the variable holds, and operations that expect one kind of value may silently get another. Each variable should hold one kind of value, with a name that says what it is.
1.3.3 Your team calls a logged-in person a "member." Which variable name is better: currentMember or currentPerson? Why?
Solution
Step 1 — What the two names say:
currentMember says the variable holds the person currently logged in, described as a member. currentPerson says the same thing, but described as a person.
Step 2 — Matching the team's vocabulary:
Your team calls a logged-in person a "member" — that's the word everyone on the team already uses when talking about the code. When a variable name matches the team's own language, anyone reading the code instantly connects it to the concept they already know. currentPerson is not wrong, but it introduces a second word for the same idea, which can cause confusion.
Answer: currentMember is better, because it matches the team's vocabulary — the team calls a logged-in person a "member," so the variable name should use that same word. Names that match the language of the people working on the code are the clearest.
1.3.4 Rewrite this code with better variable names:
let a = "Math"; let b = 88; let c = a + " final grade: " + b + "%"; console.log(c);
▶ Press Run to see the output…
Solution
Step 1 — Figuring out what each variable holds:
aholds the name of a subject:"Math".bholds a numeric grade:88.cholds a sentence built from the other two:"Math final grade: 88%".
Step 2 — Choosing descriptive names and rewriting:
Give each variable a name that says what it holds — subject, grade, and message (or finalGrade). The code does exactly the same thing, but now a reader understands it at a glance:
▶ Press Run to see the output…
The behavior is identical, but the meaning is now clear.
1.3.5 True or false: Using extra variables slows down your program. Explain.
Solution
Step 1 — What the claim says: The claim is that every extra variable you create makes your program run slower, so you should use as few variables as possible.
Step 2 — What actually happens: Modern JavaScript engines are very good at optimizing. They figure out what a variable is used for and often remove or reuse it internally — so an extra variable usually costs nothing at runtime. In fact, extra variables can make a program faster to develop and maintain, because clear names help you (and others) understand and fix the code more quickly. The real cost of a variable is not speed — it's clarity, and clear code is worth the tiny cost.
Answer: False. Extra variables do not meaningfully slow down your program — modern engines optimize them away. They can even help, because well-named variables make code easier to read, understand, and fix, which saves far more time than a few extra variables could ever cost.
1.3.6 Which of these names follow the "descriptive and concise" guideline: x, userName, data, num, totalPrice, abc? Explain your choices.
Solution
Step 1 — Understand the guideline:
"Descriptive and concise" means a name should tell you what the value is (descriptive) without being long or wordy (concise). A good name like userName reads like a sentence: "this is a user's name." A bad name either says nothing (x, abc) or is so generic it could mean anything (data, num).
Step 2 — Check each name against the guideline:
userName— follows the guideline. It clearly holds a user's name, and it's short.totalPrice— follows the guideline. It clearly holds the total price, and it's short.x— does not. It is a single letter with no meaning.data— does not. "Data" could be anything — a list, a number, a string. It is too generic.num— does not. It is an abbreviation that does not say what number it holds.abc— does not. It is a meaningless string of letters.
Answer: userName and totalPrice follow the "descriptive and concise" guideline. x is too vague, data is too generic, num is an abbreviation with no context, and abc is meaningless.
1.3.7 The following code uses a name that is too vague. Rewrite the code with better variable names:
let n = 3; let d = 2.5; let p = n * d; console.log(p);
▶ Press Run to see the output…
Solution
Step 1 — Figure out what each variable actually holds:
Read the code and ask: what real-world thing is each value? n is set to 3 — a count of items, so it is a quantity. d is set to 2.5 — a price per item, so it is a price per item (or unit price). p is the result of multiplying them — the total price.
Step 2 — Rewrite with descriptive names: Replace each vague name with a name that says what the value is:
▶ Press Run to see the output…
Now the last line reads like a sentence: "total price equals quantity times price per item." Anyone can understand the code without guessing.
Answer: The rewritten code uses quantity, pricePerItem, and totalPrice — each name describes exactly what the variable holds, so the code explains itself.
1.3.8 Your team calls a shopping cart a "basket." A teammate's code uses cartItems. Your new code uses basketItems. What is the problem, and what should you do?
Solution
Step 1 — Identify the problem:
Your team has agreed to call a shopping cart a "basket," but your teammate's code uses cartItems while your new code uses basketItems. Both names refer to the same thing — the same concept, the same data. This is an inconsistent naming problem: two names for one concept. Anyone reading the code will wonder whether cartItems and basketItems are different things, and future changes (like renaming one) become risky and confusing.
Step 2 — Decide what to do:
The team's word is "basket," so the team convention wins: the code should use basketItems. You should talk to your teammate, agree on the single name, and update the teammate's code to use basketItems everywhere. The goal is one concept, one name, used consistently across the whole codebase.
Answer: The problem is inconsistent naming — the same concept is called cartItems in one place and basketItems in another. The fix is to agree on the team's name (basketItems, since the team says "basket") and update the teammate's code to match, so the whole team uses one name for one concept.
1.3.9 Match each variable name to its problem — temp, x, a1, myVariable:
- Too generic: ____
- Abbreviation with no context: ____
- Numbered name that says nothing: ____
- Meaningless but harmless-sounding: ____
Solution
Step 1 — Review each name and its weakness:
x— a single letter. It gives you no hint at all about what the value is. That is too generic.temp— short for "temporary." It is an abbreviation, and on its own it gives no context about what is being stored (a temperature? a temporary value?). That is an abbreviation with no context.a1— a letter plus a number. It says nothing about the value; it is just a label. That is a numbered name that says nothing.myVariable— it sounds fine and harmless, but "my" and "variable" carry no information about the value. That is meaningless but harmless-sounding.
Step 2 — Fill in the blanks: Match each name to its problem:
- Too generic:
x - Abbreviation with no context:
temp - Numbered name that says nothing:
a1 - Meaningless but harmless-sounding:
myVariable
Answer: Too generic: x — Abbreviation with no context: temp — Numbered name that says nothing: a1 — Meaningless but harmless-sounding: myVariable. All four fail the "descriptive and concise" guideline, just in different ways.
1.3.10 Write two short code snippets that each store a student's name, score, and whether they passed. The first should use poor variable names (n, s, p), and the second should use clear, descriptive names. Compare how easy each is to read.
▶ Press Run to see the output…
Solution
Step 1 — Write the first snippet with poor names: Store a student's name, score, and whether they passed using vague one-letter names:
▶ Press Run to see the output…
This works, but it is hard to read. What is n? What is s? What does p mean? A reader has to guess — or worse, a reader might guess wrong.
Step 2 — Write the second snippet with clear names, then compare: Store the same data with descriptive names:
▶ Press Run to see the output…
Now compare the two. In the second snippet, each line explains itself: studentName holds a name, score holds a score, and passed clearly holds whether the student passed. The line passed = score >= 60 reads like a sentence: "passed is true when the score is at least 60." The first snippet forces the reader to memorize what n, s, and p stand for; the second snippet needs no memorization at all.
Answer: The second snippet is far easier to read because studentName, score, and passed describe their values, while n, s, and p hide them. Good names let the code explain itself — that is why the "descriptive and concise" guideline matters.
Key Terms
| Term | Definition |
|---|---|
| coding convention | A set of guidelines for writing code that is consistent and readable. |
| descriptive name | A variable name that clearly communicates what the variable holds. |
| variable reuse | The practice of using the same variable for different purposes, which makes code harder to understand and debug. |
| comment | Text inside a program that JavaScript ignores, written for people rather than for the computer. |
| stale comment | A comment that no longer matches the code beneath it, because the code changed and the comment did not. |
Table: Table 1.3.1 — Key terms introduced in this section, with their definitions.