๐ WAYPOINT LESSON
Helpers as named decisions
โญโญ intermediateโณ 12 min read๐ Lesson 93 of 256
Decompose a spec into one-decision helpers that return, never print.
A helper method is a named decision. Exam problems rarely say "write a helper" โ they reward one anyway, because a well-named helper makes the main method read like the specification itself.
"Return true if the password is valid: at least 8 characters, and either contains a digit or starts with an uppercase letter."
public static boolean hasDigit(String s) {
for (int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i))) {
return true;
}
}
return false;
}
public static boolean valid(String pw) {
return pw.length() >= 8 && (hasDigit(pw) || startsUpper(pw));
}
The main method is now two lines and mirrors the English word-for-word. Each helper is independently testable โ and on the exam, independently traceable when you must debug.
Helper design rules:
- One decision per helper. If you cannot name it in one phrase, it is two helpers.
- Helpers return, they do not print. Printing inside a helper makes it useless as a building block.
- The order you write them is the order you test them. Write
hasDigit, test it on"","abc","a1". Only then writevalid.
The AP Java Quick Reference includes Character.isDigit/isLetter โ
helpers around single-character classification are expected knowledge.