coding standard guidelines
Studies have shown that developers who use best practices when coding are up to 40% more productive than those who do not, and that code with good practices is up to 50% cheaper to maintain than code with poor practices.
Also, companies and governments lose a lot of money every day because developers don't follow the best practices and guidelines. Here are some examples you can read more about:
This lesson is a compilation of best practices that we promote at 4Geeks, it has been created and grown with contributions from all the community mentors.
It's best to avoid using global variables whenever possible, use more function arguments and return values to share data between different parts of a program. This can help you make code more maintainable, testable, and reliable.
Some of the naming conventions are given below:
camelCase
for JavaScript, Node, Java, etc. And in snake_case
for Python, Ruby, etc. Here you can read more about variable naming conventions.camelCase
starting with lower case.1Descriptive variable names: 2✅ GOOD: "customer_name" (describes the contents of the variable) 3❌ BAD: "x" (not descriptive or meaningful) 4 5Use naming conventions: 6✅ GOOD: "number_of_items" (full words and snake_case are used) 7❌ BAD: "n_items" (abbreviation is used) 8 9✅ GOOD: "customerName" (camelCase is used) 10❌ BAD: "customername" (no naming convention is used) 11 12Avoid using single-letter names: 13✅ GOOD: "customer_name" (descriptive and meaningful) 14❌ BAD: "x" (single-letter and not descriptive) 15 16Keep names short, but not too short: 17✅ GOOD: "product_price" (short and descriptive) 18❌ BAD: "p" (too short and not descriptive)
Use Prettier or any automatic indentation tool. If that is not possible (for some weird reason), make sure to manually indent every single line:
1if (condition) { 2 // code block 1 3} else if (condition) { 4 // code block 2 5} else { 6 // code block 3 7}
try-except
and try-catch
blocks to handle exceptions..catch()
function or try-catch
block (if using promises) and show the error on the console.Creating code that is easy to read is essential for producing high-quality software that is reliable, maintainable, and easy to modify or update. We have a whole article in code readability but here is a very short summary of the best practices:
else
statements.When possible avoid using nested functions like:
1function calculateDiscount(item, quantity) { 2 if (item.category === 'clothing') { 3 if (quantity >= 10) { 4 return item.price * 0.9; 5 } else { 6 return item.price * 0.95; 7 } 8 } else if (item.category === 'electronics') { 9 if (quantity >= 5) { 10 return item.price * 0.8; 11 } else { 12 return item.price * 0.9; 13 } 14 } else { 15 return item.price; 16 } 17}
The code is nested too deeply and has a complex structure that can be difficult to follow. This can be improved by refactoring the code to use fewer levels of nesting, or by using early returns or refactoring the conditional statements to simplify the structure. Here is an example:
1function calculateDiscount(item, quantity) { 2 if (item.category !== 'clothing' && item.category !== 'electronics') { 3 return item.price; 4 } 5 6 let discount = 1.0; 7 if (item.category === 'clothing') { 8 discount = quantity >= 10 ? 0.9 : 0.95; 9 } else { 10 discount = quantity >= 5 ? 0.8 : 0.9; 11 } 12 13 return item.price * discount; 14}