
- Clean Code - Home
- Clean Code - Best Practices & Principles
- Clean Code - Naming Conventions
- Clean Code - Comments & Documentation
- Clean Code - Testing Practice
- Clean Code - SOLID Principles
- Clean Code - Design Patterns
- Clean Code - Code Smells
- Clean Code - Refractoring
- Clean Code Useful Resources
- Clean Code - Useful Resources
- Discuss Clean Code
Clean Code - Naming Conventions
What are Naming Conventions?
Naming conventions in software development refers to a systematic way to name various things like Functions, variables, classes, methods, files, directories and database tables.
These conventions are validated for creating the consistent structure of our codebases. Following naming conventions make code readable, easy to understand, and make it easy to maintain. It helps all developers who are working on the same project. Naming Conventions help to avoid confusion and reduce errors also make collaboration easier.
Naming Styles
There are several naming conventions that are mostly used in software development. Following are some of the commonly used naming styles:
- PascalCase - In PascalCase, the first letter of each word is capitalized. For example, MyVariable, MyFunction, MyClass.
let MyVariable = 10; let MyFunction = () => {}; let MyClass = class {};
let myVariable = 10; let myFunction = () => {}; let myClass = class {};
let my_variable = 10; let my_function = () => {}; let my_class = class {};
let my-variable = 10; let my-function = () => {}; let my-class = class {};
Naming Standards
- Use Intention-Revealing Names
- Use Pronounceable Names
- Make Meaningful Distinctions
- Use Consistent Names
- Use Searchable Names
- Don't be Cute or Offensive
Use Intention-Revealing Names
Keep the names descriptive and when we write the name it should reveal its intention itself. The name should tell you what it's for or what it does without needing extra comments. If a variable holds a user's age, call it userAge not x or something vague.
int x; //bad name int userAge; //good name int d; //bad name int distance; //good name
Use Pronounceable Names
Names should be easy to say out loud without twisting your tongue. Its easier to talk about the code when you can actually pronounce the names. If something is for calculating a discount, call it calculateDiscount, not clcDscnt or any weird abbreviations.
int clcDscnt; //bad name int calculateDiscount; //good name int usrNm; //bad name int userName; //good name
Make Meaningful Distinctions
Don't just add numbers or vague suffixes to names to make them different. If you have data1 and data2, it doesnt really help. Be specific, like userData and orderData. Names should mean something on their own.
int data1; //bad name int userData; //good name int data2; //bad name int orderData; //good name
Use Consistent Names
If you pick a name style, stick to it everywhere. Dont mix getTotal with fetchTotal for similar methods; just pick one way and go with it across the whole codebase. Consistency makes everything smoother.
int getTotal; int fetchAmount; //not consistent int getAmount; //consistent
Use Searchable Names
Use names that can be easily searched in the codebase. One-letter names or cryptic abbreviations might save you a few keystrokes, but they make it hard to find stuff later. Use totalAmount rather than ta, so it's easier to locate and understand.
int ta; //bad name int totalAmount; //good name int uAge; //bad name int userAge; //good name
Don't be Cute or Offensive
Avoid using funny or slang names that might seem cool in the moment. It might confuse others or even come off as unprofessional. Naming something doMagic doesnt help anyone understand what its actually doing. Just keep it straightforward.
int killThat; //bad name int deleteRecord; //good name int doMagic; //bad name int addNumbers; //good name
Rules for Variables, Classes, Methods, and Constants
Naming conventions in coding help to keep everything organized and easy to understand. By following standard rules for naming different items, you make your code readable and maintainable. Heres how to name variables, classes, methods, and constants in a consistent and meaningful way.
Variables
When naming variables, use camelCase, which starts with a lowercase letter and capitalizes the first letter of each subsequent word. This style shows us that the variable is a simple piece of data being used in the code. Verify if the name reflects what the variable represents. For example, use totalAmount to represent the total sum or for storing a person's name we use customerName. Avoid random names like temp or value because later, we can't really identify what it is and why it is for. The simple aim is we have to make names self-explanatory, So we don't need to write extra comments.
let totalAmount = 100; let customerName = 'John Doe'; let temp = 0; //don't use let value = 10; //don't use
Classes
Class names should use PascalCase, means each word starts with a capital letter. Classes typically represent things, objects, or entities. So, the name should be a noun or noun phrase. For example, a class representing a user account can be named UserAccount, while if a class handles orders it can be called OrderManager. The goal is to give class names that clearly identify what the class is or does, it makes it easier to recognize from the different components of the code.
class UserAccount class OrderManager class useraccount //don't use class order_manager //don't use
Methods/Functions
Use camelCase for methods or functions, start with a lowercase letter and make sure the name describes what the function actually does. The name should be an action. Usually, we use verbs to make it clear that something is being done. For example, fetchData() it says that the method retrieves some data, and calculateTotal() says that a total is being calculated. This naming style helps us to quickly understand the method's behavior without looking at the code inside it.
function fetchData() {} function calculateTotal() {} function getdata() {} //don't use function total() {} //don't use
Constants
Constants should always be written in UPPERCASE, and use underscores for separating words. This style helps us to make a difference between constants from variables and also shows that we should not change the value. For example, for a fixed upper boundary use MAX_LIMIT. use PI_VALUE for the mathematical constant. This approach makes constants stand out in the code and let us know that they hold fixed values.
const MAX_LIMIT = 100; const PI_VALUE = 3.14; const maxLimit = 100; //don't use const piValue = 3.14; //don't use