Summary
Chapter 2 - Meaningful Names
- Don't name your variables like
accountList
: - What if it's not a
List
? - Even if it's a
List
, what if you change the datastructure after? - Better naming would be
accounts
or accountGroup
- Avoid noise word (things like
Info
, Data
, Manager
, Processor
) - Imagine having
CustomerInfo
, CustomerData
would be confusing - Or even
Customer
vs CustomerObject
- or
getActiveAccount()
, getActiveAccounts()
, getActiveAccountInfo()
- Don't name variable with tiny bit of difference as well
- Put magic number in constants for searchability
- For example, it's easier to search for
MAX_STUDENTS
comparing to 7
- Don't put type in variable name:
- For example, don't name
PhoneString
. What if it's not a String
anymore later on
- Don't put prefix in variable name for example
m_dsc
- Don't name your interface starting with
I
- Why: Unnecessary for the user to know that they're receiving an interface.
- If you must specify one, even
ShapeFactoryImpl
for the implementation class is better than IShapeFactory
- Don't introduce another
a
, or b
instead of i,j,k
for loop - Method name should starts with verb. For example
postPayment
, receivePayment
, save
- Also follow JavaBean standard by using
get
, set
, is
- For constructor overloading, it's better to have a static factory method instead. For example
Number myNumber = Number.fromFoat(23.0)
instead of new Number(23.0)
- and make the overload constructor to
private