A Sprint In a Life doing Clean Code at Yuk Recycle

Azka Ali
PPL A-4 YUK RECYCLE
4 min readApr 2, 2019

Hi! My name is Azka and I need some sleep :’(

And also I implement some Clean Code while developing Flutter in Yuk Recycle.

I made this

Clean code is a practice of making your code readable, understandable, and easy to debug. It’s just my opinion really, but it’s close to what it really meant.

The term “Clean code” comes from a book which called, unsurprisingly, Clean Code: A Handbook of Agile Software Craftsmanship. It’s a book by Robert C. Martin, which I recommended for every software developer.

There are basically four principles in Clean Code. They are:

Don’t. Repeat. Yourself.

Do you ever designing a code structure while also implementing it, and then you realise that the code you wrote looks so similar from one and another? It might be that you are repeating your self.

There are a couple strategy in flutter to avoid repeating yourself, such as:

Example usage of ListView.builder
  1. Use ListView.builder to create repeatable widgets. Instead of writing multiple similar implementation of widgets, we can save the difference in a Map or a List and iterate them inside ListView. It might look complicated, but it worth the effort!
  2. Save a TextStyle in a variable, and use it for each Text you want to style. I’m 90% sure that if you had tried to code in flutter, you must have wrote a TextStyle for each Text widget you create. It just so infuriating when you need to tweak it multiple Text with the same design guideline. Try to save the style to a variable, and just see how much impact it will have in your life :)
  3. The next level of saving TextStyle in a variable, you might also use Theme. It is very powerful, but a bit hard to understand, and harder to implement. But it can save you quite some time when you need to tweaking the design.
The use of TextStyle as Variable

And also many more! Just explore Flutter’s documentation page and see how plentiful of resource it is.

Refactor The Code

Refactoring the code should be a basic skill of every software developer. By refactoring your code, not only you improve the readability of your code but also reduce its complexity.

Some strategy to Refactoring is the following:

  1. Extract to function
  2. Save computational result
  3. Implement a design pattern

Follow Naming Convention

If you don’t follow naming convention of the language you’re using, it’d be like violating the constitution of your own country. Following naming convention will improve readability of your code, because usually the variable, classes and function names had their own difference in naming convention. The reason would be the intuitiveness of your code, so people would know immediately which is which in your code. If you still don’t follow the naming convention after knowing this, then you might want to reconsider your life decision.

Be Concise

Just be. It’s that simple. A complicated function, or class, is not only hard to read, but also hard to maintain. And also hard to debug. Seriously it will only make everyone’s life hard.

A definition of concise function or class may differ from each programming language. A few things a concise functions follows:

  1. Depth of a function shall not exceeding three. Be it a for loop, if branching, or defining a widget.
  2. Each function shall not exceeds 100 lines. Well this one I learned from python, but it’d be nice if you implement this in flutter also.
  3. Each function do only one thing. And they do it right. This will make it easier to debug and to test when what your code do is just one thing.
  4. Better use pure function. A pure function which will always give the same result when given the same input is easy to test because they yields, well, predictable result.

The most important thing from a concise code is most of them are reusable. When your code is simple enough, they should be easy to handle, and performs only one thing. Hence, it should be reusable when you need it. And you’ll need it.

Those are just a few of essence you can get from Clean Code. Go read the book to see more!

--

--