Clean Code in Flutter With Lint

Astrida Nayla
6 min readApr 5, 2021

--

Illustration on Clean Code (Source)

When I first heard of clean code, what comes to mind is a clear and structured program. Well, that’s partially correct, but there’s more to clean code than I thought. Clean code is an essential part of programming. Now it’s starting to make sense why my college professors highly encourage clean code on my programming assignments. Awesome!

So clean code, what does that even mean? Well, clean code is a practice to develop a program with high readability. By increasing our readability, hopefully, our program will be easier to understand, change, and maintain.

Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control.

- Grady Booch, author of Object-Oriented Analysis and Design with Applications

How to manifest clean code in our program?

As said before, clean code is an essential part of programming. A programmer should strive for good code, not just functionally but also structurally. What makes a good code? Below are some criteria I consider to be fitting:

  • A clear and consistent naming format for variable, class, and method names.
  • Class and method only have a single responsibility.
  • Clear and well-defined tests that are easy to understand and debug.
  • The role and responsibility of classes and methods are easily understood.
  • The execution flow of the entire application is easy to follow.

How clean code benefit my team

There are a lot of benefits that programmers could achieve by practicing clean code. Sure, one may think that it requires extra effort and not as necessary as the program’s functionality. But, when you think of all the benefits you could get from clean code, you’ll realize why it’s worth all the effort. Below are some benefits that I felt as I implemented clean code on my project.

Easier to Understand

With clean code, our program will be easier to understand. Because, variables, methods, and classes are named according to their purpose. With clean code, classes and variables have a single responsibility. Programs are also structured properly with as few as possible code duplications. Combine all of them and programmers will not have a hard time guessing what a piece of the program does.

High Maintainability

As a programmer, we want our program to be highly maintainable in the long term. In order to do that, our program should be easily understood by other programmers. The purpose of clean code is to make our program readable and easy to understand. So, clean code helps our program to be easily maintained even by other people.

Easier to Test and Debug

Part of clean code is to build clear and well-defined tests. Building a clear and well-defined test could help us debug errors better, thus saving time and effort. Messy and convoluted codes are difficult to test too, so it is better to perform clean code in order to test your code better. Better tests help improve the quality of our program.

Clean code for Flutter frontend programming

There are various ways to implement clean code on Flutter frontend programming. Various open-source packages are also available to help Flutter developers practice clean code on their program. Here, I will discuss clean code on flutter using Lint.

Lint setup

To get started, simply add lint as a dependency to your pubspec.yaml file

dev_dependencies:   lint: ^1.0.0

Now that we have lint set, it’s time to set the lint rules. You can add your custom rules in a file analysis_options.yaml that is located in the same directory as the pubspec.yaml file.

Directory location for analysis_options.yaml

There are three types of rules in lint:

  • Error rules: rules related to the common errors that can occur in our program.
  • Style rules: rules related to styles that are referred to in the Dart Style Guide.
  • Pub: rules related to pub.

You can apply your custom lint rules by adding ‘linter:’ to the analysis-options file. Then add ‘rules:’ followed by the specific rules you want to apply. You can check specific rules and their function in the lint documentation here. Below is an example of my analysis-options file:

My personalized lint rules in analysis-options.yaml

Example of lint implementation

Below are some implementation examples based on the rules in my analysis-options file.

[STYLE] Sort constructor declarators before other members and make static attributes of an immutable class final.

Bad implementation (left) and good implementation (right)

[STYLE] Clear and consistent naming for methods, classes, and variables with camel-case.

Bad implementation (left) and good implementation (right)

[STYLE] Lines are no longer than 80 characters.

Bad implementation (left) and good implementation (right)

[ERROR] Always use package import.

Bad implementation (left) and good implementation (right)

[PUB] Sort pub dependency.

Bad implementation (left) and good implementation (right)

Other non-lint clean code implementations

Clear and well-defined tests that are easy to understand and debug.

On my functional test for the frontend, I made sure that tests are named according to what it does. This will make it easier to debug should the test fails. I also ensure that each functional test does one specific responsibility only to ensure that it is readable by other programmers.

A piece of unit test from my program

Single responsibility.

Single responsibility means that a module, class, or function should only have one responsibility. This allows clean code by increasing the code readability, thus making it easier for other programmers to understand what the module, class, or function does.

Some methods in my program that only implement a single purpose

Conclusion: Is It Necessary?

The answer depends on the programmer themselves. Are you willing to take the risks that may occur should clean code be absent in your project? Are you confident that others can easily understand your code as it is? It all depends on the purpose of implementing clean code in the first place. Therefore, clean code is necessary if we want our program to have high readability, maintainability, and easier to test and debug. But, if the code will only be used by you and you are satisfied with the code as it is, then there is no need for implementing clean code.

After learning about clean code implementation on Flutter, I realized that it is actually very easy to implement and does not take as much effort as it seems. I find that clean code is very useful when I’m working in teams where I’m not the only one who will use the code. Hopefully, this article can introduce you to clean code and provide insights on when to use clean code. Thank you and I’ll see you at the next one!

--

--

Astrida Nayla
Astrida Nayla

Written by Astrida Nayla

アストリッド / product designer, passionate in design & sustainability

No responses yet