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
accountsoraccountGroup
- What if it's not a
- Avoid noise word (things like
Info,Data,Manager,Processor)- Imagine having
CustomerInfo,CustomerDatawould be confusing - Or even
CustomervsCustomerObject - or
getActiveAccount(),getActiveAccounts(),getActiveAccountInfo()- Don't name variable with tiny bit of difference as well
- Imagine having
- Put magic number in constants for searchability
- For example, it's easier to search for
MAX_STUDENTScomparing to7
- For example, it's easier to search for
- Don't put type in variable name:
- For example, don't name
PhoneString. What if it's not aStringanymore later on
- For example, don't name
- 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
ShapeFactoryImplfor the implementation class is better thanIShapeFactory
- If you must specify one, even
- Why: Unnecessary for the user to know that they're receiving an interface.
- Don't introduce another
a, orbinstead ofi,j,kfor 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 ofnew Number(23.0)- and make the overload constructor to
private