SlideShare a Scribd company logo
Clean Code : Meaningful Name
Presented by
Md. Nahidul Hasan
Meaningful Name
■ Names are everywhere in software.
■ We name our variables, our functions, our arguments, classes, and packages.
■ We name our source files and the directories that contain them.We name our jar files and war files and ear files.
■ We name and name and name. Because we do so much of it, we’d better do it well
“The hardest thing about choosing good names is that it requires good descriptive skills and a shared cultural background.
This is a teaching issue rather than a technical, business, or management issue. As a result, many people in this field do not do it
very well.”
Use Intention-Revealing Names
■ Choosing good names takes time but saves more than it takes.
■ Take care with your names and change them when you find better ones.
■ The name of a variable, function or class should answer the big questions such as why it exists, what it does, how it
is used.
■ If a name requires a comment, then the name does not reveal its intent. For example, these names specify what is
being measured and the unit of that measurement:
int d // elapsed time in days
int elapsedTimeInDays;
int daysSinceCreation;
int daysSinceModification;
Make Meaningful Distinctions
■ Distinguish names in such a way that the reader knows what the differences offer. For example, the two classes
ProductInfo and ProductData have different names but the names don’t mean anything different, because here
Info and Data are indistinct noise words.
■ More Example :
getActiveAccount();
getActiveAccounts();
getActiveAccount();
getAllActiveAccounts();
Use Pronounceable Names
■ Our minds have evolved to deal with spoken language, so take advantage of that when creating names.
■ Also, if you can’t pronounce it, you can’t discuss it without sounding like an idiot.
■ As an example, note the benefits of using the second version of this variable instead of the first version
class DtaRcrd102 {
private Date genymdhms;
private Date modymdhms;
private final String pszqint = "102";
/* ... */
};
class Customer {
private Date generationTimestamp;
private Date modificationTimestamp;
private final String recordId = "102";
/* ... */
};
Use Searchable Names
■ Names should be easy to locate across a body of text.
■ The length of a name should correspond to the size of its scope.
■ If a variable or constant might be seen or used in multiple places in a body of code, it is imperative to give it a
search-friendly name.
for (int j=0; j<34; j++) { s += (t[j]*4)/5;
}
int realDaysPerIdealDay = 4;
const int WORK_DAYS_PER_WEEK = 5;
int sum = 0;
for (int j=0; j < NUMBER_OF_TASKS; j++) {
int realTaskDays = taskEstimate[j] *
realDaysPerIdealDay;
int realTaskWeeks = (realdays /
WORK_DAYS_PER_WEEK);
sum += realTaskWeeks;
}
Avoid Encodings
■ Encoding type or scope information into names simply adds to the burden of deciphering. They are a mental
burden, seldom pronounceable and easy to mistype.
■ Hungarian Notation (HN) adds nothing to Java, as the objects are strongly typed and IDEs detect type errors
long before compilation. A variable that has its type changed, but accidentally not had its HN name changed can
be misleading.
Avoid Mental Mapping
■ Readers should not need to mentally translate your names into other names they already know/are familiar
with. This problem generally arises from a choice to use NEITHER problem-domain terms nor solution-domain
terms (see more, below, on problem-domain and solution-domain).
■ Single-letter variable names are really only okay for use as loop counters if their scope is very small and no other
names can conflict with them; plus they are traditional.
■ In general programmers are pretty smart people. Smart people sometimes like to show off their smarts by
demonstrating their mental juggling abilities. However:
“One difference between a smart programmer and a professional programmer is that the professional understands
that clarity is king. Professionals use their powers for good and write code that others can understand.”
Class Names
■ Classes and objects should have noun or noun-phrase names, like Customer,WikiPage, Account and
AddressParser.
■ But avoid names 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 deletePage or save.
■ Accessors and mutators, and their predicates, should be named according to JavaBeanstandards
Don’t Pun
■ Don’t deliberately or inadvertently re-use the same term where its semantic meaning in the code is different; for
example using the term add in several classes, for “consistency”, when you are in fact not using it in the same
sense.
Use Solution Domain Names
■ The people who read you code are programmers, so go ahead and use solution domain terms, such as
AccountVisitor, where Visitor means the VISITOR pattern, or JobQueue.
Use Problem Domain Names
■ When there is no programmer-eese for what you are doing, use the name from the problem domain. However:
The code that has more to do problem domain concepts should have names drawn from the problem domain.
Add Meaningful Context
■ Most names are not meaningful in and of themselves. So:
You need to place names in a context for your reader by enclosing them in well-named classes, functions or namespaces.
Naming within an Unclear Context:
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
Naming within a Clear Context:
public class GuessStatisticsMessage {
private String number;
private String verb;
private String pluralModifier;
public String make(char candidate, int count)
{
createPluralDependentMessageParts(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";
}
}
Pick One Word per Concept
■ Pick and use one word for abstract concept and stick with it.
■ A consistent lexicon is a great boon to the programmers who use your code.
■ Example: What’s the difference between fetch, retrieve and get?
Don’t Be Cute
■ Don’t be too clever or humourous with your names. Don’t use a function name likeholyHandGrenade, when you
mean deleItems.
■ For example, don’t use the name whack() to mean kill()
Don’t Add Gratuitous Context
■ Shorter names are generally better than longer ones, so long as they are clear.
■ Add no more context to a name than is necessary.
■ Examples: 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. If you need to differentiate between MAC
Addresses, port addresses, and Web addresses, you might consider PostalAddress, MAC, and URI.
■ In an imaginary application called “Gas Station Deluxe,” it is a bad idea to prefix every class with GSD.
A mailingadress classs for in GSD’s accounting Module - GSDAccountAdress
A mailing address for cutomer contact - GSDAccountAdress
END
Thanks to All

More Related Content

What's hot

Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
Theo Jungeblut
 
Clean code
Clean codeClean code
Clean code
Arturo Herrero
 
Clean code
Clean codeClean code
Clean code
Henrique Smoco
 
Coding conventions
Coding conventionsCoding conventions
Coding conventions
systemcrashed
 
Clean Code
Clean CodeClean Code
Clean Code
Dmytro Turskyi
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code Principles
YeurDreamin'
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
Mario Sangiorgio
 
Clean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit TestsClean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit Tests
radin reth
 
Clean code
Clean codeClean code
Clean code
Clean codeClean code
Clean code
Knoldus Inc.
 
Clean code slide
Clean code slideClean code slide
Clean code slide
Anh Huan Miu
 
C# conventions & good practices
C# conventions & good practicesC# conventions & good practices
C# conventions & good practices
Tan Tran
 
Clean Code summary
Clean Code summaryClean Code summary
Clean Code summary
Jan de Vries
 
Coding standard
Coding standardCoding standard
Coding standard
FAROOK Samath
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practices
mh_azad
 
C# in depth
C# in depthC# in depth
C# in depth
Arnon Axelrod
 
Clean code
Clean codeClean code
Clean code
ifnu bima
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
Kent Huang
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
Md. Mahedee Hasan
 
Clean Code
Clean CodeClean Code
Clean Code
swaraj Patil
 

What's hot (20)

Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
 
Clean code
Clean codeClean code
Clean code
 
Clean code
Clean codeClean code
Clean code
 
Coding conventions
Coding conventionsCoding conventions
Coding conventions
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code Principles
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
 
Clean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit TestsClean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit Tests
 
Clean code
Clean codeClean code
Clean code
 
Clean code
Clean codeClean code
Clean code
 
Clean code slide
Clean code slideClean code slide
Clean code slide
 
C# conventions & good practices
C# conventions & good practicesC# conventions & good practices
C# conventions & good practices
 
Clean Code summary
Clean Code summaryClean Code summary
Clean Code summary
 
Coding standard
Coding standardCoding standard
Coding standard
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practices
 
C# in depth
C# in depthC# in depth
C# in depth
 
Clean code
Clean codeClean code
Clean code
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
 
Clean Code
Clean CodeClean Code
Clean Code
 

Similar to Clean code: meaningful Name

The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
Ki Sung Bae
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
Ki Sung Bae
 
Coding standards php
Coding standards phpCoding standards php
Coding standards php
sagarrautray777
 
What's in a name
What's in a nameWhat's in a name
What's in a name
Koby Fruchtnis
 
Lecture No 13.ppt
Lecture No 13.pptLecture No 13.ppt
Lecture No 13.ppt
AhmadNaeem59
 
How keywords and reserved words differ?
How keywords and reserved words differ?How keywords and reserved words differ?
How keywords and reserved words differ?
Ajay Chimmani
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practices
Anilkumar Patil
 
Javascript Programming according to Industry Standards.pptx
Javascript Programming according to Industry Standards.pptxJavascript Programming according to Industry Standards.pptx
Javascript Programming according to Industry Standards.pptx
MukundSonaiya1
 
Naming Things Book : Simple Book Review!
Naming Things Book : Simple Book Review!Naming Things Book : Simple Book Review!
Naming Things Book : Simple Book Review!
Diego Pacheco
 
Clean code - DSC DYPCOE
Clean code - DSC DYPCOEClean code - DSC DYPCOE
Clean code - DSC DYPCOE
Patil Shreyas
 
What's in a Name?
What's in a Name?What's in a Name?
What's in a Name?
Kevlin Henney
 
8 programming concepts_you_should_know_in_2017
8 programming concepts_you_should_know_in_20178 programming concepts_you_should_know_in_2017
8 programming concepts_you_should_know_in_2017
Rajesh Shirsagar
 
Relevancy and synonyms - ApacheCon NA 2013 - Portland, Oregon, USA
Relevancy and synonyms - ApacheCon NA 2013 - Portland, Oregon, USARelevancy and synonyms - ApacheCon NA 2013 - Portland, Oregon, USA
Relevancy and synonyms - ApacheCon NA 2013 - Portland, Oregon, USA
Leonardo Dias
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
yannick grenzinger
 
Writing High Quality Code in C#
Writing High Quality Code in C#Writing High Quality Code in C#
Writing High Quality Code in C#
Svetlin Nakov
 
Keep It Simple - presentation at ASTC October 2018
Keep It Simple - presentation at ASTC October 2018Keep It Simple - presentation at ASTC October 2018
Keep It Simple - presentation at ASTC October 2018
Kirsty Taylor, CLPM
 

Similar to Clean code: meaningful Name (20)

Naming Conventions
Naming ConventionsNaming Conventions
Naming Conventions
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
 
Coding standards php
Coding standards phpCoding standards php
Coding standards php
 
What's in a name
What's in a nameWhat's in a name
What's in a name
 
Lecture No 13.ppt
Lecture No 13.pptLecture No 13.ppt
Lecture No 13.ppt
 
How keywords and reserved words differ?
How keywords and reserved words differ?How keywords and reserved words differ?
How keywords and reserved words differ?
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practices
 
Javascript Programming according to Industry Standards.pptx
Javascript Programming according to Industry Standards.pptxJavascript Programming according to Industry Standards.pptx
Javascript Programming according to Industry Standards.pptx
 
Naming Things Book : Simple Book Review!
Naming Things Book : Simple Book Review!Naming Things Book : Simple Book Review!
Naming Things Book : Simple Book Review!
 
Coding standard
Coding standardCoding standard
Coding standard
 
Clean code - DSC DYPCOE
Clean code - DSC DYPCOEClean code - DSC DYPCOE
Clean code - DSC DYPCOE
 
What's in a Name?
What's in a Name?What's in a Name?
What's in a Name?
 
Clean Code
Clean CodeClean Code
Clean Code
 
8 programming concepts_you_should_know_in_2017
8 programming concepts_you_should_know_in_20178 programming concepts_you_should_know_in_2017
8 programming concepts_you_should_know_in_2017
 
Relevancy and synonyms - ApacheCon NA 2013 - Portland, Oregon, USA
Relevancy and synonyms - ApacheCon NA 2013 - Portland, Oregon, USARelevancy and synonyms - ApacheCon NA 2013 - Portland, Oregon, USA
Relevancy and synonyms - ApacheCon NA 2013 - Portland, Oregon, USA
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
Writing High Quality Code in C#
Writing High Quality Code in C#Writing High Quality Code in C#
Writing High Quality Code in C#
 
Keep It Simple - presentation at ASTC October 2018
Keep It Simple - presentation at ASTC October 2018Keep It Simple - presentation at ASTC October 2018
Keep It Simple - presentation at ASTC October 2018
 
Object oriented data model
Object oriented data modelObject oriented data model
Object oriented data model
 

Recently uploaded

Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 

Recently uploaded (20)

Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 

Clean code: meaningful Name

