Clean Code: A Clean Place, Is a Safe Place.

Akbar Rafsanjhani
5 min readMar 22, 2021
Messy Room, from Google Images.

Have you ever been in a situation where you were in a hurry, need something quick, turns out you couldn’t find it? Suddenly, something flashed on your mind. “Ah, let’s try to ask mom!”

But what do you get? Your mother said, “I’ve always said to clean your room! But you have never done it.” I really understand what it feels like. However, sometimes it really happens, right?

Imagine what if your project code is not written neatly? What if your code is not readable? You will find it difficult to understand, even if other people read it. So, in this article, we are going to learn how to actually implement clean code in our project.

Messy again.., from GIPHY

What Is Clean Code?

There’s a quite correct quote about clean code:

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” — Martin Fowler.

There is no determined definition of clean code. To put it plainly, There are a few endeavors that can be kept up and sought after to conduct software craftsmanship.

Software Craftmanship, from Google Images.

Those endeavors are identified with a few angles in programming, such as naming function, error handler, class organization, etc.

For me, Clean Code is more like the art of writing codes. Because everyone who has an art-sense would definitely try in such a way to make the codes look good and easy to understand.

What Should We Do to Clean Our Code?

When we clean up our room or clean up the campground, what do you do? What drives you to clean the room? To find your needed goods easily?

In software development, we have varieties of motivations. The different motivations yield different mental thinking and different activities with different visions. There are several points for writing cleaner code.

Create Meaningful Names.

Don’ ever use undefined variables like x, y, z, or a single char to define a variable, besides being untidy, it can cause other developers confused about what each variable for, what the method does, and all other confusions.

Bad Code:

int d; // elapsed time in days.

Good Code:

int elapsedTimeInDays;

Instead of giving comments, we’d better give a meaningful name for our methods, variables, classes, etc.

Write Effective Functions/Methods.

We must have learned about how to make good functions. The best practice in making functions or methods is to keep them small. The bigger the functions/methods, the more work would be handled. Make sure functions and methods only do one thing. Naming is also considered.

Some example methods:

  • Get Elapsed Time In Days Method:
public int getElapsedTimeInDays() {
return this.elapsedTimeInDays;
}
  • Set Elapsed Time In Days Method:
public int getElapsedTimeInDays() {
return this.elapsedTimeInDays;
}

Write Only Necessary Comments.

Comments are written only for a code that needs an extra explanation. Even though many IDE provide extensions that can separate codes and comments, write comments for every line of your code is still not recommended.

Good Comments:

My Project pubspec.yaml file.

Don’t Repeat Yourself.

Code duplication should be avoided. Duplicate codes have a higher risk of compromising safety. For example, when you have some similar codes in a project, you have a double rate of risks since they are both the same. Also, it makes us harder to debug.

Examples:

  • Duplicated Codes

Every TextFormField widget needs decoration. So, instead you write the same code on each widget, try to create an inputdecoration and import it to your file

  • DRY Example:

Looks neater right?

Error and Exception Handling.

Instead of returning back the errored codes, we can use exceptions to handle errors. We can start to write our try-catch-finally statement first and define the exception messages. Don’t return and pass the null value.

Layout Formatting.

We should know the standard formatting of the used programming languages, like spacing, comments, and naming things. Different languages will be different style guides. I’m sure that many IDE now, such as VS Code, have some tools to help us in layout formatting.

  • Bad Formatting
  • Good Formatting

Test-Driven Development.

TDD, from Google Images.

TDD is a software development approach in which test cases are developed to specify and validate what the codes will do. Doing the TDD steps provides bug-free and straightforward code and prevents unnecessary things like duplication code. In TDD, a new line of code will be generated if only the automated test has failed.

  • Unit Test
  • Implementation

That’s all about the Clean Code. Cheers!🍻

Thanks For Reading!

Follow my medium for more articles.

Also, if you want to browse other content, here are my Social Media😁:

Linkedin

YouTube

Instagram

References:

--

--