Clean your code, not your laptop

A person cleaning a Laptop. Is this clean code?

Well, if you’ve already done a software project with your friend, you should already know how clean code helps your project. Here, I will give you some tips so you can write clean and better code than you’ve previously written!

Benefits of Clean Code

Have you ever written a code, then one week later your app has a bug in the code you wrote before? If you don’t write a clean code, say you write the variable with non-meaningful name (like x, y, a, b, etc.). When you try to debug your code, you need to remember what those variables represent. But you represent the variable with no meaning at all, so you need to trace how your code run. Here, I will give you an example on my IT project on the Input Validation.

In the code above, you can see that I am trying to create a Class for an input. If you understand regex, you should see that the Form1 class is a validator for Email. But, I just use Form1 for Email, Form2 for username, etc. This is a bad example. So, the name of the class, the variables, should have meaningful names. For the code above, Form1 should be renamed EmailInput or something similar. Here is the cleaned version of code:

Okay now you know about the benefits of clean code. But how can you write a clean code?

I will give you 5 most basic principle in clean code.

  1. Use meaningful names
    Here, you should understand this step by the example I’ve given you. Just give your variable name a meaningful one, it’s not that hard. Don’t use x or y in naming any variable, please.
  2. Single Responsibility Principle
    By seeing the name, a function should only has a single responsibility. New programmers tends to write a function that do almost everything. This will make your code hard to read, making it harder also to debug. A function should be small and do only one thing.
  3. Avoid writing unnecessary comments
    In the foundation of programming course in my faculty, it is encouraged to give documentation in your code. But in reality, many students understand this mindset as to give almost everything a comment/documentation. I have seen someone write a comment for almost every row he/she created. The good example for this is if your code uses a third party API, you should give a brief explanation about the API, so another developer can understand the code you’ve written.
  4. It’s broken if it’s not tested
    Start writing your test with small unit tests to make sure the small part works correctly as you want. Then, try to test subsystems together, until you started to tes the whole system. With using TDD principle, you can easily identify sources of issues if one occurs.
  5. Write HUMAN-readable code
    Most programming language has each barrier to read, like semicolons in Java, indentation in Python, etc. So, in order to help this, you may use several extensions in your text editor to help you with that. In my case, I’m using Dart at my PPL project. Everytime I save a file, if there are no syntax error, VS Code will automatically give indentations and tidy the code so it’s easier to read.

There are many benefits when all your team implements clean code, such as having easy readable code than when a problem occurs, your team can fix easily. Also your team can create tests easily, because they know what this variables is, what this method is, etc. Also, when you ask about your code in forum such as StackOverflow, if your code is so dirty, people trying to help you won’t understand your code, hence no one answered.




A Computer Science Student at Universitas Indonesia

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