Skip to content

Latest commit

 

History

History
163 lines (125 loc) · 5.78 KB

File metadata and controls

163 lines (125 loc) · 5.78 KB

Style Guide

[toc]


❓ "Hey? Where's C01.About This Book.md?" These are supposed to be notes. Not a verbatum/plagarized copy of all of Launch Code's work.

JavaScript Style Guide

1. camelCaseVariableNames

Camel Case is defined as starting with a lower case word, then using Uppercase for the first letter of any additional words in the variable name. ​

const AstronautCount = 7;		// ❌(Classes and interfaces start with uppercase )
const fuel_temp_celsius = -225;	// ❌ (Constants should be ALL caps if separated by underscores)
let is_ready = false;			// ❌
const astronautCount = 7;		// ✅ (actually this is wrong b/c it is a const. var should be used here.)
const fuleTempCelsius = -225;	// ✅ (actually this is also wrong b/c it is a const. var should be used here.)
let isReady = false;		    // ✅

😱 OH NO! Their "good" and "bad" examples are WRONG! You can use underscores in constant variables, but CONSTANTS_SHOULD_BE_ALL_CAPS. In fact the const keyword is being way too casually. You should use var for your variable declarations, let for variables you are using in iteration loops, and const for actual constants.

Also, AVOID using const in the Node.js console as constants cannot be reassigned if you make a mistake.

2. Descriptive Variable Names

Variable names should convey meaning. Don't be afraid to describe your variables!

const c = 7;					// ❌
const fuleTempC = -225; 		// ❌
let ir = false; 				// ❌
const astronautCount = 7;		// ✅
const fuelTempCelsius = -225;	// ✅
let isReady = false;			// ✅

3. Put Opening Braces and Statments on the Same Line

ℹ️ NOTE: There really is nothing wrong with puting your opening brace or statement on the next line, but it's probably a good idea to put them on the same line for style purposes.

Put opening braces on the same line as the statement.

if(fuelTempCelsius > -220)
    {							// ❌ (OTOH, here's a good reason to put them on the same line)
        console.log('WARNING');
    }							// ❌
else
    {							// ❌
        console.log('Temp fine');
    }							// ❌

// In the above example, the parser automatically indented on the next line.
// While that code does work, it's an eyesore to look at.

if(fuelTempCelsius > -220){		// ✅ (Much better)
    console.log('WARNING');
} else {						// ✅
    console.log('Temp fine');
}								// ✅

4. Always Use Braces for if Statements and Loops

🤓 ACTUALLY... if you only have ONE line following an if statement or loop, it's OK. So, the "bad" example is really not so bad. There should be a better "bad" example to show.

🎗️ TODO: Write a better "bad" example to show when using braces are necessary for grouping more than one line following a conditional statement or loop.

if (fuelTempCelsius > -220)
    console.log('WARNING');			// "❌" (Acutally, this is fine.)

for(let i = 0; i < 100; i++)		// "❌" (Acutally, this is fine.)
    console.log(i);

if (fuelTempCelsius > -220){		// ✅
    console.log('WARNING');
}

for (let i = 0; i < 100; i++){		// ✅
    console.log(i);
}

5. Use Semicolons

❓ "Why are Semicolons actually used?"

In languages like C, C++, and Java, every line ends with a semicolon. JavaScript follows the same convention, however it is not strictly enforced like it should be. Nevertheless, you SHOULD use semicolons even if some coders don't.

TBH: Semicolons are actually why I like JavaScript way more than Python.

Also, you can put two separate lines on the same line with a semicolon, which is how the for loop works.

🎗️ TODO: Write a better example fo why it is necessary to use semicolons. The examples that are considered "bad" will still work because they are self-contained.

let fuelTempCelsius = -200	// "❌" (Actually, It will still work because it's on its own line.)
if (fieldTempCelsius > -220){
    console.log('warning')	// "❌" (Acutally, It will still work for the same reason as the previous example. Also there's braces that should help contain it.)
}

let fuelTempCelsius = -200;		// ✅
if (fieldTempCelsius > -220){
    console.log('warning');		// ✅ 
}

6. Indent Code Block One Tab

OK, now THIS is an actual rule.

Indentation is a key tool for making code readable. Indent one Tab inside each code block. The defintion of what a Tab is differes between groups. The important thing is to be consistent and use the same Tab throughout your project.

ℹ️ NOTE: A Tab in most circles is considered to be defined as equivalent to four spaces. YMMV.

// 😱 The horror! The horror!
const drivingLogKm = [120, 34, 15, 71, 89, 94];
let totalKm = 0;
for (let i=0; i < drivingLogKm.length; i++) {
totalKm = totalKm + drivingLogKm[i];		// ❌
console.log("Adding", drivingLogKm[i]);		// ❌
console.log("Total Kilometers", totalKm);		// ❌
if (drivingLogKm[i] > 100) {		// ❌
console.log("warning: trip distance longer than advised") 		// ❌❌
}		// ❌
}
if (totalKm > 1000) {
console.log("Over limit for month");			// ❌
} else {
console.log("Still under limit for month");		// ❌
}

// 😌 This is much better
const drivingLogKm = [120, 34, 15, 71, 89, 94];
let totalKm = 0;
for (let i=0; i < drivingLogKm.length; i++) {
    totalKm = totalKm + drivingLogKm[i];		// ✅
    console.log("Adding", drivingLogKm[i]);		// ✅
    console.log("Total Kilometers", totalKm);		// ✅
    if (drivingLogKm[i] > 100) {		// ✅
        console.log("warning: trip distance longer than advised")	// Hey!, Waitaminute! No semi-colon! "❌"
    }		// ✅
}
if (totalKm > 1000) {
    console.log("Over limit for month"); 		// ✅
} else {
    console.log("Still under limit for month");		// ✅
}

#LaunchCode #JavaScript