Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Sign up for free
March 25, 2021
- What is Effective Dart?
- Project Setup
- Customization & Control
March 25, 2021
More Decks by Birju Vachhani
See All by Birju Vachhani
Other Decks in Programming
See All in Programming
See All Featured
Effective Dart WRITE DART CODE THE RIGHT WAY.
HELLO! I am Birju Vachhani I am here because I
love clean code. You can ﬁnd me on Twitter at @birjuvachhani 2
AGENDA 3 ∎ What is Effective Dart? ∎ Guides ∎
Project Setup ∎ Customization & Control
WHAT IS EFFECTIVE DART? 4 A set of rules and
guidelines derived and learned from code written over the past several years.
• To Be Consistent: If two pieces of code look
different it should be because they are different in some meaningful way. • To Be Brief: If there are multiple ways to say something, you should generally pick the most concise one. 5 WHY?
EXAMPLE 6 prefer_collection_literals
GUIDES 7 • Style Guide: For laying out and organizing
code. • Documentation Guide: For doc comments and regular comments. • Usage Guide: For using language features in best way to implement behavior. • Design Guide: For designing consistent, usable APIs for libraries.
constant_identifier_names 8 STYLE GUIDE EXAMPLE OF Constants are often changed
to ﬁnal non-const variables, which would necessitate a name change.
slash_for_doc_comments 9 DOCUMENTATION GUIDE EXAMPLE OF The /// syntax is
also easier to read in some situations, such as when a doc comment contains a bulleted list that uses * to mark list items.
prefer_is_empty 10 USAGE GUIDE EXAMPLE OF When testing whether an
iterable or map is empty, prefer isNotEmpty over !isEmpty to improve code readability.
omit_local_variable_types 11 DESIGN GUIDE EXAMPLE OF Omitting the type focuses
the reader’s attention on the more important name of the variable and its initialized value.
PROJECT SETUP & CUSTOMIZATION Let’s see how we can enable
analyzer and linter to use effective_dart for our project and customize it. 12
ADD DEPENDENCY Add effective_dart under your dev_dependencies section in pubspec.yaml.
13 STEP 1
CREATE CONFIGURATION Create configuration file analysis_options.yaml in the root of
the project. 14 STEP 2
EXCLUDE FILES To avoid unrelated warnings, you can exclude files
which you don’t want analyzer to analyze. e.g. auto-generated code/files. 15 STEP 3
CUSTOMIZING LINT RULES Rules are organized into familiar rule groups:
errors, style and pub. Rules can be selectively enabled in the analyzer using analysis options or through an analysis options ﬁle. 16
ENABLE / DISABLE RULES As some lints may contradict each
other, only a subset of these will be enabled in practice. Some rules may be marked experimental to indicate that they are under review. 17 DART LINT
RULES IN ACTION If the rule is enabled, linter shows
warning when the rule is broken like this one. 18 DART LINT public_member_api_docs
CHANGING SEVERITY Customizing rules is ﬁne, but could you change
the severity of the rule? Let’s see how... 19 TAKING CONTROL
CHANGING SEVERITY Severity can be deﬁned under errors section and
can be error, warning, info or ignore. This way you’re in control of what is considered error or warning by dart analyzer. 20 TAKING CONTROL
THAT’S ALL YOU NEED TO KNOW Don’t forget to keep
an eye on Dart Analysis section in your Android Studio. It can save you a lot of trouble! 21
THANKS! Any questions? You can ﬁnd me on Twitter at
@birjuvachhani or mail me on email@example.com 22
REFERENCES ➔ Effective Dart https://dart.dev/guides/language/effective-dart ➔ Linter for Dart https://dart-lang.github.io/linter/lints/
➔ Customizing Static Analysis https://dart.dev/guides/language/analysis-options 23