DEV Community

Cover image for The Reason Why You Have To Write Clean Code
Aditya Rohman
Aditya Rohman

Posted on • Updated on

The Reason Why You Have To Write Clean Code

Hi there, today I want to talk about clean code. So, what is exactly is clean code? Well, coding is not just an activity of typing in the editors, is not just typing variables or functions. But, coding is more than that. Coding is a tool and the programmer is a problem solver.

Thinking about this case. You are a mobile developer and have an application that used to solve the specific problem on your campus. You've built that app for 3 months and now it's release on the Google Play Store and App Store. This application certainly needs maintenance and updates. Moreover, the application will be more complex than the previous version and you are working with the team.

Writing code with the concept of clean code is important in this case. With clean code, your application will be easy to maintain and easy to add new features in the next update. One more benefit is your workflow will be structured with your team.

Below, I list some tips for clean code for you. I use Dart language for the example

Clean Code Tips in Dart

Write a readable variable

When you declare a variable for your program, you must be given the name of the variable. Your variable name must be readable and match its functionality in the program. Let's see this example!

DO

var firstName = "Aditya"
var lastName = "Rohman"
var age = 20
Enter fullscreen mode Exit fullscreen mode

DON'T

var x = "London"
var a = 100
Enter fullscreen mode Exit fullscreen mode

In the code snippet above, it's clear that the firstName variable is used to store a person's first name. But, when you name the variable with no descriptive name, you will be confused later on. So, give the descriptive and readable name to your variables.

Choose your code convention-style

The second tip is you must choose one of the code conventions based on the language you work with. In this example, I use Dart. So, I will choose either the lower camel case (firstName) or the snake case (first_name). You must be consistent with using it in all your code.

Alt Text

Use comments

Comment is one of the powerful tools for developers. You can write an explanation about a function or class in your program. So, you will be understood if you want to give an update later on. If you're using Visual Studio Code, I recommended you to use a plugin called Better Comments. With this plugin, you can specify the special comments. For example, you can give a warning comments, TODO comments, and more.

Alt Text

Keep on Refactoring

The last tip is to keep refactoring your code when you adding a new feature or modify your code. Clean all the warnings and errors, and don't forget to give comments. Use the refactoring feature on your IDE of code editors. For example, here I use the VS Code plugin called Prettier - Code Formatter. This plugin will reformat your code when you hit save.

Alt Text

Okay, so that's the 4 tips for implementing Clean Code in your projects. I hope this article helpful. Thank you for reading this article. Let's discuss in the comment section down below for further talks! Thank you!

Top comments (2)

Collapse
 
programmerbyday profile image
Arman @programmerByDay

Hi Aditya, nice post. thanks.

I think this post can help as well: How a Newly Joined Developer Affects How You Write Code

Collapse
 
codestronaut profile image
Aditya Rohman

Thank you