SlideShare a Scribd company logo
1 of 16
NAMING CONVENTIONS
Meaningful names for a better code
INTENTION REVEALING NAMES
 The name of a variable, function, or class, should answer all
the big questions. It should tell you
 why it exists
 what it does
 how it is used.
 If a name requires a comment, then the name does not reveal
its intent.
int d; // elapsed time in days
Replacing with this
int elapsedTimeInDays;
AVOID DISINFORMATION
 Programmers must avoid leaving false clues that change the
meaning of code.
 We should avoid words vary from our intended meaning.
Do not refer to a grouping of accounts as an
accountList unless it’s actually a List.
The word list means something specific to
programmers
DIFFERENT NAMES
 Beware of using names which vary in small ways
 First Names should be different to spot the difference
How long does it take to spot the difference in
XYZControllerForEfficientHandlingOfStrings
And
XYZControllerForEfficientStorageOfStrings
Can you say the difference in these
getActiveAccount();
getActiveAccounts();
getActiveAccountInfo();
USE PRONOUNCEABLE NAMES
 Humans are good at words.And words are, by definition,
pronounceable
class DtaRcrd102 {
private Date genymdhms;
private Date modymdhms;
private final String pszqint = "102";
/* ... */
};
What about this
class Customer {
private Date generationTimestamp;
private Date modificationTimestamp;;
private final String recordId = "102";
/* ... */
};
USE SEARCHABLE NAMES
 Single-letter names and numeric constants have a problem in
that they are not easy to locate across a body of text.
One might easily search MAX_CLASSES_PER_STUDENT, but
the number 7 could be more troublesome.
Single-letter names can ONLY be used as local
variables inside short methods.
The length of a name should correspond to the size
of its scope
AVOID ENCODINGS
 We have enough encodings to deal with without adding more
to our burden. Encoding type or scope information into names
simply adds an extra burden of deciphering
Avoid Hungarian Notation
Avoid Member Prefix
m_ will no longer needed
CLASS NAMES
 Classes and objects should have noun or noun phrase names
like Customer, WikiPage, Account, and AddressParser.
 Avoid words like Manager, Processor, Data, or Info in the
name of a class.
 A class name should not be a verb
METHOD NAMES
 Methods should have verb or verb phrase names like
postPayment, deletePage, or save.
 Accessors, mutators, and predicates should be named for
their value and prefixed with get, set, and is accordingly
string name = employee.getName();
customer.setName("mike");
if (paycheck.isPosted())...
 When constructors are overloaded, use static
factory methods with names that describe the
arguments.
PICK ONE WORD PER CONCEPT
 Pick one word for one abstract concept and
stick with it.
 It’s confusing to have fetch, retrieve, and get
as equivalent methods of different classes
ADD MEANINGFUL CONTEXT
 There are a few names which are meaningful in and of
themselves—most are not.
 Instead, you need to place names in context for your
reader by enclosing them in well-named classes,
functions, or namespaces.
 When all else fails, then prefixing the name may be
necessary as a last resort.
ADD MEANINGFUL CONTEXT-2
private void
printGuessStatistics(char
candidate, int count) {
String number;
String verb;
String pluralModifier;
If (count == 0) {
number = "no";
verb = "are";
pluralModifier = "s";
} else if (count == 1) {
number = "1";
verb = "is";
pluralModifier = "";
}
else {
number = Integer.
toString(count);
verb = "are";
pluralModifier = "s";
}
String guessMessage =
String.format(
"There %s %s %s%s",
verb, number, candidate,
pluralModifier
);
print(guessMessage);
}
ADD MEANINGFUL CONTEXT-3
public class GuessStatisticsMessage {
private String number;
private String verb;
private String pluralModifier;
public String make(char candidate, int count) {
createPluralDependentMessage
Parts(count);
return String.format("There %s %s %s%s",
verb, number, candidate, pluralModifier );
}
private void createPluralDependentMessageParts(int
count) {
if (count == 0) {
thereAreNoLetters();
} else if (count == 1) {
thereIsOneLetter();
} else {
thereAreManyLetters(count);
}
}
private void thereAreManyLetters(int
count) {
number = Integer.toString(count);
verb = “are";
pluralModifier = "s";
}
private void thereIsOneLetter() {
number = "1";
verb = "is";
pluralModifier = "";
}
private void thereAreNoLetters() {
number = "no";
verb = "are";
pluralModifier = "s";
}
}
DON’T ADD GRATUITOUS CONTEXT
 In an imaginary application called “Gas
Station Deluxe,” it is a bad idea to prefix
every class with GSD.
 The names accountAddress and
customerAddress are fine names for
instances of the class Address but could be
poor names for classes. Address is a fine
name for a class.
FINAL WORDS
 People are also afraid of renaming things for fear
