Clean Code

This article was written as an individual review assignment for PPL CSUI 2021

For a group of programmers that are working on a project together, clear communication among them is very important, especially when it comes to communication by reading code. This is where clean code can be useful, clean code can help other programmers easily understand what your code means. There are many definitions of clean code, but many developers agree that clean code is code that is easy to understand and easy to change. A group of programmers with different backgrounds and even ones that use different languages can also be helped by the use of clean code. This is because programming with clean code encourages programmers to create functions and classes that have commonly used names and only have a single responsibility.

To make your code easier to understand by others, there are many criteria such as, your code needs to be easy to understand the execution flow of the entire application, how the different objects collaborate, the role and responsibility of each class, what each method does, and what is the purpose of each expression and variable. Your code should also be easy to change, refactor, or extend. Things that can make your code easy to change are classes and methods that are small and only have a single responsibility, classes that have clear and concise public APIs, classes and methods that are predictable and work as expected, code that is easily testable and has unit tests, and tests are easy to understand and easy to change.

Programming clean, understandable, and maintainable code is a skill that every programmer should master. To improve the quality of your code, there are principles that are very important. The first principle in clean code is how to name variables. To make your variable names good, the names you make should be meaningful, avoid disinformation, avoid noise words, searchable, pronounceable, and consistent.

Above is an example of one of the codes written my PPL project. The first variable that I named “hariTanggal”, in English means day and date implies that the type of data that I store in this variable is a combination of day and date. A bad example of this is if I named this variable “ht”, which is an abbreviation of “hariTanggal”. To the programmer that wrote this code, this variable is clearly defined, but other programmers that just read this code might not understand what this name means.

The next principle is how to write functions, a good function is small, has a single responsibility, encapsulates conditions, does not use flag arguments, does not have side effects, and follows the DRY principle. A function should only have a single responsibility because if it has too many things to do, the function will be hard to understand. If your function follows the single responsibility principle, your will probably be small. You should also follow the Don’t Repeat Yourself principle, this means that you shouldn’t have duplicated lines of the same code. Following the DRY principle avoids the need to change your code repeatedly as your same code are not in multiple places.

Above is an example from my PPL project of a function that converts a date from English to Indonesia. This function only has a single responsibility and is small.

Effective Widget Creation

When making a custom widget, sometimes it's better to create a new class because when a particular widget is used over and over again having a reusable custom widget can help avoid code repetition. For example, CustomTextButton class below.

This function called CustomTextButton is used in multiple pages from my PPL project. This widget creates a text that is pushable and enables the user to activate a certain function or in this case switch pages. This custom widget also helps to keep our UI neat and uniform.

Efficient Comments

When you have applied clean code correctly, comments will rarely be useful. The fewer comments on our code, the better the code we create. Some code like the ones I have provided above do not need comments but some codes do. Below is an example of comments in my project.

Organizing your Project

To help you explore your file smoothly and without confusion, a good form of organization is good to have. A well-organized project can also help new developers on the team understand the structure of the project. Maintaining a good hierarchy and well-named folders and files is a good way to keep your project well organized. Below is an example of project file hierarchy from my PPL project.





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What is Spagetti code?

10 reasons to choose Ruby on Rails for web development

Rising Prospects Within Open Source

Run rclone as service on Windows — C# — DIY

Careers in Cloud

Features and Benefits of Bootstrap 4

Overfitting Software Design

How to Learn to Program: Writing Code

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
Farrel Alfarabi Saleh

Farrel Alfarabi Saleh

More from Medium

ROracle installation on Linux

2-Day Linux Training

Deploy a python lambda function with CDK (Part 1) — With PythonFunction Construct

CS373 Spring 2022: Dinesh Krishnan Balakrishnan