Clean Coding: Correct Naming By albro

If you remember, I explained in the first post that clean code has special features.
- The first point is naming. Naming variables, parameters, functions, classes and the like must follow certain rules to be readable.
- The second point is the general structure of the code and commenting. We should use code formatting and learn to write good and bad comments.
- The third point is functions. The length of the functions (the number of lines of code inside the function) and the number of parameters received by the functions are very important points in this field.
- The fourth point is conditional commands and error management. From the topics of this section, we can talk about the nesting of conditional commands and the lack of error management, which lower the quality of our code.
- The fifth point is to work with classes and data structures. In this section, we will learn about writing minimal classes and their differences with data structures.
Today I will start with the first point, naming. Naming itself includes three important parts:
- Naming variables, constants and properties
- Naming functions and methods
- Naming classes
Why is naming important in coding?
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:
- If you are learning to code, you usually need to share your code with others so that they can correct your mistakes for you.
- If you are looking for a job, you should know that most programmers work together in different companies and we don't have time to waste and read the details of each other's codes.
- If you want to work as a freelancer, you should know that a project just starts after it is finished! In the future, the project owner, who received the project from you, may want to make changes to it (updates, etc.) and by writing bad codes, you make things unnecessarily difficult for others.
- If you want to code only for your character projects, you should know that projects constantly need to be edited and updated. If you leave a project for 6 months and come back to it, you won't remember anything, and that's where bad naming can cause you a lot of trouble. In this case, you are "another person" reading your codes!
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.
Naming rules in programming
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 and constants and properties
- Functions and methods
- classes
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 rules in naming
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:
- snake_case: In this method, all letters are written in lowercase and words are separated using underscores (
_). Python developers usually use this method (example:user_dataandfirst_user_name). - camelCase: In this method, all words are joined together and the first letter of all words is capitalized except for the first letter of the first word. Developers of languages like Java and JavaScript use this method (example:
userDataandfirstUserName). - PascalCase: In this method, all words are concatenated, so it is exactly like camelCase, but the difference is that the first letter is always capitalized (example:
UserDataandFirstUserName). 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. - kebab-case: In this method, all letters are written in lowercase letters, so it is exactly like snake_case, except that instead of the underscore (_ sign), we use a space (- sign). HTML uses this method (example: < side-drawer>).
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.










Comments