Writing Clean Code
By the end of this lesson, you'll be able to:
- Name variables and methods so their purpose is clear without a comment
- Recognize when a method is doing too much and should be split up
- Apply the "read it out loud" test to judge code clarity
Prerequisites: "Branching and Committing Discipline"
Names that explain themselves
// BAD
Integer d = 30;
if (x > d) { ... }
// GOOD
Integer daysOverdue = 30;
if (accountAge > daysOverdue) { ... }
// BAD— a label for the unclear version.Integer d = 30;—dcompiles fine, but a reader has to guess what it means.if (x > d) { ... }— same problem withx.- (blank line)
// GOOD— a label for the clear version.Integer daysOverdue = 30;—daysOverduereads almost like English.if (accountAge > daysOverdue) { ... }— the whole condition is now self-explanatory.
Clear names are the single cheapest way to make code easier to understand.
A method should do one thing
// BAD: one method doing three unrelated jobs
public void processOrder(Order__c order) {
// validate the order
if (order.Total__c <= 0) { return; }
// calculate tax
order.Tax__c = order.Total__c * 0.15;
// send a confirmation email
Messaging.sendEmail(...);
}
// BAD: one method doing three unrelated jobs— the label for what follows.public void processOrder(Order__c order) {— one method, about to do three unrelated jobs.// validate the order— job one: validation.if (order.Total__c <= 0) { return; }— the validation logic.// calculate tax— job two: calculation.order.Tax__c = order.Total__c * 0.15;— the calculation logic.// send a confirmation email— job three: notification.Messaging.sendEmail(...);— the notification logic.}— closes the method.
This mirrors the refactor from Module 12's "Review and Refactor" lesson: processOrder is doing validation, calculation, and notification all at once. Splitting it into isValidOrder(), calculateTax(), and sendConfirmation() — each with one clear job — makes each piece independently readable, testable, and reusable.
The "read it out loud" test
if (isEligibleForDiscount(customer) && cartTotal >= minimumForFreeShipping) {
applyFreeShipping(order);
}
if (isEligibleForDiscount(customer) && cartTotal >= minimumForFreeShipping) {— read out loud: "if the customer is eligible for a discount and the cart total is at least the minimum for free shipping."applyFreeShipping(order);— "...apply free shipping."}— closes theif.
It reads like a sentence describing the actual business rule — a strong sign the names and structure are doing their job. If a piece of code makes you stumble reading it aloud, that's usually a sign it needs clearer names or restructuring.
Exercise
Rename the variables in this snippet to clear, self-explanatory names.
Show hint
What do p and t actually represent?
Writing Clean Code Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Clean code is code that's easy for the next person (often you, months later) to read and change safely — a handful of concrete habits make an enormous difference.