Chapter 2: Meaningful names [Slide]
Using d
instead of elapsedTimeInDays
.
- Using names like
hp
,aix
where it has different meaning in different contexts. - Using
AccountList
for group of accounts where there is no list
- Using
a1
anda2
instead ofsource
anddestination
. - what's the difference between
TheCustomer
,CustomerData
,CustomerInfo
andCustomer
Don't compact names for sake of smaller words, eg. pref
, mnger
, genymdhms
.
Using constants and complete names make it easy to find them with search, eg. WORK_DAYS_PER_WEEK
In the modern IDE's there is no need to prefix with HN, eg. frmReserve
, tblStudent
.
Don't use stupid m prefix for field names, eg. mLatitude
, mContext
Prefer to suffix Implementation class, rather than Interface class
eg. ShapeFactory
for the interface and ShapeFactoryImpl
for the implementation.
Readers shouldn’t have to mentally translate your names into other names they already know.
noun or noun phrases
- methods should have verb or phrase names
eg.
deletePage()
,save()
,isEven()
,setPoint()
,hasElement()
- prefer factory methods for multiple constructors
Using whack()
instead of kill()
, eatMyShorts()
instead of abort()
, nukeTable()
instead of deleteAll()
eg. fetch
, retrieve
and get
Avoid using the same word for two purposes. Using the same term for two different ideas
is essentially a pun.
eg. using add
instead of insert
for a method which is putting single parameter to a collection is a pun
use computer science (CS) terms, algorithm names, pattern names, math terms
When there is no “programmer-eese” for what you’re doing, use the name from the problem domain.
Separate your concerns using well-named :
- Constant
- Function
- Class
- Namespace
- Package
- Module
When all else fails, then prefixing the name may be necessary as a last resort.
eg. GSDAccountAddress