A code writer is like any other writer; writes to be read and understood. To do so it must obey certain laws. They are so important that must be followed religiously.
Being a good writer is an art. But you can be a better programmer by knowing and applying a set of standards. In this guide, I will show you TypeScript samples for the 10 rules of clean naming.
When you're finished, you'll be able to write heavenly code.Let there be code naming conventions
1️⃣ Be Descriptive
// ❌
const width = 5;
class Image {
imageWidth = width;
}
// ✅
const imageWidth = 5;
class Image {
width = imageWidth;
}
2️⃣ Be Meaningful
// ❌
getClient(){}
readProvider(){}
postCustomer(){}
// ✅
getClient(){}
getProvider(){}
postClient(){}
3️⃣ Spell properly
// ❌
const crtAtTs = new Date();
function insInv() {}
// ✅
const createdAtTimestamp = new Date();
function insertInvoice() {}
4️⃣ Respect your language style
// ❌
const created_at = new Date();
const workingDays = 5;
function Calculate_payroll() {}
class employee {}
interface IPayable {}
// ✅
const createdAt = new Date();
const WORKING_DAYS = 5;
function calculatePayroll() {}
class Employee {}
interface Pay {}
5️⃣ Use verbs for doing or asking things
// ❌
function client() {
return new Client();
}
function allowed() {
return false;
}
// ✅
function createClient() {
return new Client();
}
function isAllowed() {
return true;
}
6️⃣ Be positive
// ❌
const isNotEmpty = true;
if (isNotEmpty) {
doSomething();
}
// ✅
const hasValue = true;
if (hasValue) {
doSomething();
}
7️⃣ Don't do magic
// ❌
function calculateDiscount(price: number) {
if (price > 1000) {
return price * 0.2;
} else {
return price * 0.1;
}
}
// ✅
function calculateDiscountedPrice(price: number) {
const EXPENSIVE_LIMIT_PRICE = 1000;
const EXPENSIVE_DISCOUNT_FACTOR = 0.2;
const CHEAP_DISCOUNT_FACTOR = 0.1;
if (price > EXPENSIVE_LIMIT_PRICE) {
return price * EXPENSIVE_DISCOUNT_FACTOR;
} else {
return price * CHEAP_DISCOUNT_FACTOR;
}
}
8️⃣ No tech encodings
// ❌
function getEmployeeList(paramCompanyNameString: string) {
return findInMongo(paramCompanyNameString);
}
// ✅
function getEmployees(companyName: string) {
return findByCompanyName(companyName);
}
9️⃣ No mental mapping
// ❌
const cs = customers();
const cn = cs.length;
for (i = 0; i < cn; i++) {
cs[i].sendInvoices();
}
cs.forEach((c) => c.sendInvoices());
// ✅
const customers = getCustomers();
const numberOfCustomers;
for (i = 0; i < numberOfCustomers; i++) {
customers[i].sendInvoices();
}
customers.forEach((c) => c.sendInvoices());
1️⃣0️⃣ No comments
// ❌
const d = 5; // days
// print the schedule
function print() {}
// ✅
const days = 5;
function printSchedule() {}
🌅 Conclusion
These ten commandments can be summed up in two:
✍🏼 Reveal the writer's intention
📖 With minimum reader effort
In this article, you have a set of clean naming best practice examples. Don't be guilty of writing hard-to-understand code. Words are the bricks to build narratives. Choose your names to say what your code does clearly.
learn, code, enjoy, repeat
Alberto Basalo