that some other developers will object.
 Developers do not share that fear and find that we
are actually grateful when names change (for the
better).
 Follow some of these rules and see whether you
don’t improve the readability of your code.
 If you are maintaining someone else’s code, use
refactoring tools to help resolve these problems.
 It will pay off in the short term and continue to pay
in the long run.
THANK YOU

More Related Content

What's hot

What's hot (20)

Python dictionary
Python dictionaryPython dictionary
Python dictionary
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Java Strings
Java StringsJava Strings
Java Strings
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Java features
Java featuresJava features
Java features
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Effective Java - Enum and Annotations
Effective Java - Enum and AnnotationsEffective Java - Enum and Annotations
Effective Java - Enum and Annotations
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
C#, .NET, Java - General Naming and Coding Conventions
C#, .NET, Java - General Naming and Coding ConventionsC#, .NET, Java - General Naming and Coding Conventions
C#, .NET, Java - General Naming and Coding Conventions
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
 

Viewers also liked

Viewers also liked (7)

Naming conventions
Naming conventionsNaming conventions
Naming conventions
 
Java & J2EE Coding Conventions
Java & J2EE Coding ConventionsJava & J2EE Coding Conventions
Java & J2EE Coding Conventions
 
Steel Naming Conventions
Steel Naming ConventionsSteel Naming Conventions
Steel Naming Conventions
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for java
 
Abap coding standards
Abap coding standardsAbap coding standards
Abap coding standards
 
Java packages
Java packagesJava packages
Java packages
 

Similar to Naming Conventions

Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handlingRai University
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Ch-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptxCh-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptxChereLemma2
 
Clean code lecture part I
Clean code lecture part IClean code lecture part I
Clean code lecture part IJun Shimizu
 
structure and union
structure and unionstructure and union
structure and unionstudent
 
MaciasWinter 2020ENG 107Project #2A Year in Gami.docx
MaciasWinter 2020ENG 107Project #2A Year in Gami.docxMaciasWinter 2020ENG 107Project #2A Year in Gami.docx
MaciasWinter 2020ENG 107Project #2A Year in Gami.docxcroysierkathey
 
C intro
C introC intro
C introKamran
 
Practices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerPractices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerSrikanth Shreenivas
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003R696
 
Kernel Adiutor
Kernel AdiutorKernel Adiutor
Kernel Adiutorwilli ye
 
PLF-Lesson tsu lecture time 2 units-2.pptx
PLF-Lesson tsu lecture time 2 units-2.pptxPLF-Lesson tsu lecture time 2 units-2.pptx
PLF-Lesson tsu lecture time 2 units-2.pptxMattFlordeliza1
 

Similar to Naming Conventions (20)

SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Ch-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptxCh-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptx
 
What's in a name
What's in a nameWhat's in a name
What's in a name
 
Clean code lecture part I
Clean code lecture part IClean code lecture part I
Clean code lecture part I
 
structure and union
structure and unionstructure and union
structure and union
 
MaciasWinter 2020ENG 107Project #2A Year in Gami.docx
MaciasWinter 2020ENG 107Project #2A Year in Gami.docxMaciasWinter 2020ENG 107Project #2A Year in Gami.docx
MaciasWinter 2020ENG 107Project #2A Year in Gami.docx
 
C intro
C introC intro
C intro
 
Clean code
Clean codeClean code
Clean code
 
Practices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerPractices For Becoming A Better Programmer
Practices For Becoming A Better Programmer
 
Structures
StructuresStructures
Structures
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
Kernel Adiutor
Kernel AdiutorKernel Adiutor
Kernel Adiutor
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
 
PLF-Lesson tsu lecture time 2 units-2.pptx
PLF-Lesson tsu lecture time 2 units-2.pptxPLF-Lesson tsu lecture time 2 units-2.pptx
PLF-Lesson tsu lecture time 2 units-2.pptx
 
Oop
OopOop
Oop
 
Lecture No 13.ppt
Lecture No 13.pptLecture No 13.ppt
Lecture No 13.ppt
 
Refactoring
RefactoringRefactoring
Refactoring
 
Structure and union
Structure and unionStructure and union
Structure and union
 

