Clean Code

Clean Code

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.

No alt text provided for this image

Characteristics

  1. Clean code should be pleasing to read.
  2. 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.
  3. Clean code is taken care of. Someone has taken the time to keep it simple and orderly.
  4. Runs all the tests
  5. Contains no duplication
  6. 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:

No alt text provided for this image

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

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image
No alt text provided for this image

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.

No alt text provided for this image

References

  1. https://cvuorinen.net/2014/04/what-is-clean-code-and-why-should-you-care/
  2. https://gist.github.com/wojteklu/73c6914cc446146b8b533c0988cf8d29
  3. https://www.dhirubhai.net/pulse/clean-code-shiny-michael-halim/

要查看或添加评论,请登录

社区洞察