In this tutorial, we aim to improve your JavaScript coding skills by teaching you how to write clean, efficient, and readable JavaScript code. Good code is not just about making your code work; it's also about writing code that is easy to read, understand, and maintain by others.
You will learn about:
Prerequisites: Basic knowledge of JavaScript.
Variable names should be descriptive and indicate the data they hold. For instance, instead of x
, y
, z
, you should have firstName
, age
, address
.
// Bad practice
let x = 'John Doe';
// Good practice
let fullName = 'John Doe';
Comments are crucial for understanding the purpose of certain code blocks. Use them liberally to explain why you did something, not just what you did.
// Good practice
// This function calculates the sum of two numbers
function sum(a, b) {
return a + b;
}
The DRY (Don't Repeat Yourself) principle is about reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it.
// Bad practice
let areaCircle1 = 3.14 * radius1 * radius1;
let areaCircle2 = 3.14 * radius2 * radius2;
// Good practice
function calculateArea(radius) {
return 3.14 * radius * radius;
}
let areaCircle1 = calculateArea(radius1);
let areaCircle2 = calculateArea(radius2);
// This variable holds the name of a user
let userName = 'John Doe'; // Good practice
// This variable holds the age of a user
let userAge = 25; // Good practice
// This function calculates the sum of two numbers
function sum(a, b) {
// The function receives two parameters a and b
// It returns the sum of a and b
return a + b; // Good practice
}
// This function calculates the area of a circle
function calculateArea(radius) {
// It receives one parameter: radius
// It returns the area of a circle
return 3.14 * radius * radius; // Good practice
}
// Now we can use this function to calculate the area of any circle
let areaCircle1 = calculateArea(5); // Good practice
let areaCircle2 = calculateArea(10); // Good practice
In this tutorial, we've covered how to write clean and readable JavaScript code. We've learned the importance of using meaningful variable names, the necessity of adding comments, and the benefits of keeping our code DRY.
To further enhance your JavaScript skills, you can practice by writing your own functions and applying the principles we've learned. You might also want to explore other best practices like using consistent indentation, keeping your functions small and single-purposed, and handling errors properly.
Exercise 1: Write a function that takes two numbers and returns their product. Make sure to use meaningful variable names and comments.
Exercise 2: Write a function that calculates the area of a rectangle. Use the DRY principle to avoid code repetition.
Solutions:
Exercise 1:
// This function calculates the product of two numbers
function multiply(num1, num2) {
// It returns the product of num1 and num2
return num1 * num2;
}
Exercise 2:
// This function calculates the area of a rectangle
function calculateRectangleArea(length, width) {
// It returns the area of a rectangle
return length * width;
}
// Now we can use this function to calculate the area of any rectangle
let areaRectangle1 = calculateRectangleArea(5, 10);
let areaRectangle2 = calculateRectangleArea(8, 12);
Always remember, practice makes perfect. Keep practicing and happy coding!