Being “pedantic” about the code you write — The flutter way.

Annsh Singh
Flutter Community
Published in
4 min readMay 26, 2020
Photo by Scott Graham on Unsplash

We as software engineers tend to write code that follow a certain architecture. We try our best to give our project a structure which makes it scalable i.e. more and more set of features can be added without much adjustment. This is done for the betterment of the whole application and the team involved.

But what about the minute details we miss out on? Like for example, what if someone in the team writes a conditional code where the “else” is empty. What if someone writes a code where they are unnecessarily using keywords like “this”, “new” or “const”. Will this affect the overall functionality of the module? No. Will this affect the scalability factor of the application? No.

Then why look into these minute details, one might ask.

The main reason one might prefer something that keeps a check on minute details, a linter, is maintainability. What I mean by maintainability is how related, readable and understandable code is. Maintainability is itself a measure of the ease to modify code, higher maintainability means less time to make a change. This is generally observed when you start working on a new project or someone new joins your team. In both the situations, you want the new developer (be it you or someone else) to understand the code and the structure of the project as quickly as possible and start contributing at the earliest.

In flutter, we do have something like this available out of the box. I am talking about Dart Linter, which is built right into the Dart Analyzer. You might choose to enable/disable the lints as per your need. The way to decide this is by a lot of trial and errors and choosing the right ones after this tiring process, or having a comprehensive list of lints available with you which the developers at Google prefer in their own Dart code.

Well, I chose the second option and we will be discussing about that next.

Pedantic.

As per the dictionary, pedantic is someone who is excessively concerned with minor details or rules and the name justifies the use case of this Package.

Enough said. Let’s see how you can add these lint rules to your Flutter project.

Step 1.

Add pedantic: 1.9.0 to your dev_dependencies inside your pubspec.yaml file.

If you add this, you might face the error of version conflict with the flutter_test package. No worries, just add pedantic: 1.9.0 to your dependency_overrides in the pubspec.yaml.

Why add this package as a dev dependency? Well, if you just want to enforce the lint rules and are not planning on referencing this dependency inside your code, then just add this as a dev dependency. Otherwise, you do have an option to simply add it to the dependencies.

Step 2.

Moving on, we now need a file to have all the lint rules that we want to enforce. The way you do this is by adding a file called analysis_options.yaml to the root of you project (where your pubspec.yaml file is). Now, add all the rules you want (I would suggest you add all) from here to the file.

Your lint rules will likely fail whenever a new version of package:pedantic is released. To avoid this, specify a specific version of analysis_options.yaml instead

include: package:pedantic/analysis_options.1.9.0.yaml

This is to be added on top of your analysis_options.yaml file.

Step 3.

Now comes the part where in you will run the static analysis of your code and all the rules that you have mentioned in the file will be enforced.

Now, there might be a situation wherein your team does not agree with a certain lint rule and would like to disable it. Well, that can easily be done by adding : false in front of the dependency. For example:

omit_local_variable_types: false

This link will help you decide which lint rule you and your team are comfortable with. This gives a detailed info about the lint rules available that too with an easy to understand example.

After this, simply run the command flutter analyze and you will be shown all the rules that are being violated. It will give you the rule name and the path of the file as well.

How many claps does this article deserve?

If you find this article useful, please click the 👏 button and share to help others find it! Feel free to clap many times (10💥, 20💥 or maybe 50💥 ?) It fuels my focus to write more of it.

Connect with me on LinkedIn or say hi on Twitter, mentioning this article. You can drop an e-mail at annsh29@gmail.com as well.

--

--

Annsh Singh
Flutter Community

Mobile Application Developer 📱 Android 💚 | Flutter 💙 Design | Create | Build stuff ⚒️ https://play.google.com/store/apps/dev?id=4716299969505523086