Some best practices for Class, Variable, Functions naming convention
// Bad variables naming
var s= 0 // employee salary
var n= 0 // employee name
// Bad functions naming
fun salary()
fun name()
// Bad classes naming to get user data
class EmployeeInfo()
// Best practices varibales naming
var employeeSalary = 0
var employeeName= “”
// Best practices functions naming
fun getEmployeeName()
fun setEmployeeName()
// Best practices classes naming to get user data
class Employee()
When naming a function, variable or class, you should keep the following things in mind:
- Choose a word with meaning (provide some context)
- The name should be descriptive, concise and self-explanatory.
- Avoid generic names (like
tmp
) - Attach additional information to a name (use suffix or prefix)
- Don’t make your names too long or too short
- Use consistent formatting
- Avoid using the same name for different functions, variables or classes.
- Classes should use PascalCase, while variables and functions should use camelCase.
More Example:
// bad
var dogname = "Droopy"
// bad
var dog_name = "Droopy"
// bad
var DOGNAME = "Droopy"
// bad
var DOG_NAME = "Droopy"
// bad
var d = "Scooby-Doo"
// bad
var name = "Scooby-Doo"
// good
var dogName = "Scooby-Doo"
// good
var dogName = "Droopy"
// bad
var bark = false
// good
var isBark = false
// bad
var ideal = true
// bad
var owner = true
// good
var hasOwner = true
// good
var areIdeal = true
For constant variable you may use uppercase. For example:
//kotlin
val LEG = 4
val TAIL = 1
val MOVABLE = LEG + TAIL
//more than one world
val DAYS_UNTIL_TOMORROW = 1
Pointer variables should be prepended with ‘p’ and place the asterisk ‘*’ close to the variable name instead of the pointer type.
int *pAge, address; // Here only pAge is a pointer variable
Reference variables should be prepended with ‘r’. This helps to differentiate between the method returning a modifiable object and the same method returning a non-modifiable object.
Static variables should be prepended with ‘s’.
static int sCount;
For function:
You should use descriptive nouns and verbs as prefixes. For example, if we declare a function to retrieve a name, the function name should be getName.
// bad
function name(dogName, ownerName) {
return '${dogName} ${ownerName}';
}
// good
function getName(dogName, ownerName) {
return '${dogName} ${ownerName}';
}
The function should clearly hint at what it does. Each method/ function name should begin with a verb.
- Suffixes are sometimes useful. For example,
- Count- the current count of the counter.
- Key- the key value.
- Prefixes are sometimes useful. For example,
- get-get value.
- set- set value.
//java
int getValue();
The first character of function/ method argument names should be lowercase. Each word should also begin with a capital letter.
For class:
The major difference between function and class names is that we have to use Pascal case for class names. Use noun as a class name. The first character in the class name must be in upper case.
//java scripts
class DogCartoon {
constructor(dogName, ownerName) {
this.dogName = dogName;
this.ownerName = ownerName;
}
}
class FingerprintScanner
//java scripts
var cartoon = new DogCartoon('Scooby-Doo', 'Shaggy');
Ping me: Linkedin