Alan Hou的个人博客

概览

Over the past several years, we’ve written a ton of Dart code and learned a lot about what works well and what doesn’t. We’re sharing this with you so you can write consistent, robust, fast code too. There are two overarching themes:

  1. 保持连贯。 When it comes to things like formatting, and casing, arguments about which is better are subjective and impossible to resolve. What we do know is that being consistent is objectively helpful.If two pieces of code look different it should be because they are different in some meaningful way. When a bit of code stands out and catches your eye, it should do so for a useful reason.
  2. 保持简洁。 Dart was designed to be familiar, so it inherits many of the same statements and expressions as C, Java, JavaScript and other languages. But we created Dart because there is a lot of room to improve on what those languages offer. We added a bunch of features, from string interpolation to initializing formals, to help you express your intent more simply and easily.If there are multiple ways to say something, you should generally pick the most concise one. This is not to say you should code golf yourself into cramming a whole program into a single line. The goal is code that is economical, not dense.

The Dart analyzer has a linter to help you write good, consistent code. If a linter rule exists that can help you follow a guideline, then the guideline links to that rule. Here’s an example:

Linter rule: prefer_collection_literals

For help on enabling linter rules, see the documentation for customizing static analysis.

指南

We split the guidelines into a few separate pages for easy digestion:

For links to all the guidelines, see the summary.

如何阅读指南

Each guide is broken into a few sections. Sections contain a list of guidelines. Each guideline starts with one of these words:

Some guidelines describe an exception where the rule does not apply. When listed, the exceptions may not be exhaustive—you might still need to use your judgement on other cases.

This sounds like the police are going to beat down your door if you don’t have your laces tied correctly. Things aren’t that bad. Most of the guidelines here are common sense and we’re all reasonable people. The goal, as always, is nice, readable and maintainable code.

Glossary

To keep the guidelines brief, we use a few shorthand terms to refer to different Dart constructs.

Summary of all rules

Usage

Libraries

Booleans

Strings

Collections

Functions

Parameters

Variables

Members

Constructors

Error handling

Asynchrony

Design

Names

Libraries

Classes and mixins

Constructors

Members

Types

Parameters

Equality

退出移动版