Writing Clean and Readable JavaScript Code

Tutorial 5 of 5

Writing Clean and Readable JavaScript Code

1. Introduction

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:

  • Meaningful variable names
  • Importance of comments
  • The DRY (Don't Repeat Yourself) principle

Prerequisites: Basic knowledge of JavaScript.

2. Step-by-Step Guide

2.1 Meaningful Variable Names

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';

2.2 Importance of Comments

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;
}

2.3 The DRY Principle

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);

3. Code Examples

Example 1: Using meaningful variable names

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

Example 2: Using comments for code explanation

// 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
}

Example 3: Applying DRY Principle

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

4. Summary

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.

5. Practice Exercises

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!