Readable Code
beginner12 min readLesson 46 of 169
Names, small functions, no magic numbers, and comments that explain why.
Readable code is debuggable code. The rules are few and earned:
- Names carry the design.
retry_countbeatsn;is_valid_order(order)beatsif o[2] == 1 and len(o) > 5. - Small functions. One screen, one job. If you need "and" to describe what a function does, it is two functions.
- No magic numbers.
if score >= 90:— what is 90?GRADE_A_CUTOFF = 90documents itself. - Comments explain WHY, not what.
# skip header rowteaches;# add 1repeats.
def letter_grade(score):
if score >= GRADE_A_CUTOFF:
return "A"
...
These habits cost seconds while writing and save hours while debugging — the compound interest of craftsmanship.