Orientation and Web Foundations
Establish how the web works, core protocols, and the tools and habits for succeeding in this course.
Content
Coding style and linting
Versions:
Watch & Learn
AI-discovered learning video
Coding Style and Linting: The Fashion Police of Your Codebase
If code is a conversation with future you, linting is the part where future you is begging present you to use deodorant and punctuation.
You have your editor set up, you’ve tangoed with the terminal, and you’ve probably made a file called app.py or index.js that works. Cute. Now let’s make it readable, consistent, and slightly less chaotic. Enter: coding style and linting — the gentle-but-firm guardians that keep your project from becoming a spaghetti opera.
What Are We Talking About (Without the Buzzwords)?
- Style guide: A set of rules for how your code should look. Think: naming, spacing, line length, quotes, semicolons. Python’s big one is PEP 8. JavaScript has the ESLint recommended rules or popular community guides.
- Linter: A tool that checks your code for style issues and suspicious patterns. It’s like a spell-checker, but for code logic and best practices.
- Formatter: A robot that automatically fixes the look of your code — spacing, quotes, trailing commas — so your team stops arguing about tabs vs spaces and starts arguing about real things, like pizza toppings.
Why do people keep misunderstanding this? Because style feels subjective. But in teams (yes, even team-of-one you), consistency beats preference. Your editor and your command line are now not just where you write code, but where you enforce your team vibe.
The Big Picture: Tools You Will Actually Use
| Language | Style guide | Linter | Formatter | Typical config |
|---|---|---|---|---|
| Python | PEP 8 | flake8 or pylint | black (plus isort) | pyproject.toml, .flake8 |
| JavaScript/TypeScript | ESLint recommended or Airbnb | ESLint | Prettier | .eslintrc.json, package.json |
| CSS | community conventions | stylelint | Prettier (some formatting), stylelint fixes | .stylelintrc |
Pro tip from command line basics: these all run as CLI commands, which means you can script them, automate them, and look powerful while doing it.
Python: PEP 8 Meets Black (and Friends)
A messy Python snippet
# example.py (chaos edition)
def add(a,b): return a+ b
X = [1,2,3,4,]
print ( add( 1 ,2))
Run a linter:
$ flake8 example.py
example.py:1:1: E701 multiple statements on one line (colon)
example.py:1:12: E231 missing whitespace after ','
example.py:3:1: F841 local variable 'X' is assigned to but never used
example.py:5:8: E211 whitespace before '('
Run a formatter:
$ black example.py
reformatted example.py
Formatted result:
# example.py (after black)
def add(a, b):
return a + b
X = [1, 2, 3, 4]
print(add(1, 2))
Key differences:
- The linter told you what’s wrong.
- The formatter fixed what it could.
- You still decide whether
Xshould exist (flake8 flagged it as unused).
Install and configure
# create a virtual environment if you haven't
python3 -m venv venv && source venv/bin/activate
pip install black flake8 isort
pyproject.toml for shared settings:
[tool.black]
line-length = 88
target-version = ["py311"]
[tool.isort]
profile = "black"
.flake8 to match black’s quirks and common ignores:
[flake8]
max-line-length = 88
extend-ignore = E203, W503
Hot take: black is opinionated on purpose. Arguing with it is like arguing with gravity. Save the debate energy for architecture decisions.
JavaScript: ESLint and Prettier, the Dynamic Duo
A messy JS snippet
// app.js (chaos edition)
function greet(name){ if(name == null){ var msg = "Hi"; } else { let msg = "Hello, " + name ; } console.log(msg) }
Potential issues:
==instead of===varinstead oflet/const- Block-scoped variable redeclaration and scope confusion
- Missing semicolons (depending on your rules)
Initialize tools
npm init -y
npm install --save-dev eslint prettier eslint-config-prettier eslint-plugin-prettier
npx eslint --init
A minimal .eslintrc.json that plays nice with Prettier:
{
"env": { "browser": true, "es2021": true },
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
"rules": { "eqeqeq": "error", "no-var": "error" }
}
Add scripts to package.json:
{
"scripts": {
"lint": "eslint .",
"format": "prettier -w ."
}
}
After ESLint and Prettier, your code becomes:
function greet(name) {
let msg;
if (name === null) {
msg = "Hi";
} else {
msg = `Hello, ${name}`;
}
console.log(msg);
}
Prettier is your auto-layout for code. ESLint is the critic that says, hey, use
===and stop summoningvarfrom 2006.
CSS and HTML: Yes, They Need Style Too
- stylelint catches things like invalid properties, unknown units, and suspicious patterns.
- Prettier can format CSS and HTML so indentation and braces behave themselves.
npm install --save-dev stylelint stylelint-config-standard
.stylelintrc:
{
"extends": ["stylelint-config-standard"]
}
Run:
npx stylelint "**/*.css" --fix
Make Your Editor Do The Work (VS Code)
Remember your editor setup? Time to turn it into a style-enforcement cyborg.
- Install extensions: Python, ESLint, Prettier, stylelint.
- Enable format on save and default formatters in
settings.json:
{
"editor.formatOnSave": true,
"[python]": { "editor.defaultFormatter": "ms-python.python" },
"[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
Bonus: add an .editorconfig so every editor in the team respects basics like indentation, newlines, and UTF-8.
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
Command Line Power Moves (From Your CLI Superpowers)
- Run all linters before you commit:
# Python
flake8 . && black --check . && isort --check-only .
# JS/CSS/HTML
npm run lint && npm run format -- "**/*.{js,jsx,ts,tsx,css,html}"
- Add a Git pre-commit hook with the
pre-commitframework for Python:
pip install pre-commit
cat > .pre-commit-config.yaml <<'YAML'
repos:
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
YAML
pre-commit install
Automation is kindness to your future self. Make the computer say no so you don’t have to.
The Cheat Sheet: Common Rules You’ll See Everywhere
Python
- Max line length: 88 (black) or 79 (classic PEP 8)
- Snake case for functions and variables; PascalCase for classes
- One import per line; sort imports (use
isort) - Avoid wildcard imports like
from x import *
JavaScript
- Use
===always; avoid== - Prefer
constfor values that don’t change;letotherwise; avoidvar - No unused variables; no console logs in production (lint will catch)
- Use trailing commas in multiline objects/arrays for cleaner diffs (Prettier default)
- Use
CSS
- Lowercase property names, consistent hex case, no duplicate properties in a rule
- Prefer longhand vs shorthand when readability suffers
- Keep specificity low; use class-based styles
When (and How) to Ignore a Rule Without Summoning Chaos
Sometimes linters get in the way of a deliberate choice. Fine. Do it intentionally and document it.
- Python: add
# noqa: F401to intentionally unused imports (e.g., for re-exports) - JS: add
// eslint-disable-next-line rule-nameon the line you must exempt - Config-level ignores: use them sparingly and comment why
Linters are mentors, not monarchs. But if you ignore them constantly, maybe you needed their rule in the first place.
Real-World Flow You Can Copy-Paste Into Your Life
- In a new Python project: add
black,flake8,isort, andpyproject.toml+.flake8. - In a JS project: add ESLint + Prettier, and wire scripts
lintandformatinpackage.json. - Turn on format-on-save in VS Code. Add
.editorconfig. - Add pre-commit hooks or a simple shell script to run linters before pushing.
- In CI (GitHub Actions), run the same commands so your main branch stays shiny.
Imagine this in everyday life: you write code like a gremlin at 2 a.m. Your formatter tucks it into bed. Your linter leaves a sticky note: you forgot ===. Next day, you read your code and say, oh, past me was actually organized. Growth.
Wrap-Up: The Vibe Is Consistency
- Style guides reduce cognitive load. Same patterns, less thinking about commas, more thinking about logic.
- Linters catch bugs early. Formatters end pointless debates.
- Your editor and command line are now teammates. Automate them.
Key takeaways:
- Pick tools and standardize: black + flake8 for Python; ESLint + Prettier for JS; stylelint for CSS.
- Configure once, run everywhere: editor, CLI, pre-commit, CI.
- Ignore rules intentionally and rarely.
Powerful insight to tape to your monitor: Make it work. Make it clear. Then make it pretty — automatically.
Now go lint something. Your future self is already writing a thank-you note.
Comments (0)
Please sign in to leave a comment.
No comments yet. Be the first to comment!