JavaScript Clean Code 🛠
Avoid negative conditionals 💡
Bad :
Good :
➖➖➖➖➖➖
#JSTips #CleanCode
@ProgrammingTip
Avoid negative conditionals 💡
Bad :
function isDOMNodeNotPresent(node) {
// ...
}
if (!isDOMNodeNotPresent(node)) {
// ...
}Good :
function isDOMNodePresent(node) {
// ...
}
if (isDOMNodePresent(node)) {
// ...
}➖➖➖➖➖➖
#JSTips #CleanCode
@ProgrammingTip
Async & Await in JavaScript 🔥
Async/Await will make your code simpler even more than you think. ✅
Stop writing callback functions and love JavaScript ES7. 💎
https://blog.patricktriest.com/what-is-async-await-why-should-you-care/
#JavaScript #CleanCode
@ProgrammingTip
Async/Await will make your code simpler even more than you think. ✅
Stop writing callback functions and love JavaScript ES7. 💎
https://blog.patricktriest.com/what-is-async-await-why-should-you-care/
#JavaScript #CleanCode
@ProgrammingTip
Break | Better
Async/Await Will Make Your Code Simpler
Or How I Learned to Stop Writing Callback Functions and Love Javascript ES8.
JavaScript Clean Code 🛠
Use getters and setters 💡
Bad :
Good :
➖➖➖➖➖➖
#JSTips #CleanCode
@ProgrammingTip
Use getters and setters 💡
Bad :
function makeBankAccount() {
return {
balance: 0,
// ...
};
}
const account = makeBankAccount();
account.balance = 100;Good :
function makeBankAccount() {
let balance = 0;
function getBalance() {
return balance;
}
function setBalance(amount) {
balance = amount;
}
return {
getBalance,
setBalance,
};
}
const account = makeBankAccount();
account.setBalance(100);➖➖➖➖➖➖
#JSTips #CleanCode
@ProgrammingTip
JavaScript Clean Code 🛠
Async/Await are even cleaner than Promises 💡
Bad :
Good :
➖➖➖➖➖➖
#JSTips #CleanCode
@ProgrammingTip
Async/Await are even cleaner than Promises 💡
Bad :
get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin')
.then((response) => {
return writeFile('article.html', response);
})
.then(() => {
console.log('File written');
})
.catch((err) => {
console.error(err);
});Good :
async function getCleanCodeArticle() {
try {
const response = await get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin');
await writeFile('article.html', response);
console.log('File written');
} catch(err) {
console.error(err);
}
}➖➖➖➖➖➖
#JSTips #CleanCode
@ProgrammingTip
JavaScript Clean Code 🛠
Only comment things that have business logic complexity. 💡
Comments are an apology, not a requirement. Good code mostly documents itself.
Bad :
Good :
➖➖➖➖➖➖
#JSTips #CleanCode
@ProgrammingTip
Only comment things that have business logic complexity. 💡
Comments are an apology, not a requirement. Good code mostly documents itself.
Bad :
function hashIt(data) {
// The hash
let hash = 0;
// Length of string
const length = data.length;
// Loop through every character in data
for (let i = 0; i < length; i++) {
// Get character code.
const char = data.charCodeAt(i);
// Make the hash
hash = ((hash << 5) - hash) + char;
// Convert to 32-bit integer
hash &= hash;
}
}Good :
function hashIt(data) {
let hash = 0;
const length = data.length;
for (let i = 0; i < length; i++) {
const char = data.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
// Convert to 32-bit integer
hash &= hash;
}
}➖➖➖➖➖➖
#JSTips #CleanCode
@ProgrammingTip
Refactoring •GURU• 🐿
Refactoring.Guru is a shiny website where you can find tons of information on refactoring, design patterns, SOLID principles and other smart programming topics. ✨
https://refactoring.guru/
#CleanCode #Refactoring
@ProgrammingTip
Refactoring.Guru is a shiny website where you can find tons of information on refactoring, design patterns, SOLID principles and other smart programming topics. ✨
https://refactoring.guru/
#CleanCode #Refactoring
@ProgrammingTip
refactoring.guru
Refactoring and Design Patterns
Refactoring is a controllable process of improving code without creating new functionality. Design Patterns are typical solutions to the commonly occurring problems in software design.
JavaScript Clean Code 🛠
Use consistent capitalization 💡
Bad :
Good :
➖➖➖➖➖➖
#JSTips #CleanCode
@ProgrammingTip
Use consistent capitalization 💡
Bad :
const DAYS_IN_WEEK = 7;
const daysInMonth = 30;
const songs = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'];
const Artists = ['ACDC', 'Led Zeppelin', 'The Beatles'];
function eraseDatabase() {}
function restore_database() {}
class animal {}
class Alpaca {}
Good :
const DAYS_IN_WEEK = 7;
const DAYS_IN_MONTH = 30;
const SONGS = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'];
const ARTISTS = ['ACDC', 'Led Zeppelin', 'The Beatles'];
function eraseDatabase() {}
function restoreDatabase() {}
class Animal {}
class Alpaca {}
➖➖➖➖➖➖
#JSTips #CleanCode
@ProgrammingTip
Try to group extension methods into static classes dealing with the same extended type. 📦
Sometimes related classes (such as DateTime and TimeSpan) can be sensibly grouped together. ↔️
But avoid grouping extension methods targeting disparate types such as Stream and string within the same class. ❌
〰〰〰〰〰〰
#CleanCode #ExtensionMethods
@ProgrammingTip
Sometimes related classes (such as DateTime and TimeSpan) can be sensibly grouped together. ↔️
But avoid grouping extension methods targeting disparate types such as Stream and string within the same class. ❌
〰〰〰〰〰〰
#CleanCode #ExtensionMethods
@ProgrammingTip
6 Simple Tips on How to Start Writing Clean Code 👾
1️⃣ Make code readable for people.
2️⃣ Use meaningful names for variables, functions and methods.
3️⃣ Let every function or method perform only one task.
4️⃣ Use comments for clarification ( if really needed )
5️⃣ Be consistent
6️⃣ Review your code regularly
〰〰〰〰〰〰
#CleanCode
@ProgrammingTip
1️⃣ Make code readable for people.
2️⃣ Use meaningful names for variables, functions and methods.
3️⃣ Let every function or method perform only one task.
4️⃣ Use comments for clarification ( if really needed )
5️⃣ Be consistent
6️⃣ Review your code regularly
〰〰〰〰〰〰
#CleanCode
@ProgrammingTip
Yagni : You Arent Gonna Need It 👾
Never Add Functionality Early 💡
Even if you're totally, totally, totally sure that you'll need a feature later on, don't implement it now. ⏳
Usually, what you actually need is quite different from what you foresaw needing earlier. 🙅🏻♂️
This doesn't mean you should avoid building flexibility into your code. ✅
It means you shouldn't overengineer something based on what you think you might need later on. ❌
🔹🔸🔹🔸🔹🔸
There are two main reasons to practise YagNi :
1️⃣ You save time, because you avoid writing code that you turn out not to need.
2️⃣ Your code is better, because you avoid polluting it with 'guesses' that turn out to be more or less wrong but stick around anyway.
This also follows the KISS theorem:
https://t.me/pgimg/13
〰〰〰〰〰〰
#CleanCode #Principles #Tips
@ProgrammingTip
Never Add Functionality Early 💡
"Always implement things when you actually need them, never when you just foresee that you need them."
Even if you're totally, totally, totally sure that you'll need a feature later on, don't implement it now. ⏳
Usually, what you actually need is quite different from what you foresaw needing earlier. 🙅🏻♂️
This doesn't mean you should avoid building flexibility into your code. ✅
It means you shouldn't overengineer something based on what you think you might need later on. ❌
🔹🔸🔹🔸🔹🔸
There are two main reasons to practise YagNi :
1️⃣ You save time, because you avoid writing code that you turn out not to need.
2️⃣ Your code is better, because you avoid polluting it with 'guesses' that turn out to be more or less wrong but stick around anyway.
This also follows the KISS theorem:
Keep it simple, stupid!
https://t.me/pgimg/13
〰〰〰〰〰〰
#CleanCode #Principles #Tips
@ProgrammingTip
Telegram
Programming Tips Resources