The Zen of Python Applied to Node.js

Introduction

The Zen of Python is a collection of guiding principles for writing computer programs in the Python language, but its wisdom extends beyond Python. These principles emphasize readability, simplicity, and practicality, making them valuable for developers working in any language, including Node.js. Below we will explore each principle with explanations and examples of good and bad implementations in Node.js.

Principles and Examples

1. Beautiful is better than ugly.

Good Example:

1
2
3
4
function formatUser(user) {
  return `${user.firstName} ${user.lastName}`;
}
console.log(formatUser({ firstName: "Alice", lastName: "Smith" }));

Bad Example:

1
2
function f(u){return u.fN+" "+u.lN;}
console.log(f({fN:"Alice",lN:"Smith"}));

Ugly code sacrifices clarity and maintainability.

2. Explicit is better than implicit.

Good Example:

1
2
3
4
5
6
function getUserFullName(user) {
  if (!user || !user.firstName || !user.lastName) {
    throw new Error("Invalid user object");
  }
  return `${user.firstName} ${user.lastName}`;
}

Bad Example:

1
2
3
4
5
6
7
8
9
10
function isUserFullNameValid(user) {
  return user && user.firstName && user.lastName;
}

function getUserFullName(user) {
  if (!isUserFullNameValid(user)) {
    throw new Error("Invalid user object");
  }
  return `${user.firstName} ${user.lastName}`;
}

Extracting the check into a separate function for such a small validation adds unnecessary indirection, making the code harder to follow. Keeping the check inline makes it more explicit and readable.

3. Simple is better than complex.

Good Example:

1
2
3
function isEven(num) {
  return num % 2 === 0;
}

Bad Example:

1
2
3
function isEven(num) {
  return (num / 2 === Math.floor(num / 2)) ? true : false;
}

The simpler approach is easier to read and maintain.

4. Complex is better than complicated.

Good Example:

1
2
3
4
5
function fetchData(url) {
  return fetch(url)
    .then(response => response.json())
    .catch(error => console.error("Fetch error:", error));
}

Bad Example:

1
2
3
4
5
6
7
8
function fetchData(url) {
  return new Promise((resolve, reject) => {
    fetch(url)
      .then(response => response.json())
      .then(data => resolve(data))
      .catch(err => reject(err));
  });
}

Unnecessary complexity should be avoided when simpler solutions exist.

5. Flat is better than nested.

Good Example:

1
2
3
4
5
async function processUser(userId) {
  const user = await getUser(userId);
  const orders = await getOrders(userId);
  return { user, orders };
}

Bad Example:

1
2
3
4
5
6
7
function processUser(userId) {
  return getUser(userId).then(user => {
    return getOrders(userId).then(orders => {
      return { user, orders };
    });
  });
}

Deeply nested code is harder to follow; async/await improves readability.

6. Sparse is better than dense.

Good Example:

1
2
3
function sum(a, b) {
  return a + b;
}

Bad Example:

1
function sum(a,b){return a+b;}

Adding spaces makes code easier to read.

7. Readability counts.

Good Example:

1
2
3
function getUserEmail(user) {
  return user?.email ?? "No email provided";
}

Bad Example:

1
function g(u){return u?.e??"No email";}

Descriptive function and variable names improve clarity.

Conclusion

By applying these principles from the Zen of Python to Node.js development, we can write cleaner, more maintainable, and more effective code. Prioritizing readability, simplicity, and explicit design decisions leads to better collaboration and more robust applications.


[alex_rocha] by alex

🇧🇷Senior Software Developer