Code style refers to the way we write and organize our code so that it is easy to read, understand, and maintain. Writing code is not only about making it work, it is about making it clear for other developers and for your future self.
Readable code reduces confusion. Maintainable code makes it easier to fix bugs, add new features, and improve performance without breaking existing functionality.
Good code style is a habit that professional developers follow consistently.
One of the most important parts of readable code is choosing meaningful names. Variable names, function names, and class names should clearly describe their purpose.
Instead of using short or unclear names like x, data, or temp, use descriptive names like userAge, totalPrice, or isLoggedIn.
Good naming reduces the need for comments because the code explains itself.
Readable naming:
Example:
Poor:
let d = 10;
Better:
let numberOfStudents = 10;
Readable code is simple code. Avoid writing overly complex logic in one line. Break large problems into smaller steps.
If a function becomes too long or does too many things, split it into smaller functions. Each function should ideally do one task.
Simple code:
Example: Instead of writing one large function that validates, calculates, and prints data, divide it into smaller focused functions.
Consistent formatting makes code visually clear. Proper indentation and spacing allow others to quickly understand the structure of your code.
Good formatting includes:
Using tools like Prettier or ESLint helps maintain consistent formatting automatically.
Too many nested if statements or loops make code difficult to read. Deep nesting increases complexity and makes debugging harder.
Instead of nesting multiple conditions inside each other, try to return early or simplify the logic.
Cleaner structure improves readability and reduces mental effort when reading code.
Magic numbers are numbers written directly in code without explanation. They make the code harder to understand.
Instead of writing:
if (age > 18)
Define a constant:
const LEGAL_AGE = 18;
if (age > LEGAL_AGE)
Using constants makes your code self-explanatory and easier to update.
Good code explains itself through clear structure and naming. Comments should only be added when necessary, for example, when explaining complex logic or important decisions.
Avoid writing comments that simply repeat what the code already clearly says. Readable code is better than heavily commented confusing code.
Consistent naming improves professionalism and readability.
In JavaScript:
totalPrice)calculateTotal)UserAccount)MAX_USERS)Consistency helps teams work smoothly together.
Computers can understand messy code. Humans cannot.
Always ask:
Readable and maintainable code saves time in the long term.
Writing readable and maintainable code means:
Clean code is not about being clever. It is about being clear.
The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0

Found a mistake or have a suggestion? Let us know in the feedback form.