DEV Community

Malik-Haziq
Malik-Haziq

Posted on

Make your JavaScript code more efficient, readable, and maintainable.(Part 2)

Here are a few JavaScript tips and tricks with examples and reasons.

Use clear and meaningful variable and function names:

By using clear and meaningful names for your variables and functions, it becomes much easier for other developers (or even future you) to understand what the code is doing. It also makes it much easier to find and fix bugs.

//bad
let x = 5;
let y = 10;
let z = x * y;

//good
let width = 5;
let height = 10;
let area = width * height;
Enter fullscreen mode Exit fullscreen mode

Reason: Using clear and meaningful variable and function names makes the code more readable and understandable, which helps other developers (or even future you) to understand what the code is doing. It also makes it much easier to find and fix bugs.

Break code into smaller functions:

Breaking code into smaller, reusable functions makes it much easier to understand and maintain. It also makes it much easier to test and debug.

//bad
function calculateTotal(items) {
  let total = 0;
  for (let item of items) {
    total += item.price * item.quantity;
  }
  return total;
}

//good
function calculateTotal(items) {
  return items.reduce((total, item) => total + calculatePrice(item), 0);
}

function calculatePrice(item) {
  return item.price * item.quantity;
}

Enter fullscreen mode Exit fullscreen mode

Reason: Breaking code into smaller functions makes the code more manageable and reusable. It also makes it much easier to test and debug. This will help to make the code more maintainable and easy to understand.

Use destructuring assignment to unpack values from arrays or objects:

Destructuringassignment makes it easy to unpack values from arrays or objects, which makes your code shorter and more readable.

// general way
let user = {name: 'John', age: 30};
let name = user.name;
let age = user.age;
console.log(name); // 'John'
console.log(age); // 30

//using destructuring 
let {name, age} = user;
console.log(name); // 'John'
console.log(age); // 30

Enter fullscreen mode Exit fullscreen mode

Reason: Using destructuring assignment to unpack values from arrays or objects makes the code shorter and more readable, it also makes it easy to extract values from arrays or objects.

Use spread operator to expand arrays and objects:

The spread operator makes it easy to expand arrays and objects, which makes your code shorter and more readable.

let numbers = [1, 2, 3];
let moreNumbers = [4, 5, 6];
let allNumbers = [...numbers, ...moreNumbers];
console.log(allNumbers); // [1, 2, 3, 4, 5, 6]

Enter fullscreen mode Exit fullscreen mode

Reason: Using spread operator to expand arrays and objects makes the code shorter and more readable, it also makes it easy to expand arrays and objects.

Use default parameters to provide default values for function arguments:

Default parameters make it easy to provide default values for function arguments, which makes your code more robust and easier to understand..

//bad
function divide(a, b) {
  if (b === undefined) {
    b = 1;
  }
  return a / b;
}
console.log(divide(10)); // 10

//good
function divide(a, b = 1) {
  return a / b;
}
console.log(divide(10)); // 10

Enter fullscreen mode Exit fullscreen mode

Reason: Using default parameters to provide default values for function arguments makes the code more robust and easier to understand, it also makes it easy to provide default values for function arguments.

Use comments to explain complex or non-obvious code:

Comments can be a great tool for making code more readable and understandable. Use comments to explain what the code is doing, and why it's doing it.

//calculate the area of the rectangle
function rectangleArea(width, height) {
  return width * height;
}

Enter fullscreen mode Exit fullscreen mode

Reason: Using comments to explain complex or non-obvious code helps to make the code more readable and understandable. This helps other developers (or even future you) to understand the purpose of the code and how it works.

Top comments (1)

Collapse
 
haidernqvi profile image
haidernqvi

Good