Clean Code for Beginners

Tips & tricks from Uncle Bob

Nabila Ayu
3 min readApr 4, 2021
An illustration of code literally cleaned with a duster
Illustration from Lynda.com

A code is clean when it is easily understood by not just you, but also the rest of the team. You definitely don't want your hard-worked code to be deemed unreadable by the time you hand it off to your teammates, don’t you? Creating maintainable code is critical for long-living software. Don’t worry, you don’t have to be an expert to be able to understand how to do it.

In this article, I’m going to point out some tips and tricks to get you started on your journey of making cleaner codes taken from the words of Uncle Bob himself in his book Clean Code.

*Every example given will be a snippet from my current software team project using ReactJS and Python.

Meaningful Names

Names are everywhere in a code, the least we could do is be intentional about it. A variable should be able to explain its function through its name. A code reviewer also shouldn’t have to guess what each i and j means in every for loop. Take a second and make sure each time you name something, it is descriptive, searchable, and unambiguous.

Bad 🤢

const [dd, setdd] = useState(false)

Good 🤩

const [dropdown, setDropdown] = useState(false)

Tip #1: Don’t be afraid of making long names. A long descriptive name is better than a short unclear name or a long descriptive comment.

Functions

Functions should be small. A long function wouldn’t just be painful to understand, it could also give space for duplications. If you couldn’t grasp the intention of a function in under three minutes, it probably could do some refactoring.

Functions should do one thing only. Make sure your function doesn’t have any hidden intentions that could create a side effect. There are no sins for making a lot of functions as long as every one of them has different responsibilities.

Tip #2: Don’t forget to assure your functions and arguments are well named.

Bad 🤢

def test(self):
Iumk.objects.create(nama_usaha="justika")
i = Iumk.objects.get(nama_usaha="justika")
self.assertTrue(isinstance(i, Iumk))

Good 🤩

def setUp(self):
Iumk.objects.create(nama_usaha="justika")
def test_iumk(self):
justika = Iumk.objects.get(nama_usaha="justika")
self.assertTrue(isinstance(justika, Iumk))

Comments

Some comments are good and helpful, but the best one is the one you found a way not to write. Make use of comments to clarify your code or as a warning of consequences (no mumbling, please). Nonetheless, prioritize explaining your intentions through code, not comment. There are certainly cases where describing in code is hard, but don’t let it deter you from trying your best.

Understand that TODO comments are different because it serves as a reminder for the programmer, though they should be nonexistent by the time of deployment.

Bad 🤢

// a = a boolean that tells whether or not the user is logged in
// b = name of the user
const Navbar = ({a, b}) => {
...
}

Tip #3: Sometimes, all you need to do is name your variables the way you wanted to describe them.

Good 🤩

const Navbar = ({isLoggedIn, name}) => {
...
}

Last Words

Keep in mind that all of the tips above are among the general rules of clean code. You should also adjust your formatting according to your language’s conventions. For example, ReactJS has Airbnb Style Guide and Python has PEP 8. In conclusion, implementing clean code is going to save you a lot of time and effort later on, though keeping it up is a habit formed by continuously practicing the rules.

“It takes less time to do things right than to explain why you did it wrong.” — Henry Longfellow

You might be wondering why didn’t I mention testing? Check out my article on TDD (Test Driven Development) where I covered the process behind it and how clean code could come into the picture.

--

--