Clean Code

Clean code is a best practice format that makes your code readable, maintainable, changeable, and modifiable. Because of that advantages, Implementing clean code can help you to improve your code quality and help your team to understand your code. So in the future, they have no problem in modifying, improving, and debugging your code. There are several points that can help us to achieve “clean” in our code.

  1. Using naming convention, we have to make sure that the name of our functions, classes, variables, etc. follows the convention of each framework or programming language. (e.g. if you use python, you can see PEP-8 or if you use java, you can see java language specification).
  2. The efficiency of comments, Before we write a comment to our code make sure that we have created a good code that is easy to understand by ourselves and team members. Because it’s better to refactor your code than create a bunch of comments to explain your code. If your code is easy to understand, you don’t need to make a comment to explain your code. But if you have to write a comment to your code because there’s some hard logic to understand, you need to write a comment as simple as possible and use the easy word so the people who read your comments can understand in no time.
  3. The effectiveness of functions, we have to implement “one function just do one thing”. if one function does multiple things, the consequence is that function prone to have side effects.
  4. Error handling, handling all of the possible outcome with try-catch or something like that. We need to cover all possible exceptions from our code. So, the error message can be more readable for programmer and users.
  5. Don’t repeat yourself, we can’t duplicate our code for doing the same thing. As I mention before that “one function just do one thing”, we can reuse the function for doing same thing or we need to use some paradigm in programming(like OOP, design pattern,etc)[1].
  6. Layout Formatting, there are some convention about how many space that we’re used for creating function inside the class, how many empty lines for creating another function, etc. This problem can be solved with linter tools. By using that tools, we’re gonna have feedback about some syntactical and stylistic errors in our source code.
  • The picture below is an example of implemention naming convention from python. Python use CamelCase for class name.
  • The picture below is an example from creating efficiency comment. I do not create a comment, because my code below was very understandable from the name of the function and the algorithm. So I don’t need to create a comment for this code. Moreover, The picture below also example of implementation from error handling. It will raise an exception if there’s no data retrieved.
  • Code below is the example from the implementation of The effectiveness of functions. As you can see, the function below only does one task.

If we’re achieving “clean” in our code, we’re gonna help ourselves and our teams in the future. So if we revisit our old codes in the future, we’ll remember how our old codes work easily (as long as our old code have implemented clean code).

Source:

[1] Azurat, Ade. 2022. Panduan Pemrograman. Depok. PPL Fasilkom UI.

--

--

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