  • 1.
  • 2. Clean Code : Meaningful Name Presented by Md. Nahidul Hasan
  • 3. Meaningful Name ■ Names are everywhere in software. ■ We name our variables, our functions, our arguments, classes, and packages. ■ We name our source files and the directories that contain them.We name our jar files and war files and ear files. ■ We name and name and name. Because we do so much of it, we’d better do it well “The hardest thing about choosing good names is that it requires good descriptive skills and a shared cultural background. This is a teaching issue rather than a technical, business, or management issue. As a result, many people in this field do not do it very well.”
  • 4. Use Intention-Revealing Names ■ Choosing good names takes time but saves more than it takes. ■ Take care with your names and change them when you find better ones. ■ The name of a variable, function or class should answer the big questions such as why it exists, what it does, how it is used. ■ If a name requires a comment, then the name does not reveal its intent. For example, these names specify what is being measured and the unit of that measurement: int d // elapsed time in days int elapsedTimeInDays; int daysSinceCreation; int daysSinceModification;
  • 5. Make Meaningful Distinctions ■ Distinguish names in such a way that the reader knows what the differences offer. For example, the two classes ProductInfo and ProductData have different names but the names don’t mean anything different, because here Info and Data are indistinct noise words. ■ More Example : getActiveAccount(); getActiveAccounts(); getActiveAccount(); getAllActiveAccounts();
  • 6. Use Pronounceable Names ■ Our minds have evolved to deal with spoken language, so take advantage of that when creating names. ■ Also, if you can’t pronounce it, you can’t discuss it without sounding like an idiot. ■ As an example, note the benefits of using the second version of this variable instead of the first version class DtaRcrd102 { private Date genymdhms; private Date modymdhms; private final String pszqint = "102"; /* ... */ }; class Customer { private Date generationTimestamp; private Date modificationTimestamp; private final String recordId = "102"; /* ... */ };
  • 7. Use Searchable Names ■ Names should be easy to locate across a body of text. ■ The length of a name should correspond to the size of its scope. ■ If a variable or constant might be seen or used in multiple places in a body of code, it is imperative to give it a search-friendly name. for (int j=0; j<34; j++) { s += (t[j]*4)/5; } int realDaysPerIdealDay = 4; const int WORK_DAYS_PER_WEEK = 5; int sum = 0; for (int j=0; j < NUMBER_OF_TASKS; j++) { int realTaskDays = taskEstimate[j] * realDaysPerIdealDay; int realTaskWeeks = (realdays / WORK_DAYS_PER_WEEK); sum += realTaskWeeks; }
  • 8. Avoid Encodings ■ Encoding type or scope information into names simply adds to the burden of deciphering. They are a mental burden, seldom pronounceable and easy to mistype. ■ Hungarian Notation (HN) adds nothing to Java, as the objects are strongly typed and IDEs detect type errors long before compilation. A variable that has its type changed, but accidentally not had its HN name changed can be misleading.
  • 9. Avoid Mental Mapping ■ Readers should not need to mentally translate your names into other names they already know/are familiar with. This problem generally arises from a choice to use NEITHER problem-domain terms nor solution-domain terms (see more, below, on problem-domain and solution-domain). ■ Single-letter variable names are really only okay for use as loop counters if their scope is very small and no other names can conflict with them; plus they are traditional. ■ In general programmers are pretty smart people. Smart people sometimes like to show off their smarts by demonstrating their mental juggling abilities. However: “One difference between a smart programmer and a professional programmer is that the professional understands that clarity is king. Professionals use their powers for good and write code that others can understand.”
  • 10. Class Names ■ Classes and objects should have noun or noun-phrase names, like Customer,WikiPage, Account and AddressParser. ■ But avoid names 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 deletePage or save. ■ Accessors and mutators, and their predicates, should be named according to JavaBeanstandards
  • 11. Don’t Pun ■ Don’t deliberately or inadvertently re-use the same term where its semantic meaning in the code is different; for example using the term add in several classes, for “consistency”, when you are in fact not using it in the same sense. Use Solution Domain Names ■ The people who read you code are programmers, so go ahead and use solution domain terms, such as AccountVisitor, where Visitor means the VISITOR pattern, or JobQueue. Use Problem Domain Names ■ When there is no programmer-eese for what you are doing, use the name from the problem domain. However: The code that has more to do problem domain concepts should have names drawn from the problem domain.
  • 12. Add Meaningful Context ■ Most names are not meaningful in and of themselves. So: You need to place names in a context for your reader by enclosing them in well-named classes, functions or namespaces. Naming within an Unclear Context: 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 Naming within a Clear Context: public class GuessStatisticsMessage { private String number; private String verb; private String pluralModifier; public String make(char candidate, int count) { createPluralDependentMessageParts(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. Pick One Word per Concept ■ Pick and use one word for abstract concept and stick with it. ■ A consistent lexicon is a great boon to the programmers who use your code. ■ Example: What’s the difference between fetch, retrieve and get? Don’t Be Cute ■ Don’t be too clever or humourous with your names. Don’t use a function name likeholyHandGrenade, when you mean deleItems. ■ For example, don’t use the name whack() to mean kill()
  • 15. Don’t Add Gratuitous Context ■ Shorter names are generally better than longer ones, so long as they are clear. ■ Add no more context to a name than is necessary. ■ Examples: 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. If you need to differentiate between MAC Addresses, port addresses, and Web addresses, you might consider PostalAddress, MAC, and URI. ■ In an imaginary application called “Gas Station Deluxe,” it is a bad idea to prefix every class with GSD. A mailingadress classs for in GSD’s accounting Module - GSDAccountAdress A mailing address for cutomer contact - GSDAccountAdress