Naming Conventions

  • 2. INTENTION REVEALING NAMES  The name of a variable, function, or class, should answer all the big questions. It should tell you  why it exists  what it does  how it is used.  If a name requires a comment, then the name does not reveal its intent. int d; // elapsed time in days Replacing with this int elapsedTimeInDays;
  • 3. AVOID DISINFORMATION  Programmers must avoid leaving false clues that change the meaning of code.  We should avoid words vary from our intended meaning. Do not refer to a grouping of accounts as an accountList unless it’s actually a List. The word list means something specific to programmers
  • 4. DIFFERENT NAMES  Beware of using names which vary in small ways  First Names should be different to spot the difference How long does it take to spot the difference in XYZControllerForEfficientHandlingOfStrings And XYZControllerForEfficientStorageOfStrings Can you say the difference in these getActiveAccount(); getActiveAccounts(); getActiveAccountInfo();
  • 5. USE PRONOUNCEABLE NAMES  Humans are good at words.And words are, by definition, pronounceable class DtaRcrd102 { private Date genymdhms; private Date modymdhms; private final String pszqint = "102"; /* ... */ }; What about this class Customer { private Date generationTimestamp; private Date modificationTimestamp;; private final String recordId = "102"; /* ... */ };
  • 6. USE SEARCHABLE NAMES  Single-letter names and numeric constants have a problem in that they are not easy to locate across a body of text. One might easily search MAX_CLASSES_PER_STUDENT, but the number 7 could be more troublesome. Single-letter names can ONLY be used as local variables inside short methods. The length of a name should correspond to the size of its scope
  • 7. AVOID ENCODINGS  We have enough encodings to deal with without adding more to our burden. Encoding type or scope information into names simply adds an extra burden of deciphering Avoid Hungarian Notation Avoid Member Prefix m_ will no longer needed
  • 8. CLASS NAMES  Classes and objects should have noun or noun phrase names like Customer, WikiPage, Account, and AddressParser.  Avoid words like Manager, Processor, Data, or Info in the name of a class.  A class name should not be a verb
  • 9. METHOD NAMES  Methods should have verb or verb phrase names like postPayment, deletePage, or save.  Accessors, mutators, and predicates should be named for their value and prefixed with get, set, and is accordingly string name = employee.getName(); customer.setName("mike"); if (paycheck.isPosted())...  When constructors are overloaded, use static factory methods with names that describe the arguments.
  • 10. PICK ONE WORD PER CONCEPT  Pick one word for one abstract concept and stick with it.  It’s confusing to have fetch, retrieve, and get as equivalent methods of different classes
  • 11. ADD MEANINGFUL CONTEXT  There are a few names which are meaningful in and of themselves—most are not.  Instead, you need to place names in context for your reader by enclosing them in well-named classes, functions, or namespaces.  When all else fails, then prefixing the name may be necessary as a last resort.
  • 12. ADD MEANINGFUL CONTEXT-2 private void printGuessStatistics(char candidate, int count) { String number; String verb; String pluralModifier; If (count == 0) { number = "no"; verb = "are"; pluralModifier = "s"; } else if (count == 1) { number = "1"; verb = "is"; pluralModifier = ""; } else { number = Integer. toString(count); verb = "are"; pluralModifier = "s"; } String guessMessage = String.format( "There %s %s %s%s", verb, number, candidate, pluralModifier ); print(guessMessage); }
  • 13. ADD MEANINGFUL CONTEXT-3 public class GuessStatisticsMessage { private String number; private String verb; private String pluralModifier; public String make(char candidate, int count) { createPluralDependentMessage Parts(count); return String.format("There %s %s %s%s", verb, number, candidate, pluralModifier ); } private void createPluralDependentMessageParts(int count) { if (count == 0) { thereAreNoLetters(); } else if (count == 1) { thereIsOneLetter(); } else { thereAreManyLetters(count); } } private void thereAreManyLetters(int count) { number = Integer.toString(count); verb = “are"; pluralModifier = "s"; } private void thereIsOneLetter() { number = "1"; verb = "is"; pluralModifier = ""; } private void thereAreNoLetters() { number = "no"; verb = "are"; pluralModifier = "s"; } }
  • 14. DON’T ADD GRATUITOUS CONTEXT  In an imaginary application called “Gas Station Deluxe,” it is a bad idea to prefix every class with GSD.  The names accountAddress and customerAddress are fine names for instances of the class Address but could be poor names for classes. Address is a fine name for a class.
  • 15. FINAL WORDS  People are also afraid of renaming things for fear that some other developers will object.  Developers do not share that fear and find that we are actually grateful when names change (for the better).  Follow some of these rules and see whether you don’t improve the readability of your code.  If you are maintaining someone else’s code, use refactoring tools to help resolve these problems.  It will pay off in the short term and continue to pay in the long run.

Editor's Notes

  1. Complex fulcrumPoint = Complex.FromRealNumber(23.0);   is generally better than   Complex fulcrumPoint = new Complex(23.0); Consider enforcing their use by making the corresponding constructors private
  2. You type G and press the completion key and are rewarded with a mile-long list of every class in the system. Is that wise? Why make it hard for the IDE to help you?