Part 2 of 7 · 1 min read
Names
A name is the first comment.
A good label on a drawer saves opening it. Names do the same for code. Rules of thumb (common practice):
- Classes are nouns from the requirements:
Booking,Room,PricingStrategy. - Methods are verbs:
book,cancel,findFreeRooms. - Booleans read as questions:
isActive,hasCapacity. - No abbreviations a stranger cannot read.
slotis fine,sltis not. - One word per idea. Do not use
cancelin one class andremovein another for the same action. - Same names as the diagram. The board and the code must agree.
Java style: classes in PascalCase, methods and variables in camelCase, constants in UPPER_SNAKE_CASE.
Intention-revealing name
An intention-revealing name tells the reader what a thing is for, without a comment.
Example: isActive() tells more than check(), and TimeSlot tells more than Pair.
Check yourself:
1. Which is better for a boolean, active or isActive?
2. Why must code names match the diagram?
3. What is wrong with using cancel and remove for the same thing?