Clean Code
Jeffrey Jeffrey
Computer Science Undergraduate student at University of Indonesia
Checklist
- Tests working well ?
- Codes working well ?
- Client happy ?
Feels like something is missing. Yeps, that's right. Clean code.
What is clean code?
Clean code is cleaning your code so that it is easy to understand and easy to change. Easy to understand means that the code is easy to read, whether the reader is you or somebody else. The number of times you curse or question the author is the measurement of how clean your code is. On the other hand, easy to change means that the code is easy to extend and refactor, where one can understand and put in changes that do not break any existing functionality.
Characteristics
- Clean code should be pleasing to read.
- Clean code is focused —Each function, each class, each module exposes a single-minded attitude that remains entirely undistracted, and unpolluted, by the surrounding details.
- Clean code is taken care of. Someone has taken the time to keep it simple and orderly.
- Runs all the tests
- Contains no duplication
- Minimize the number of entities such as classes, methods, functions, and the like.
Why clean code?
As stated before, poorly written code may be faster in the short term, but in a few months, you’ll be glad you put your faith in clean code. Having clean lines make it easier to make changes. Apart from the time, having clean codes mean happy team members. There is no need for documentation to understand the code; your teammates can directly jump into it. You'll also be making good deeds by reducing the number of curse words from your team members in understanding your codes. Debugging and maintenance are also easier with clean code.
Our Team Clean Code Implementation
1.Efficient comments: Comments are usually needed to explain code that is not good. Make sure comments are only written for parts that need extra explanation and not because the program code is written unclear or not good. Here is an example of efficient comment in our project:
2. Good Naming: Make sure the naming of variables and functions follow common and standard references to the specified programming language or framework. Example: when using python, it should follow PEP-8. In our project, we agreed to use the camel case as our naming conventions, such as
3. Writing functions (methods) that are simple and effective: Each function/method only does one thing. Avoid the side-effects in making functions that can make developers later mistakenly anticipate and understand the behavior of these functions. The code below is taken from mobile, where it can be seen that only one function has one responsibility.
4. Error and Exception Handling: It is necessary to anticipate every possible exception with appropriate handling. Prepare a good error code and agreed with other team members. Below is an example of an error handling when the Input Form for the program's name is empty.
5. Don't repeat yourself: No code duplication and create a program that is modular and neatly structured. As much as possible apply the appropriate concepts/paradigms (for example Object-Oriented, higher-order-function, or design patterns. In order to prevent DRY, one of the examples we made is a table list component, so there are no redundant codes.
6. Formatting Layout: Apply the formatting code (layout) rules, apply the linter tool according to the programming language technology used. In this PPL project, we are provided with SonarQube to help us in checking our code quality.