Clean Cling Code

This article was written to help you with basic knowledge of Clean Code and as an individual review assignment of PPL CSUI 2021

the image is taken from pinterest

Clean code is essential in the software development process, especially in Agile Development Methodology. In Agile Development Methodology, the software is delivered frequently. The developer team needs to make sure the software that delivered is easy for other team members to maintain and develop so that it does not interfere with the development process.

Clean code is a term in Software Engineering that describes code that easy to understand and easy to change.

Easy to understand means the code is ready to read, whether by the author of the code or somebody else. It means the code is clear and minimizes the possibility of misunderstandings, specifically easy to understand:

  • The execution flow of the entire application
  • How the different objects collaborate with each other
  • The role and responsibility of each class
  • What each method does
  • What is the purpose of each expression and variable

Easy to change means the code is easy to reuse and refactor. This achieved if changes made by the developer do not break any existing functionality and if there any bugs, it is easy to fix them. specifically easy to change:

  • Classes and methods are small and only have a single responsibility
  • Classes have clear and concise public APIs
  • Classes and methods are predictable and work as expected
  • The code is easily testable and has unit tests (or it is easy to write the tests)
  • Tests are easy to understand and easy to change

When implementing a code, you should avoid code duplication. It means you should make a modular and well-structured program to that code. By making it, you don't need to rewrite the exact same code. Just reuse the modular code so your program looks nice and elegant.

example of DRY Principles of Navbar Components

In the example above, rather than rewrite the NavbarUser code in the code, It is better to make it into one module called NavbarUser so you only need to import and reuse it.

Comments are needed for a complex code. Rather than writing a comment to your code, try to fix the code so anyone can easily understand it. Make sure the comment you write only for hard-understood code, not because the code not well defined.

example of efficient comments of logout function

In the example above, rather than writing a comment to the handleLogout() function. It is better to fix the name that representative in the function so it is easy to understand.

Example of good naming in file creation

Good Naming is a term used to describe a condition when you naming your code that represents and consistent with the contents so that it is easy to understand what the files or functions are doing.

Make sure all the naming of variables and function follow the common standard references for the specified programing language or framework. PEP-8

When making a function, the function should be only doing one job. Avoid writing functions that could have a side-effect that could make another developer make a mistake in anticipating and understanding the behavior of the function.

Example of simple and effective function

In the example above, receiveLoginData function only has one job and it is to fetch user data. It keeps the function doing only one job and fewer wedges with other functions so it is traceable if any bugs occur.

Layout Formatting is a term to apply code formatting in your files. It makes your code follow several rules so the code is well-managed and easy to read. You can use code-formatting tools like linter(react), black(python), prettier(VSCode), and many others.

example of Layout Formatting Implementation using es-lint

In the example above, It is an implementation of es-lint in the pipeline. It will format your code automatically so you don’t need to aware of Layout Formatting anymore.

End Words

Thank you for your enthusiasm. I hope this article can enrich you with clean code.

Reference

Buku Panduan Pemrograman PPL 2021

Buku Panduan Definition of Done PPL 2021

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store