Writing Clean Code By albro

When we talk about clean code, we mean the same stress points, but when we talk about clean architecture, we mean how to structure your projects, "where" to write the code and how to "structure" it (for example: do you whether to use dependency injection or not and such examples).
Therefore, clean code and clean architecture are two separate issues. Clean architecture is highly dependent on your programming paradigm (eg, do you use object-oriented programming?) and your programming language. Our stress in clean code is on a specific problem, such as a specific function, but our stress in clean architecture is on the integrity and overall structure of the project (modeling and data distribution).
For example, how to access the database is a concept that is raised in the discussion of project architecture. It depends on what kind of API you have designed. Are you using GraphQL or will you be using a REST API? Are you using NoSQL databases like MongoDB or will you be using SQL databases like MySQL? Do you use object oriented programming paradigm or do you go for functional oriented programming? What will be the basis of the folder and structure of your project? The answer to each of these questions causes other questions and ultimately leads to the architecture of your project, so we cannot relate such topics to writing clean and readable code.
What is clean code?
At the very beginning, I have to talk about Clean Code. What do you think is clean code? Let's say you wrote some code and now you give it to me or another programmer. Can this programmer figure out your code without too much trouble? We call a clean code that is easy for humans to understand and does not use strange tricks to write it. Naturally, clean code is a big spectrum; It means that a code may be clean, but it can still be made cleaner.
Let me give you an example. Pay attention to the Python code below:
def create(m, n):
if m == 'Max':
return lambda v: v < n
elif m == 'Min':
return lambda v: v > n
max = create('Max', 31)
print(max(15))
print(max(32))
Do you think this code is clean? To answer this question, take a look at the code above and see if you can figure it out. My assumption is that you are a programmer but you don't know Python. Do you notice the code above? In this code, we have a function called create, which itself creates other functions. Functions that create another function are called factory function or higher order function.
This function initially creates a specific value based on whether we have chosen max or min (max variable) and then we can compare other values based on it. In the above example I have made the number 31 and then compared the numbers 15 and 32 with it. We passed Max to the create function, so the numbers 15 and 32 must be less than the original number (31), in which case we get True, but if not, we get False.
You probably also guess that this code is not clean at all. Why? Because this code will take several minutes to understand (whether you know Python or not) and you may have to run it to understand what is going on. If you need to think for more than a minute to understand a code, like the code above, it means that the code is not clean. As a programmer, you should not focus all your efforts on code efficiency. The code above works without errors and has no problems in terms of performance, but it has many problems in terms of readability. One of your main tasks as a programmer is to read code, so writing clean code is a necessity for everyone. Let's convert the above code into a clean code:
def create_validator(mode, number):
if mode == 'Max':
return lambda value: value < number
elif mode == 'Min':
return lambda value: value > number
is_below_max = create_validator('Max', 31)
print(is_below_max(15))
print(is_below_max(32))
Now that we have written the names of variables and arguments descriptively, it is much easier to understand this code. Anyone can quickly understand how this code works. Here we have the number 31 which is the maximum and the other numbers must be less than that. Did you catch what I said at the beginning of this post? Writing clean code depends a lot on your knowledge and that's exactly what I meant. One of the factors of writing clean code is writing descriptive code; This means that all arguments, parameters and variables must have full names.
Of course, we could write the above code in another way. For example, let's use classes:
class ValidatableNumber:
def __init__(self, number):
self.number = number
def is_bigger_than(self, other_number):
return other_number < self.number
def is_smaller_than(self, other_number):
return other_number > self.number
input_number = ValidatableNumber(31)
print(input_number.is_bigger_than(15))
print(input_number.is_smaller_than(32))
As you can see, this code is also a clean code and maybe for many people it is cleaner than the first method. The important thing here is that there are always different solutions to achieve clean code, and it's not like we have only one correct answer to such a question. So if I were to summarize the clean code, I would mention the following points:
- Clean code is code that is readable and meaningful (readability).
- Clean code reduces the cognitive load on your mind. It means that we don't need to think too much and put pressure on our brain to understand it.
- Clean code goes straight to the point and solves the problem.
- Clean code does not have strange and cryptic naming (such as m and v).
- Clean code doesn't have too many nested blocks.
- Clean code does not have excessively large blocks.
- Clean code follows common patterns and conventions among programmers (best practices).
- Writing and reading the code is clean, simple and attractive, and people will be attracted to the code by looking at it.
To write clean code, you need to look at code as a story or an essay, as if someone were to read a storybook written by you. This is your goal!
Specify data types in different languages
Languages like JavaScript are weakly typed, that is, the type of data is determined dynamically by the interpreter, but we can change the variables to a completely separate type. Languages like Python are also strongly typed and dynamically typed. For example, in the code that we saw above, I have not specified the data type, but a language like TypeScript (which is basically an extension for JavaScript and is not a separate programming language) is a strongly typed language, which means that we can Specify the data type explicitly. I have rewritten the example of the previous section for you in TypeScript language:
function createValidator(mode: 'Max' | 'Min', number: number) {
if (mode == 'Max') {
return (value: number) => value < number;
} else if (mode == 'Min') {
return (value: number) => value > number;
}
}
const isBelowMax = createValidator('Max', 31)
console.log(isBelowMax(15))
console.log(isBelowMax(32))
As you can see, in addition to choosing the name of the parameters, I have also specified their data type. For example, in number: number, the first part is the name of the parameter, and the part that comes after the colon (:) is the type of that parameter (number means that this parameter must be of numerical type). This issue is usually to prevent possible errors in the program, but it may also help clean coding to some extent because it specifies the type of data for us. I wanted to mention this as well so you don't get confused.










Comments