Convention: Use ALL_CAPS for true constants that never change
Indentation & Line Length 📏
Consistency is KEY!
Pick ONE and stick with it:
2 spaces OR 4 spaces OR tabs
Same bracket style throughout
~80 characters per line maximum
// Breaking long lines
let reallyLongLine =
something +
somethingElse +
anotherThing +
howManyTacos +
oneMoreThing;
// Consistent indentation
if (condition) {
doSomething();
if (anotherCondition) {
doSomethingElse();
}
}
The Semicolon Debate ;
To use or not to use?
JavaScript can automatically insert semicolons...
BUT this can cause bugs!
// Recommended: Use semicolons
const name = "Alice";
const age = 25;
console.log(name);
// Can work without, but risky
const name = "Alice"
const age = 25
console.log(name)
Our recommendation: Use semicolons consistently to avoid surprises
Bad Comments 💬❌
What NOT to do
1. Don't use comments for version control
/**
* 2023-01-10: Fixed bug (RM)
* 2023-03-05: Simplified code (JP)
* 2023-05-15: Removed old function (LI)
*/
// That's what Git is for!
// Increment counter by 1
counter++;
// Check if user age is over 18
if (user.age > 18) {
// Set adult to true
adult = true;
}
✅ Helpful
// Track failed login attempts for security
counter++;
// Legal requirement in some regions
if (user.age > 18) {
adult = true;
}
When Comments Are Valuable 💎
Good Example:
function calculateBMI(height, weight) {
// BMI formula: weight(kg) / height(m)²
// We receive height in cm, so convert first
const heightInMeters = height / 100;
const bmi = weight / (heightInMeters * heightInMeters);
return bmi;
}
Comments are great for:
Explaining complex algorithms
Clarifying business logic
Warning about edge cases
Documenting WHY decisions were made
Self-Documenting Code 📖
Let the code speak for itself!
Needs Comments
// Extract text between brackets
function extract(s) {
// Find [ and get position after it
// Find ] and get text between
return s.substring(s.indexOf("[") + 1, s.indexOf("]"));
}
// Clear, descriptive names
function getComputerChoice() { }
function getHumanChoice() { }
function playRound(humanChoice, computerChoice) { }
function playGame() { }
Good Variable Names:
let humanScore = 0;
let computerScore = 0;
const choices = ["rock", "paper", "scissors"];
Remember: Write code as if the person maintaining it is a violent psychopath who knows where you live! 😅
Recommended Project Workflow 🔄
Step 1: Plan
Pseudocode your solution first
Step 2: Write
Code one function at a time
Step 3: Test
Use console.log() to verify each step
Step 4: Commit
Make meaningful commits often!
Important: NO GUI yet - that comes in a later lesson!
Common Pitfalls to Avoid ⚠️
In Your Code:
❌ Single-letter variable names (except in loops)
❌ Inconsistent indentation
❌ No spacing between code blocks
❌ Massive functions that do everything
❌ Copy-pasting without understanding
Quick Fixes:
✅ Use descriptive names
✅ Keep functions small and focused
✅ Test frequently
✅ Ask "Would I understand this in 2 weeks?"
Historical Insight 📜
The Zen of Python
19 Aphorisms for Writing Great Code
In 1999, Tim Peters wrote PEP 20 - The Zen of Python,
a collection of guiding principles for Python developers.
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Readability counts.
Fun fact: You can see all 19 principles in Python by typing import this
Universal Principles of Clean Code 🌍
Not Just for Python!
The Zen of Python Applies to ALL Languages:
Python
# Simple is better
average = sum(numbers) / len(numbers)
JavaScript
// Simple is better
const average = numbers.reduce((a,b) => a+b) / numbers.length;
Key Takeaways for Your Code:
📖 Readability counts - Others will read your code
✨ Simple is better - Don't overcomplicate solutions
🎯 Explicit is better - Make your intentions clear
Reflection Question: Which of the Zen principles resonates most with you?
How can you apply it to your Rock Paper Scissors project?
Clean Code Summary 📋
Remember These Principles:
📖 Code is read more than it's written
🏷️ Names should be self-explanatory
📏 Consistency > Personal preference
💬 Comments explain WHY, not WHAT
🔄 Refactor as you learn
"Any fool can write code that a computer can understand.
Good programmers write code that humans can understand." - Martin Fowler
Work Session Time! 💻
Rock Paper Scissors Project
Your Tasks:
Set up project structure
Create getComputerChoice()
Create getHumanChoice()
Build playRound() function
Implement playGame() for 5 rounds
Test thoroughly!
I'll be walking around to help! Raise your hand if you get stuck.