Programming Tips 💡
51.6K subscribers
67 photos
10 videos
30 files
354 links
Programming & AI:
Tips 💡
Articles 📕
Resources 👾
Design Patterns 💎
Software Principles

🇳🇱 Contact: @MoienTajik

🎯 Buy ads: https://telega.io/c/ProgrammingTip
Download Telegram
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
JavaScript Clean Code 🛠

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 :
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 :
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