Week 11 - OOP concepts & LLMs

Object oriented programming

Classes and objects

Encapsulation

Code style: clean code

LLMs

Tokenization

Inference

Tools

RAGs

Using LLMs in Code

Practice

Assignment

Back to core program

Code style: clean code

What is Code Style?

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.

1. Use Clear and Meaningful Names

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;

2. Keep Code Simple

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.

3. Use Consistent Formatting

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.

4. Avoid Deep Nesting

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.

5. Avoid Magic Numbers

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.

6. Write Self-Documenting Code

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.

7. Follow Naming Conventions

Consistent naming improves professionalism and readability.

In JavaScript:

Consistency helps teams work smoothly together.

8. Write Code for Humans First

Computers can understand messy code. Humans cannot.

Always ask:

Readable and maintainable code saves time in the long term.

Summary

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

CC BY-NC-SA 4.0 Icons

*https://hackyourfuture.net/*

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