Tutelage

Lessons › coding and computer science · taught by Leo

Writing clean code basics

Learn how clear names, small functions, and simple structure make code easier to read, debug, and maintain.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

Clean code helps future-you

Clean code is code that another person can read quickly. That other person might be future you.

The goal is not fancy code. The goal is code that is easy to understand and change.

When code is clear, bugs are easier to spot too.

Big idea: make intent obvious

A clean program tells you what it does without extra guessing.

Good names, small pieces, and simple flow help the reader follow along.

Think: if I return in one week, will I still understand this fast?

Predict: which name is clearest?

Which variable name is the clearest for a student’s score in a game?

  1. s
  2. score
  3. data
Show answer

score: Yes. score says exactly what the value means.

Worked example: rename messy code

Here is a small example. We start with a vague line of code.

Then we rename the parts so a reader knows what is being added.

Next, we give the function a job name that matches its action.

Now the whole idea is easier to scan: one job, one purpose.

Quick check: what helps most?

What should you do next to make code cleaner?

  1. Use shorter names so it looks neat
  2. Split a big function into smaller ones
  3. Hide the logic in one giant loop
Show answer

Split a big function into smaller ones: Exactly. Smaller functions are easier to read, test, and fix.

Why small pieces work

When a function does one job, it is easier to test that one job. If something breaks, you can look in a smaller place. That saves time and lowers confusion.

This works because your brain can hold only so much at once.

Small code pieces reduce the amount you need to think about at the same time.

That is why clean code often looks simpler, even if it does the same work.

Common mistake: clever over clear

A common mistake is trying to be clever.

If the logic is hard to explain, it is usually too hard to read.

Plain code is usually better than impressive code.

Your turn: pick the better fix

A function does three different things. What is the best clean-code fix?

  1. Keep it all together because fewer files is always better
  2. Split it into smaller functions with clear names
  3. Add more comments and leave the structure messy
Show answer

Split it into smaller functions with clear names: Yes. Split it up. Clear structure beats one large block.

Recap: your clean-code checklist

First, use names that say what things are.

Second, keep each function focused on one job.

Third, write for the reader, not just for the computer.

If you can read it fast, you can fix it fast.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app