Four clean code techniques I wish I knew years ago
Tags: tips, cleancode, beginners, programming
Published: 2020-07-23 23:35:46

---
title: Four clean code techniques I wish I knew years ago
published: true
description:
tags: tips, cleancode, beginners, programming
cover_image: https://res.cloudinary.com/dje4crtui/image/upload/v1595545878/desenvolvimento-low-code_yyihjx.jpg
---

When I read [Clean code by Robert C. Martin](https://amzn.to/30HDbGH), I realized I should have read this book years ago, probably in the first year, I start learning to program, because the book contains techniques to write code that is easy to maintain and understandable (especially by other developers) and also points out some mistakes in the code examples that I made too. However, write clean code requires practicing and I was wondering if among all that techniques there are some that are easy to apply even if you are at the beginning of your journey to becoming a software developer. So I came up with four:

### Meaningful names

The task we frequently do is name things: files, variables, methods, etc. And sometimes is hard to find a good name, to make it easier to use intention-revealing names, so when anyone read it will understand it. 

Take this variable, for example:

```js
let u;
```
Can you tell what the variable **u** stores? Probably not because the name **u** reveals nothing, what about now:
```js
let activeUsers;
```
That’s a better name because we read it and understand the intention to stores active users. 


Another example:
```js
function getUsers() {
	// … some code here
	return users.map( user => user.status === ‘active’)
}
```

When you read the function name you understand that the function return users, but when you look at the implementation you discover that the function returns only the active users, a better name would be **getActiveUsers** because it reveals the full intention of the function.

### Small functions that do one thing

How many times did you come across 100 line functions? Well, I have and some of them were written by me. Chances are that those functions are doing more than one thing, you may ask how I’ll know if a function is small enough? What I do is to give an intention-revealing name to it, and that name will tell me if the function is doing more than one thing, if so, I break the function into other functions that do different tasks, and consequently, the functions will become smaller. 

For instance, imagine a function that receives a list of users as an argument, then for each one validates its data, send an email and save it in a database, the intention-revealing name for this function would be something like:

```js
function validateAndSendEmailAndSaveUsers(users) {

}
```
Now it’s easy to know that function is doing more than one thing, next step is to break it into more single-purpose functions: 
```js
function validateUserData(user) {

}

function sendEmail(email) {

}

function saveUser(user) {

}
```

### Avoid using comments

I know, this one is controversial, there are cases where comments are necessary and there are cases where it's not. I want to focus here on the type of comments used to explain a piece of code, like this one:

```js
// check if you are eligible for the discount
function check() {

}
```

Usually, this type of comment is a sign that the code is not intelligible, so when this happens, and I did it a lot in the past, you should take the time to analyze the code and see if there is a way to improve its intelligibility. The previous example, personally, I prefer like this:

```js
function isElegibleForDiscount() {

}
```

Try to use code to express yourself instead of comments.

### Single-responsibility classes

Probably you have heard of the single responsibility principle, it’s a principle easy to understand, “a class should have one, and only one, reason to change”, therefore only one responsibility. However, for some reason, we still come across super classes with many attributes and methods. Perhaps some developers think that a system with a few large classes is easier to understand, but is the other way around. 
Take a look at the classes you wrote, identify their responsibilities, then for each one create another class to handle it, you’ll end up with a system, with more classes but smaller than the originals, easier to understand and to maintain. Seriously, have you tried to understand an 800 lines class, maybe you have succeeded but didn’t you wish that class were smaller?

I tried to keep it short, I hope those tips help you in your journey and I really recommend reading the [Clean Code book](https://amzn.to/30HDbGH) to delve deeper into the subject.