Have you ever encountered difficulties when reading or modifying code—whether it's someone else's or your own? If so, you're not alone. It's possible that the same confusion we feel when reading another person's code is also experienced by others when reading ours. That’s why it’s important for us to learn how to write code that is easy to understand—not only for ourselves, but also for others.
There are two fundamental principles to consider when writing code: readability and comprehensibility. By adhering to these principles, we ensure that our code remains clear and accessible, even when revisited or modified at a later time.
“Whenever I have to think to understand what the code is doing, I ask myself if I can refactor the code to make that understanding more immediately apparent.” Martin Fowler, Refactoring: Improving the Design of Existing Code
Best Practices for Writing Code:
1. Variable Naming
// Bad Variable Name
const account = [...,...]
// God Variable Name
const listAccount = [...,...]
Variable names should be clear and specific. For example, when naming an array variable, the name should represent a collection of items rather than a single value.
For boolean variables, use names that clearly indicate a true/false condition, usually starting with words like is, has, can, or should.
// Bad Variable Name
const validate = false
// God Variable Name
const isActive = true
2. Function Naming
Use names that clearly describe the action being performed. Create functions that have a single responsibility and a specific purpose. This way, when reading the code, we do not need to read the entire class or function implementation to understand what it does.
// Bad Function Name
function getData() {}
// God Function Name
function getCustomer() {}
In the end, fundamentals like naming variables and functions may seem trivial, but they are exactly where the foundation of code quality is built. These small habits help us maintain consistency, simplify future maintenance, and make our code easier to understand — not only for others, but also for ourselves down the line.
Code doesn’t have to be perfect, but we can always make it better every time we write it. Grow your code. Grow your craft.