What Is Clean Code
The term clean code can be a bit confusing. How can the code be clean? First, let's explain this a bit.
What is meant by the cleanliness of the code is the readability, understandability and non-repetition of the code. Although the importance of clean code is not understood at first, the importance of clean code increases as the projects and project team grow. Some of the reasons for this are that the project may be changed in the future and a new person may be involved in the project. The cleaner the code is written, the easier it is to understand and make changes when the code is read later.
Key Points
There are some key points to writing clean code. We can list them as follows.
- Names
- Variables
- Functions
- Classes
- Structure & Comments
- Code Formatting
- Good & Bad Comments
- Functions
- Length
- Parameters
- Conditionals & Error Handling
- Deep Nesting
- Missing Error Handling
- Classes & Data Structures
- Missing Distinction
- Bloated Classes
We wil discuss the naming section in this article.
Naming
Naming things such as variables, properties, functions, methods, classes correctly and in an understandable way an extremely important part of writing clean code.
Be Descriptive
The purpose of the names is simple. They should explain what is stored in a variable or property or what a function or method does. You should basically stick to this when naming in your code. Doing so may not be as it seems. It will take a lot of practice to get used to it.
Naming Rules
Variables & Properties
Variables and properties hold various data (numbers, strings, lists, boolean values, arrays, objects, etc.). So the name should imply what type of data is being held. Therefore, variables and properties should typically receive a noun as a name. For example: user
, product
, customer
, database
, transaction
etc.
Additionally, you can use a short expression with the adjective to store boolean values. For example:
isEmpty
,isValid
,isLoggedIn
,emailExists
etc.
Functions & Methods
Functions and methods can then be called to execute some code. This indicates that it will perform the functions and tasks that it will do. Therefore, functions and methods should usually take a verb as a noun. For example: login()
, createUser()
, database.insert()
, saveUser()
etc.
Additionally, you can use some functions that return boolean values with adjectives and short expressions. For example:
isValid()
,isEmpty()
etc.
If you can be more specific when naming, choose that. For example, you should choose
createUser()
instead ofcreate()
.
Classes
Class names should be descriptive of the type of class to be created. For example, if you are going to write a user class, you should name it User
, or if you have an authorization process and want to create a class accordingly, you should prefer namings such as AdminUser
, RootAdministrator
.
Avoid Generic Names
You should generally avoid generic naming in your code. Because that makes your code harder to understand. Try to be as specific as you can when naming. For example, you have a function that only saves users to the database. In other words, this function is user specific, if you want to save a product, this function will not work for you. If you save()
the name of this function, it will cause confusion in the future. If you choose the saveUser()
naming instead, your code will be clearer and less confusing.
Be Consistent
An important part of using proper names is consistency. You should be consistent in naming while writing your codes. For example, you wrote a function that retrieves users from the database and you named this function getUsers()
. Then you wrote another function that returns products from the database. To be consistent in your code, you should call this function getProducts()
instead of something like fetchProducts()
. This keeps your naming consistent and avoids confusion. Actually, it doesn't matter if you use fetch or get. You should only use your preference for one and use that naming in functions with similar functions. So you can be consistent in naming.
Writing Clean Code is very important. As the projects grow and the codes increase, the importance of clean code is better understood. There are a lot of steps to take care of to write clean code. In this article, we discussed one of them, the naming section. While writing code, you may not pay attention to the naming and it may be enough for you to understand your own code because there is no one else reading your code, but when you start working with more than one person, it is very important that your code is read and understood by others. Therefore, take care to name properly and write clean code.