Clean code

Variable Names

Meaningful names

“A name should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does not reveal its intent.”

– Clean Code

Boolean variables can be named as

expired?, has_expiry or is_expired.

Date variables can be named as

expires_on, expired_at

Avoid noise words

  • the, a, an

  • info, data, variable, object

  • manager

Pronounceable

current_date

Magic numbers

use constants instead of magic numbers

DAYS_IN_WEEK = 7

Consistent

Do not use synonyms

fetch, get, retrieve

constants should be deeply immutable

static final ImmutableList<String> NAMES = ImmutableList.of('Naruto', 'Sasuke')

Functions

do only one thing

  • not more than 20 lines

Functions should do one thing. They should do it well. They should do it only.

– Clean Code

extract complex conditionals in a function

Fewer arguments

pass object or dict

do not use multiple arguments of same type

distance(x1, y1, x2, y2)

Avoid flag arguements

contradicts single responsibility

No side effects

DRY


📌 Tagged As