Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Clean code guidelines for intermediate developers: A roadmap

May 13, 2023

Clean Code Guidelines for Intermediate Developers: A Roadmap

As an intermediate developer, it’s important to understand the significance of writing clean, maintainable code. Clean code not only makes your work easier to understand and maintain, but it also contributes to the overall success of the project. In this post, we will discuss some clean code guidelines and provide comprehensive examples in C and Python to illustrate the concepts in detail.

1. Meaningful Variable and Function Names

One of the fundamental aspects of clean code is the use of meaningful names for variables and functions. The names should reflect the purpose and usage of the entity they represent.

C Example:

1
2
3
4
5
// Bad:
int x = 10; // What does 'x' represent?

// Good:
int numberOfStudents = 10; // Clearly indicates the purpose of the variable

Python Example:

1
2
3
4
5
# Bad:
a = 5  # What does 'a' signify?

# Good:
num_of_apples = 5  # Clearly indicates the purpose of the variable

2. Consistent Formatting and Indentation

Consistent formatting and indentation are crucial for code readability. It’s essential to follow a consistent style throughout your codebase.

C Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Bad:
void incorrectFormatting() {
int x=5;
printf("Incorrect formatting");
}

// Good:
void correctFormatting() {
    int x = 5;
    printf("Correct formatting");
}

Python Example:

1
2
3
4
5
6
7
# Bad:
def incorrect_formatting():
print("Incorrect formatting")

# Good:
def correct_formatting():
    print("Correct formatting")

3. Function Length and Single Responsibility Principle

Functions should be concise and adhere to the Single Responsibility Principle - a function should do one thing and do it well.

C Example:

1
2
3
4
5
6
7
8
9
// Bad:
void complexFunction() {
    // 100 lines of code performing different tasks
}

// Good:
void simpleFunction() {
    // 10 lines of code performing a single task
}

Python Example:

1
2
3
4
5
6
7
# Bad:
def complex_function():
    # 100 lines of code performing different tasks

# Good:
def simple_function():
    # 10 lines of code performing a single task

4. Avoiding Magic Numbers and Constants

Avoid using magic numbers and instead use constants or named variables to improve code readability and maintainability.

C Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Bad:
float calculateArea(float radius) {
    return 3.14 * radius * radius; // What is 3.14?

}

// Good:
#define PI 3.14
float calculateArea(float radius) {
    return PI * radius * radius; // Clear usage of constant
}

Python Example:

1
2
3
4
5
6
7
8
# Bad:
def calculate_area(radius):
    return 3.14 * radius * radius  # What is 3.14?

# Good:
PI = 3.14
def calculate_area(radius):
    return PI * radius * radius  # Clear usage of constant

Conclusion

In this post, we have discussed some clean code guidelines for intermediate developers, along with comprehensive examples in C and Python. Adhering to these guidelines will not only improve the readability and maintainability of your code but also set you on the path to becoming a proficient and efficient developer.

Remember, clean code is not just a matter of personal preference; it’s an essential practice for every software developer striving for excellence. Keep practicing and refining your coding skills, and you will see the positive impact of clean code on your projects and career. Happy coding!


➡️ Observer design pattern


⬅️ Factory Method design pattern


Go back to Posts.