Northcentral Technical College Introduction To Programming
10-152-310
Introduction to Programming with JavaScript NTC 10-152-310

Learning Unit 2 JavaScript Coding Standards

# JavaScript Coding Standards for 10-152-310

> Here are the coding standards for the course and a screencast overview. Your projects and labs will be graded partially on conformance to these rules. This document will be expanded as the semester progresses. The accompanying screencast is at the bottom or click here.

> One of the most important aspects of modern programming is readability. To be able to read code written by other people and understand it has become much more important than having condensed, optimal code. Modern programming languages can optimize your code better than you can. So, the number one criteria for the way we write is for other human beings to read it. These standards are about readability, making your could as readable as possible.

### 1. Indentation

> The unit of indentation of JavaScript source code will be four (4) spaces. Do not use the tab character. The course editor (jEdit) has been configured so that when you press the tab key it will insert 4 spaces instead of a tab. Keep this setting and if you use another text editor make sure you are not inserting tab characters into your code.

### 2. Line Length

> Avoid making any line of JavaScript code more than 80 characters. This improves readability. Sometimes this can't be avoided but when possible it is required to have 80 or fewer characters in a line of code. The course editor has been configured to show a vertical line at column 80 to help you with this standard:

### 3. Comments

> Good documentation will be required in your labs and projects files. Here is a minimal list of what the instructor will be looking for. Quality counts!

1. The student's name, section, and email at the top of lab and project files.

### 4. Variable Declarations

> Good variable naming and usage is very important when learning to program computers. Here is a list of the rules for variables:

1. Variables must follow the naming conventions outlined below.
2. All variables must be declared before they are used. Many programming languages require as part of their syntax. However, JavaScript does not require this so you need do this intentionally.
3. `var` statements should be at the top of your JavaScript code and functions.
4. Every variable should be declared on its own line.

### 5. Naming Conventions

> How things are named in a computer program is very important. In the early days of programming names were very short and cryptic to save on RAM usage. You may have seen variables with names like "i", "fnme", and "cal_prc". These days our computers all have lots and lots of RAM and we can stop this old confusing habit. Here are the course rules for naming things:

1. Names should be created with the 26 upper and lower case letters (A..Z, a..z) and the 10 digits (0..9).
2. Do not use the underscore "_", the dollar sign "$", or slashes "/ \" in names.
3. Variables should start with a lower case letter.
4. Abbreviations should be avoided, use complete words wherever possible, which is the vast majority of the time. What does "cal_prc" mean? Calculate Price? Calculate Percentage? Calender Perfection? Names need to be very clear, for others and for you. You won't remember your own cryptic names in the future.
5. Names that have more than one word should use the "camel case" convention. The name starts with a lower case letter and then each new word in the name starts with an upper case letter. Examples:

### 6. Semicolons

- Semicolons are used in JavaScript to end a line. Most of the time they are not required by JavaScript. However, they are required in a few places so as a course standard, that is also a convention in the industry, the use of semicolons at the end of statements is required.
- Examples

### 7. Statements

> The way we write statements is critical for code that is readable and easy to understand. Here is how we're going to write JavaScript statements.

#### Simple Statements

- Every simple statement must end with a semicolon.
- Every simple statement must be on its own line. This code will work but it fails the course standards in a couple of ways:
- On line 3 there are two statements. JavaScript doesn't care but your instructor and future professional teammates will.
- On line 5 the statement is missing the semicolon at the end. JavaScript won't remind you to put them there. You will just have to get in the habit.
- Here's the correct version:

### 8. Whitespace

> Whitespace can improve the readability of code. Here are the course standards for whitespace.

- Use blank lines liberally, your code shouldn't be all bunched together.
- Most operators should have a space on either side.
- A space should follow every comma ","
- A blank line should separate sections of your code.