Clean Code for Better Life

Azzahra Abraara
Portelier
Published in
4 min readOct 8, 2020

I am currently doing a continued project. Before, the project is created and done by my seniors in my University. When I saw the source code. I am kind of understand what they were trying to do to their code, or I can say that I can understand the code quite well. I wonder why? Why can I understand? Oh. It is because the code is ✨clean✨

What is a clean code?

So, what is a clean code? From what I have learned so far, clean code is code that can be understood easily, can be read clearly, and easy to maintain. Ward Cunningham, inventor of Wiki said, “You know you are working on clean code when each routine you read turns out to be pretty much what you expected. You can call it beautiful code when the code also makes it look like the language was made for the problem”

How can you write clean code?

There are things that you have to do to make your code clean, such as:

Naming your variables, functions, methods, etc. just like the usage

You have to declare your variables and others just like the usage. For example, in my project, I want to customize my button style. Instead of naming my variable like this:

I declare my variables into this:

It is more understandable, right?

let’s move to the second one.

Creating a function

when you create a function, make sure that your function is small or only has one purpose or task. Make it simple! For example here in my project:

this function only has one task. When we call the function, it will show an alert. By creating a small function it will make the code more understandable. And do not forget the first principle! I named my function Alert, to show an alert (to know what are the function usage clearer).

Write a comment to make it clearer

For example, you have a code that is complex and hard to understand for someone who wants to see your code. You can add comments to it. Try to explain your code on your comments.

in my project, I wrote some comments so that everyone can understand it.

ok, next!

Error Handling!

we can use try and catch method to do the error handling in order to anticipate errors in our program. Furthermore, try to return Exceptions in your function to handle errors.

for example here in my project, I tried to retrieve data from backend and database of riwayat/buyer. If the database is not found, we can catch the error and store the error to the console log so that we know what kind of error is that or where is the error.

Don’t Repeat Yourself (DRY)

Do not write codes that are ambiguous or has the same purpose because it is redundant. Try to combine that ambiguous codes.

for example here in my project, instead I coded repeated code, I made that into a class and defined of each paragraph’s style with the class

Layout Formatting

Every experienced your code is difficult to recognize a typo on your code? Use layout formatting! Or you can use a linter tool on your IDE.

for example in my project, using eslint or linter will help you to recognize unused variables and others!

To recap briefly, clean code is important for us, programmers to make our code pretty or aesthetically pleasing, easy to understand, and more! That is all for me. I hope this article helps you to know more about clean code.

Thank you for reading! xo

--

--