snippetjavascriptTip
The art of writing great code
Viewed 0 times
javascriptcodetheartwritinggreat
Problem
Writing great code is both an art and a discipline. Over the years, I've learned that the best code isn't just code that works, but code that's easy to read, maintain, and test. In this article, I'll share some practical tips and examples to help you write code that stands the test of time, focusing on testability, maintainability, and readability.
@Suggested reading
State is at the heart of many bugs and headaches. Poor state management considerations can often lead to unexpected results and make your code hard to reason about, maintain and test.
Mutable state is one of, if not the most, common source of bugs. Try to avoid it whenever possible. When you mutate an object or array, you change its state, which can lead to unexpected results elsewhere in your code.
This example's function mutates the original object, which can lead to side effects, affecting other parts of your code. It's a whole lot easier to reason about code that doesn't mutate state. Instead, prefer passing state as parameters and returning new values. Then, you can return a new object with the updated state. Changes such as these makes your function pure and predictable.
@Suggested reading
State is at the heart of many bugs and headaches. Poor state management considerations can often lead to unexpected results and make your code hard to reason about, maintain and test.
Mutable state is one of, if not the most, common source of bugs. Try to avoid it whenever possible. When you mutate an object or array, you change its state, which can lead to unexpected results elsewhere in your code.
This example's function mutates the original object, which can lead to side effects, affecting other parts of your code. It's a whole lot easier to reason about code that doesn't mutate state. Instead, prefer passing state as parameters and returning new values. Then, you can return a new object with the updated state. Changes such as these makes your function pure and predictable.
Solution
let user = { name: 'Alice', age: 25 };
const birthday = user => {
user.age++;
};State is at the heart of many bugs and headaches. Poor state management considerations can often lead to unexpected results and make your code hard to reason about, maintain and test.
Mutable state is one of, if not the most, common source of bugs. Try to avoid it whenever possible. When you mutate an object or array, you change its state, which can lead to unexpected results elsewhere in your code.
This example's function mutates the original object, which can lead to side effects, affecting other parts of your code. It's a whole lot easier to reason about code that doesn't mutate state. Instead, prefer passing state as parameters and returning new values. Then, you can return a new object with the updated state. Changes such as these makes your function pure and predictable.
> [!NOTE]
>
> Object immutability is pretty hard in JavaScript and, this example only scratches the surface. There's plenty of relevant content, if you feel like diving deeper into the topic.
Code Snippets
let user = { name: 'Alice', age: 25 };
const birthday = user => {
user.age++;
};const birthday = user => { ...user, age: user.age + 1 };const processOrder = order => {
if (order.paid) {
if (order.items.length > 0) {
for (let item of items) {
if (item.active) {
if (item.value > 10) {
// ...
}
}
}
}
}
};Context
From 30-seconds-of-code: writing-great-code
Revisions (0)
No revisions yet.