25 min·advanced

Lesson 1

Clean code & modules

Build a practical mental model for clean code & modules and use it in working JavaScript.

By the end, you will be able to…

  • Explain clean code & modules in your own words
  • Use clean code & modules in a small program
  • Recognize and debug a common failure mode

Build the mental model

Clean code favors small functions and modules with one clear responsibility, descriptive names over comments explaining unclear ones, and consistent structure across a codebase.

Syntax and working example

Read the code from inputs to output. Predict what it prints, then run it and change one value.

Working example
Expected output: Smaller, independently readable and testable files

Why this matters

Code is read far more often than it's written; clarity now saves hours of confusion for you or a teammate later.

Real-world example

Code review comments like 'can you extract this into its own function?' are this principle being applied in a real team.

Common mistakes

  • Writing one giant function that does five unrelated things
  • Naming things generically (data, temp, handleStuff) instead of descriptively
  • Duplicating logic in multiple places instead of extracting one shared function

Try it yourself

Challenge

Split calculateOrder into two smaller, named functions: one for the subtotal, one for tax.

Practice

Knowledge check

What is a strong signal a function should be split into smaller ones?

Summary

Keep functions and modules small and single-purpose, with descriptive names that reduce the need for comments.