Slide 1

Slide 1 text

Effective Dart WRITE DART CODE THE RIGHT WAY.

Slide 2

Slide 2 text

HELLO! I am Birju Vachhani I am here because I love clean code. You can find me on Twitter at @birjuvachhani 2

Slide 3

Slide 3 text

AGENDA 3 ∎ What is Effective Dart? ∎ Guides ∎ Project Setup ∎ Customization & Control

Slide 4

Slide 4 text

WHAT IS EFFECTIVE DART? 4 A set of rules and guidelines derived and learned from code written over the past several years.

Slide 5

Slide 5 text

● 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?

Slide 6

Slide 6 text

EXAMPLE 6 prefer_collection_literals

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

constant_identifier_names 8 STYLE GUIDE EXAMPLE OF Constants are often changed to final non-const variables, which would necessitate a name change.

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

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.

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

PROJECT SETUP & CUSTOMIZATION Let’s see how we can enable analyzer and linter to use effective_dart for our project and customize it. 12

Slide 13

Slide 13 text

ADD DEPENDENCY Add effective_dart under your dev_dependencies section in pubspec.yaml. 13 STEP 1

Slide 14

Slide 14 text

CREATE CONFIGURATION Create configuration file analysis_options.yaml in the root of the project. 14 STEP 2

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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 file. 16

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

CHANGING SEVERITY Customizing rules is fine, but could you change the severity of the rule? Let’s see how... 19 TAKING CONTROL

Slide 20

Slide 20 text

CHANGING SEVERITY Severity can be defined 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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

THANKS! Any questions? You can find me on Twitter at @birjuvachhani or mail me on [email protected] 22

Slide 23

Slide 23 text

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