SlideShare a Scribd company logo
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

Clean Code
Clean CodeClean Code
Clean Code
Dmytro Turskyi
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
Kamal Acharya
 
Clean code
Clean codeClean code
Clean code
Duc Nguyen Quang
 
Naming Standards, Clean Code
Naming Standards, Clean CodeNaming Standards, Clean Code
Naming Standards, Clean Code
CleanestCode
 
Coding standards
Coding standardsCoding standards
Coding standards
Mark Reynolds
 
Basic SQL and History
 Basic SQL and History Basic SQL and History
Basic SQL and History
SomeshwarMoholkar
 
Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 
Dart PPT.pptx
Dart PPT.pptxDart PPT.pptx
Dart PPT.pptx
DSCMESCOE
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
Abu Bakr Ramadan
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practices
mh_azad
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
Ayshwarya Baburam
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
tanmaymodi4
 
Python
PythonPython
Python
Shivam Gupta
 
Array
ArrayArray
Array
PRN USM
 
python Function
python Function python Function
python Function
Ronak Rathi
 
Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#Asim Rais Siddiqui
 
Operators
OperatorsOperators
Operators
Kamran
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 

What's hot (20)

Clean Code
Clean CodeClean Code
Clean Code
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
Clean code
Clean codeClean code
Clean code
 
Naming Standards, Clean Code
Naming Standards, Clean CodeNaming Standards, Clean Code
Naming Standards, Clean Code
 
Coding standards
Coding standardsCoding standards
Coding standards
 
Basic SQL and History
 Basic SQL and History Basic SQL and History
Basic SQL and History
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Structure in C
Structure in CStructure in C
Structure in C
 
Dart PPT.pptx
Dart PPT.pptxDart PPT.pptx
Dart PPT.pptx
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practices
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Python
PythonPython
Python
 
Array
ArrayArray
Array
 
python Function
python Function python Function
python Function
 
Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#Coding Standards & Best Practices for iOS/C#
Coding Standards & Best Practices for iOS/C#
 
Operators
OperatorsOperators
Operators
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 

Viewers also liked

Naming conventions
Naming conventionsNaming conventions
Naming conventionsJTHSICT
 
Java & J2EE Coding Conventions
Java & J2EE Coding ConventionsJava & J2EE Coding Conventions
Java & J2EE Coding Conventions
Vijaya Raghava Vuligundam
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for javamaheshm1206
 
Abap coding standards
Abap coding standardsAbap coding standards
Abap coding standardssurendra1579
 
Java packages
Java packagesJava packages
Java packages
Raja Sekhar
 

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

Clean code: meaningful Name
Clean code: meaningful NameClean code: meaningful Name
Clean code: meaningful Namenahid035
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
Shakila Mahjabin
 
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
Rai 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 handling
Rai 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++.pptx
ChereLemma2
 
What's in a name
What's in a nameWhat's in a name
What's in a name
Koby Fruchtnis
 
Clean code lecture part I
Clean code lecture part IClean code lecture part I
Clean code lecture part I
Jun 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.docx
croysierkathey
 
C intro
C introC intro
C intro
Kamran
 
Clean code
Clean codeClean code
Clean code
Arturo Herrero
 
Practices For Becoming A Better Programmer
Practices For Becoming A Better ProgrammerPractices For Becoming A Better Programmer
Practices For Becoming A Better Programmer
Srikanth Shreenivas
 
Structures
StructuresStructures
Structures
archikabhatia
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
R696
 
Kernel Adiutor
Kernel AdiutorKernel Adiutor
Kernel Adiutor
willi ye
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
VimalSangar1
 
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
MattFlordeliza1
 
Oop
OopOop
Lecture No 13.ppt
Lecture No 13.pptLecture No 13.ppt
Lecture No 13.ppt
AhmadNaeem59
 

Similar to Naming Conventions (20)

Clean code: meaningful Name
Clean code: meaningful NameClean code: meaningful Name
Clean code: meaningful Name
 
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
 

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?