Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

The psychology of clean code: Understanding the human factor

June 3, 2023

The Psychology of Clean Code: Understanding the Human Factor

As programmers, we often strive to write clean, understandable, and maintainable code. However, what may seem clean to one person might appear convoluted to another. In this blog post, we will explore the psychological aspects of clean code and how they apply to programming in C and Python. We will delve into the human factors that influence code readability and demonstrate how to write cleaner code in both languages.

What is Clean Code?

Clean code is more than just following a set of coding conventions. It goes beyond adhering to a specific style guide and involves writing code that is easy to understand, modify, and maintain. Clean code is often attributed to Robert C. Martin, who wrote the book “Clean Code: A Handbook of Agile Software Craftsmanship.” In this book, Martin emphasizes the importance of writing code for humans, not machines.

The Human Factor in Clean Code

Psychology plays a significant role in how we perceive and understand code. Our brains are wired to process information in certain ways, and as software developers, we must take these cognitive processes into account when writing code.

Cognitive Load

Cognitive load refers to the amount of mental effort required to process information. When we write code, we should strive to minimize cognitive load for ourselves and other developers who may read our code.

Example in C

Consider the following C code snippet:

1
2
3
4
int CalculateSum(int a, int b) {
    int result = a + b;
    return result;
}

In this example, the function CalculateSum is concise and straightforward. It has a low cognitive load as the reader can quickly understand its purpose.

Example in Python

In Python, the same function would look like this:

1
2
3
def calculate_sum(a, b):
    result = a + b
    return result

Again, the Python version is clear and easy to parse, minimizing cognitive load for the reader.

Consistency and Familiarity

Humans are creatures of habit, and we tend to prefer familiarity and consistency. When writing code, we should strive to follow established patterns and conventions that are familiar to other developers.

Example in C

1
2
3
4
5
6
void main() {
    int i;
    for (i = 0; i < 5; i++) {
        printf("%d\n", i);
    }
}

In this C code example, the for loop follows a familiar pattern, making it easier for developers to understand and reason about the code.

Example in Python

1
2
3
def main():
    for i in range(5):
        print(i)

Similarly, the Python code uses familiar constructs, enhancing readability and maintaining consistency with common Python idioms.

Clarity and Explicitness

Clear and explicit code reduces the cognitive burden on developers. It allows them to comprehend the code’s intent without having to infer meaning from ambiguous or obscure syntax.

Example in C

1
2
3
int IsEven(int n) {
    return n % 2 == 0;
}

The function IsEven in C explicitly checks whether the input n is even and returns a boolean result, making its purpose clear to the reader.

Example in Python

1
2
def is_even(n):
    return n % 2 == 0

The Python version maintains the same clarity and explicitness, making it easy to understand at a glance.

Conclusion

In conclusion, the psychology of clean code hinges on understanding the cognitive processes of the human mind. By minimizing cognitive load, maintaining consistency and familiarity, and prioritizing clarity and explicitness, we can write code that is cleaner and more easily comprehensible to ourselves and our fellow developers.

Remember, clean code is not just for machines; it’s for human programmers who need to read, understand, and maintain it. By taking the human factor into account, we can strive to write code that is not only correct and efficient but also a pleasure to work with.

Happy coding!


I hope you find this blog post informative and insightful. If you need further clarification or more examples, please feel free to reach out.


➡️ Simple NTP client in Python


⬅️ Open/Close principle


Go back to Posts.