If you remember, I explained in the first post that clean code has special features.
Today I will start with the first point, naming. Naming itself includes three important parts:
When it comes to naming in programming, there is one general rule that overrules all other rules, and that is the meaningfulness of the names you choose. Many developers think that they have chosen a great name by choosing a meaningful English word. If this meaningful English word is one of the terms of programming and the web field, then that's it! According to these people, they have chosen the best name, but choosing a meaningful name does not mean that. The meaningfulness of the chosen name means that when someone reads the name of your variable, he/she should understand to a large extent what the work of that variable is and what value it holds without looking at other codes, or for example if someone looks at the name of your function It should understand the main task of that function without needing to check the codes inside the function. For example, consider the following code:
const us = new MainEntity();
us.process();
if (login) {
//any work
}
This code is a special part of a big project. Although the above code is only a few lines and the selected names of the English words are correct, but for me and you who do not have all the codes, it isn't at all clear what is going on and what is the function of this code! Anyone who sees the above code will ask themselves questions: What is MainEntity? What does the process method do? What is the value of login and what kind of data does it accept? Is login a special function and we are checking for presence or absence of this function or is it a boolean value or is it a string? Now I will put the readable version of the above code for you:
const user = new User();
user.save();
if (isLoggedIn) {
// any process
}
This code is much easier to understand. In the first line, we have created a new user and in the second line we have saved this user (we don't have all the code, but we guess that this saving happens in the database). And finally we have isLoggedIn. From this simple name (isLoggedIn), we understand that its value must be the result of checking whether the user is logged in or not, and we are most likely dealing with a boolean.
When our naming is meaningful, someone else reading our code can easily understand the generality of the work. We didn't need to have the source code of the whole project to understand the above codes; For example, there was no need to check the User class to know exactly what was written inside it. Why should it matter to me that someone else can read my code, you might ask? This question has different answers:
You should note that we won't always agree on the naming because each person's taste is different and individual differences are undeniable. The important thing here is that your codes are meaningful and readable. I've prepared three versions of the same code for you:
const admin = new Admin();
const admin = new AdminUser();
const admin = createAdmin();
The first two lines have used classes and the third line has used a function, but all three codes above are quite clear and readable, and we can see at a glance that this code is for creating a new admin.
Before we want to get acquainted with good and bad examples of naming, we should check the rules related to them. I've divided this part into three different parts:
Variables, constants, and properties are all data containers, that is, we use them to store data, so I sometimes refer to them as data containers in these posts. The data stored in these data containers come from various sources; For example, it may be the data entered by the user, or the validation result, or an array of products from the database, and so on. So, for this category of names, you should use "nouns" or "short phrases with adjectives" to describe what is inside our data containers. For example, User is a noun or isValid (which means "valid") is an expression (of course, the use of adjectives is not mandatory). Of course, booleans like isValid are a bit more like naming functions.
To name the functions and methods, we have to adopt another method. We don't store any data specifically inside the functions and methods, but we have the code that needs to be executed. Even if you have defined a variable inside the function, that variable will be destroyed after the execution of the function and is temporary. To name this category, we must use "verb" or "short phrases with adjectives". Names like sendData (send data) or InputIsValid (input is valid) are suitable names. The important point is the presence of "verb" in these two names.
To name classes, you must be familiar with the nature of classes. Classes are a blueprint for building a particular object. For example, if we have a class for creating users, we can get a user object from it using instantiation. So, to name them, we must use "nouns" or "simple phrases with nouns". Names like User or RequestBody are examples of good names.
Naturally, you don't need to memorize these recipes, but over time you will get used to these structures and you will choose such names without having to think.
Casing refers to the use of upper and lower case letters and underscores and the like. In programming, we usually have four Casing or well-known writing methods for naming:
_). Python developers usually use this method (example: user_data and first_user_name).userData and firstUserName).UserData and FirstUserName). Programmers of languages like Python, Java, and JavaScript use this method only for class names. A language like PHP also uses this method for the names of its classes and camelCase for the names of its methods.Naturally, it is the programming language that determines which Casing method you use. You might ask, I'm a JavaScript developer, can't I use kebab-case? From a technical point of view, you can write your codes with any Casing you want, but following the conventions of any programming language is part of the way of writing clean and readable code, so I suggest you always use the famous conventions of the language or framework you are using.