DEV Community

Cover image for Tips for Improving Code Length and Making Your Code More Concise
Luckey
Luckey

Posted on

Tips for Improving Code Length and Making Your Code More Concise

1. Use destructuring to extract values from objects and arrays:

Destructuring allows you to extract values from objects and arrays and assign them to variables in a concise syntax. This can help reduce the amount of code you need to write and make your code easier to read.

// Good code - uses destructuring
let user = { name: 'John', email: 'john@example.com', password: '123456' };
let { name, email, password } = user;

console.log(name); // 'John'
console.log(email); // 'john@example.com'
console.log(password); // '123456'

// Poor code - does not use destructuring
let user = { name: 'John', email: 'john@example.com', password: '123456' };
let name = user.name;
let email = user.email;
let password = user.password;

console.log(name); // 'John'
console.log(email); // 'john@example.com'
console.log(password); // '123456'

Enter fullscreen mode Exit fullscreen mode

2. Use shorthand syntax for common operations

JavaScript provides shorthand syntax for common operations, such as incrementing and decrementing variables and adding and subtracting from variables. Using this shorthand syntax can help reduce the amount of code you need to write and make your code easier to read.

// Good code - uses shorthand syntax
let count = 0;
count++;
count--;

let total = 100;
total += 50;
total -= 25;

// Poor code - does not use shorthand syntax
let count = 0;
count = count + 1;
count = count - 1;

let total = 100;
total = total + 50;
total = total - 25;

Enter fullscreen mode Exit fullscreen mode

3. Use the ternary operator for simple conditional statements

The ternary operator allows you to create simple conditional statements in a concise syntax, which can help reduce the amount of code you need to write and make your code easier to read.

// Good code - uses ternary operator
let num = 5;
let result = num > 0 ? 'positive' : 'negative';

console.log(result); // 'positive'

// Poor code - does not use ternary operator
let num = 5;
let result;
if (num > 0) {
  result = 'positive';
} else {
  result = 'negative';
}

console.log(result); // 'positive'

Enter fullscreen mode Exit fullscreen mode

4. Use the shorthand syntax for object methods

JavaScript provides shorthand syntax for defining object methods, which can help reduce the amount of code you need to write and make your code easier to read.

// Good code - uses shorthand syntax
let user = {
  name: 'John',
  email: 'john@example.com',
  greet() {
    console.log(`Hello, my name is ${this.name}!`);
  }
};

user.greet(); // 'Hello, my name is John!'

// Poor code - does not use shorthand syntax
let user = {
  name: 'John',
  email: 'john@example.com',
  greet: function() {
    console.log(`Hello, my name is ${this.name}!`);
  }
};

user.greet(); // 'Hello, my name is John!'

Enter fullscreen mode Exit fullscreen mode

5. Use the shorthand syntax for computed property names

JavaScript provides shorthand syntax for defining object properties with computed names, which can help reduce the amount of code you need to write and make your code easier to read.

// Good code - uses shorthand syntax
let name = 'John';
let user = { [name]: 'Hello, my name is John!' };

console.log(user.John); // 'Hello, my name is John!'

// Poor code - does not use shorthand syntax
let name = 'John';
let user = {};
user[name] = 'Hello, my name is John!';

console.log(user.John); // 'Hello, my name is John!'

Enter fullscreen mode Exit fullscreen mode

6. Use the for-of loop to iterate over arrays

The for-of loop allows you to iterate over arrays and other iterable objects in a concise syntax, which can help reduce the amount of code you need to write and make your code easier to read.

// Good code - uses for-of loop
let numbers = [1, 2, 3, 4, 5];
for (let number of numbers) {
  console.log(number);
}

// Poor code - does not use for-of loop
let numbers = [1, 2, 3, 4, 5];
for (let i = 0; i < numbers.length; i++) {
  console.log(numbers[i]);
}

Enter fullscreen mode Exit fullscreen mode

7. Use the for-in loop to iterate over objects

The for-in loop allows you to iterate over the properties of an object in a concise syntax, which can help reduce the amount of code you need to write and make your code easier to read.

// Good code - uses for-in loop
let user = { name: 'John', email: 'john@example.com', password: '123456' };
for (let key in user) {
  console.log(`${key}: ${user[key]}`);
}

// Poor code - does not use for-in loop
let user = { name: 'John', email: 'john@example.com', password: '123456' };
for (let i = 0; i < Object.keys(user).length; i++) {
  let key = Object.keys(user)[i];
  console.log(`${key}: ${user[key]}`);
}

Enter fullscreen mode Exit fullscreen mode

8. Use the every() method to test if all elements in an array meet a certain condition

The every() method allows you to test if all elements in an array meet a certain condition in a concise syntax, which can help reduce the amount of code you need to write and make your code easier to read.

// Good code - uses Array.prototype.every()
let numbers = [1, 2, 3, 4, 5];
let allPositive = numbers.every(n => n > 0);

console.log(allPositive); // true

// Poor code - does not use Array.prototype.every()
let numbers = [1, 2, 3, 4, 5];
let allPositive = true;
for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] <= 0) {
    allPositive = false;
    break;
  }
}

console.log(allPositive); // true

Enter fullscreen mode Exit fullscreen mode

9. Use the spread operator to concatenate arrays or spread objects

The spread operator allows you to concatenate arrays or spread objects in a concise syntax, which can help reduce the amount of code you need to write and make your code easier to read.

// Good code - uses spread operator
let numbers1 = [1, 2, 3];
let numbers2 = [4, 5, 6];
let numbers3 = [...numbers1, ...numbers2];

console.log(numbers3); // [1, 2, 3, 4, 5, 6]

let user1 = { name: 'John', email: 'john@example.com' };
let user2 = { password: '123456' };
let user3 = { ...user1, ...user2 };

console.log(user3); // { name: 'John', email: 'john@example.com', password: '123456' }

// Poor code - does not use spread operator
let numbers1 = [1, 2, 3];
let numbers2 = [4, 5, 6];
let numbers3 = numbers1.concat(numbers2);

console.log(numbers3); // [1, 2, 3, 4, 5, 6]

let user1 = { name: 'John', email: 'john@example.com' };
let user2 = { password: '123456' };
let user3 = Object.assign({}, user1, user2);

console.log(user3); // { name: 'John', email: 'john@example.com', password: '123456' }

Enter fullscreen mode Exit fullscreen mode

10. Use the find() method to find the first element in an array that meets a certain condition:

The find() method allows you to find the first element in an array that meets a certain condition in a concise syntax, which can help reduce the amount of code you need to write and make your code easier to read.

// Good code - uses Array.prototype.find()
let numbers = [1, 2, 3, 4, 5];
let firstEvenNumber = numbers.find(n => n % 2 === 0);

console.log(firstEvenNumber); // 2

// Poor code - does not use Array.prototype.find()
let numbers = [1, 2, 3, 4, 5];
let firstEvenNumber;
for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] % 2 === 0) {
    firstEvenNumber = numbers[i];
    break;
  }
}

console.log(firstEvenNumber); // 2

Enter fullscreen mode Exit fullscreen mode

If you want to learn more tips in JavaScript, you can now ahead to my other articles like below:

Top comments (0)