Intro to Design & Refactoring / Chapter 1 — Start with Readability (lessons 1–5) / Lesson 1

Naming (Expressing Intent Through Names)

Time needed: about 6 min (estimate)

☆ Read later

You can try the opening of this lesson without an account.

What refactoring is

Refactoring means reshaping the inside of your code without changing its behavior (what it looks like from outside).

It's not about adding features or fixing bugs.

That's why refactoring depends on tests, and every lesson's sample in this course confirms that before and after give the same answer for the same input.

The code is written in two languages, Python and Java.

Explanations start with Python, and Java only shows what's different.

The verified environment is Python 3.14.2 + pytest 9.1.1 and JDK 21, and the Java tests run on nothing but a small TestKit with no external libraries.

Code whose names don't reveal intent

The following code works.

But reading it doesn't tell you what calc computes, what d is, or what x[0], x[1], and x[2] are.

You end up holding it all in your head as you read — "x[2] is the quantity, x[0] is the unit price..."

before.py

def calc(d): r = [] for x in d: if x[2] > 0 and x[1] != "": r.append(x[0] * x[2]) return sum(r)

Let names and data shapes do the talking

Give the function, its arguments, and its data names that say what they are and what they do.

The tuple (unit_price, product_name, quantity) gets bundled into a type called OrderLine (a dataclass).

after.py

from dataclasses import dataclass @dataclass class OrderLine: unit_price: int product_name: str quantity: int def total_amount(order_lines: list[OrderLine]) -> int: """Sum named products by their quantity.""" return sum( line.unit_price * line.quantity for line in order_lines if line.quantity > 0 and line.product_name != "" )

calc became total_amount (computes the total amount), and d became order_lines (the list of order line items).

x[0] and x[2] became line.unit_price and line.quantity, so you no longer need to remember which index is the unit price, and turning it into a type also rules out mixing up the order.

It's the same in Java, where the data gets bundled into a record (a small type for grouping values, since Java 16).

After.java (excerpt)

public record OrderLine(int unitPrice, String productName, int quantity) {} public static int totalAmount(List<OrderLine> orderLines) { int total = 0; for (OrderLine line : orderLines) { if (line.quantity() > 0 && !line.productName().isEmpty()) { total += line.unitPrice() * line.quantity(); } } return total; }

The total stays at 1300

Even after renaming things and building a type, the calculation's result has to stay the same.

The sample's test uses 4 lines, mixing in a line with an empty product name and one with quantity 0, and confirms that both before and after return 1300.

Try running both in the editor below.

On your own machine, scripts/test-lessons.sh 01 runs both the Python and Java tests.

editor.py

Tab indents. To leave the editor, press Esc, then Tab.

Create a free account to keep reading

With an account you can read the rest of this lesson, take the quiz, and read every free lesson in this course. Lessons you answer correctly are recorded as complete.

Report a problem with this lesson

Log in to send a report.

Keyboard shortcuts

Keys available in a lesson
KeyAction
[ or Alt + ←Go to the previous lesson
] or Alt + →Go to the next lesson
?Open this list
EscClose this list

The keys do not work while the cursor is in a code editor or an input field.