Names just repeat the types - if types are correct and methods are small
The same object should have the same name when passed to other methods - not always but usually
Prefer positive conditional names - e.g. avoid !isNot()_
Some conventions
Class name: noun
Method name: verb
Avoid prefixes and technical terms (e.g. Abstract)
Name length corresponds scope length
Old Enterprise JavaBeans - EJB
Comply with: Java Code Conventions (Sun, 1997)
EJB - Enterprise JavaBeans
private Foo foo - foo is a 'property' name
public Foo getFoo() - the property name with a capital
public void setFoo(Foo foo)
public boolean isSomething() - also hasSomething()
Example: Bad: Names
classDtaRcrd102 {// Not meaningful, not readable, not pronounceableDate genymdhms;Date modymdhms;String pszqint ="102";// Avoid prefixes and Hungarian notationString m_dsc;String strName;// Hard to readXYZControllerForEfficientStorageOfStrings con1;XYZControllerForEfficientHandlingOfStrings con2;// DisinformingvoidsetName(String name) { m_dsc = name; }// Not informativevoiddoCalculation() {// Too short names, Magic numbersfor (int j=0; j<34; j++) { s += (t[j]*4)/5; } }}
// What does it really do?assertThat(scores, is(StudentService.collectScores(STUDENT_LIST2)));
Example: Good: Informative names
// Readable: "It creates an empty score list from an empty student list"assertThat(emptyScoreList, is(StudentService.collectScores(STUDENT_LIST_EMPTY)));
Example: Bad: Not informative name of input parameter
// What kind of input value is converted?// It is not a 'find' but rather a 'convert'privateStringfindDeadlineType(int i) {String result;switch (i) {case0: result = RK_FIRST_NAME_DEADLINE;break;case1: result = RK_SECOND_NAME_DEADLINE;break;default: result =""; //future values come herebreak; }return result;}// Best practice: Refactor to enum
Example: Bad: Confusing characters (From the book)
int a = l;if ( O == l ) a = O1;else l =01;
Example: Good: Name lengths correspond to scope
// The class is one screen long@ComponentpublicclassNameDeadlineReminderMessageCollector { @AutowiredprivateDeadlineReminderMessagePnrFilter filter; // Used only in this class @AutowiredprivateDeadlineReminderMessageSorter sorter; // Used only in this class @Transactional(readOnly =true)publicList<DeadlineReminderMessage> collectDeadlineRemindersByAgency(String agencyInternalId) {returnsorter.sort(filter.filter(findDeadlines(agencyInternalId),findPnrs(agencyInternalId))); } @Transactional(readOnly =true)publicList<DeadlineReminderMessage> collectDeadlineRemindersByPoses(List<String> poses) {returnsorter.sort(filter.filter(findDeadlines(poses),findPnrs(poses))); }}