[关闭]
@Rookie 2022-06-22T07:08:15.000000Z 字数 7660 阅读 691

Flutter及Dart 编码规范

flutter


Flutter及Dart 编码规范

每个部分都有许多的例子说明,每个例子都会以下面五个词中的某些作为开头:

DO :表示你需要遵守的做法
DONT :表示这样的做法是非常不好的
PREFER :在多数情况下,都推荐的做法
AVOID : 在多数情况下,都应该避免的做法
CONSIDER : 需要你自己去斟酌的做法

在我看来,编码习惯都是因人而异的,并没有所谓的最佳方案。

如果你是一个人开发,当然不需要在意这些问题,但是如果你的代码需要展现给别人,或者你需要与别人协同开发,编码规范就非常有必要了。

下面,将会从官方文档中选取最基本,最典型,发生率较高的一些情况,作为规范说明。

✅表示正面做法,❌表示反面做法

样式规范

命名
DO: 类, 枚举, 类型定义, 以及泛型,都需要使用大写开头的驼峰命名法

  1. class SliderMenu { ... }
  2. class HttpRequest { ... }
  3. typedef Predicate<T> = bool Function(T value);

在使用注解时候,也应该这样

  1. class Foo {
  2. const Foo([arg]);
  3. }
  4. @Foo(anArg)
  5. class A { ... }
  6. @Foo()
  7. class B { ... }

不过为一个类的构造函数添加注解时,你可能需要创建一个小写开头的注解变量

  1. const foo = Foo();
  2. @foo
  3. class C { ... }

DO: 命名库、包、目录、dart文件都应该是小写加上下划线

  1. library peg_parser.source_scanner;
  2. import 'file_system.dart';
  3. import 'slider_menu.dart';
  1. library pegparser.SourceScanner;
  2. import 'file-system.dart';
  3. import 'SliderMenu.dart';

DO: 将引用使用as转换的名字也应该是小写下划线

  1. import 'dart:math' as math;
  2. import 'package:angular_components/angular_components'
  3. as angular_components;
  4. import 'package:js/js.dart' as js;
  1. import 'dart:math' as Math;
  2. import 'package:angular_components/angular_components'
  3. as angularComponents;
  4. import 'package:js/js.dart' as JS;

DO: 变量名、方法、参数名都应该是小写开头的驼峰命名法

  1. var item;
  2. HttpRequest httpRequest;
  3. void align(bool clearItems) {
  4. // ...
  5. }
  1. const pi = 3.14;
  2. const defaultTimeout = 1000;
  3. final urlScheme = RegExp('^([a-z]+):');
  4. class Dice {
  5. static final numberGenerator = Random();
  6. }
  1. const PI = 3.14;
  2. const DefaultTimeout = 1000;
  3. final URL_SCHEME = RegExp('^([a-z]+):');
  4. class Dice {
  5. static final NUMBER_GENERATOR = Random();
  6. }

花括号

DO: 只有一个if语句且没有else的时候,并且在一行内能够很好的展示,就可以不用花括号

  1. if (arg == null) return defaultValue;

但是如果一行内展示比较勉强的话,就需要用花括号了:

  1. if (overflowChars != other.overflowChars) {
  2. return overflowChars < other.overflowChars;
  3. }
  1. if (overflowChars != other.overflowChars)
  2. return overflowChars < other.overflowChars;

文档规范

DO: 在dart的注释中,更加推荐使用///而非//

  1. /// The number of characters in this chunk when unsplit.
  2. int get length => ...
  1. // The number of characters in this chunk when unsplit.
  2. int get length => ...

至于为什么要这样做,官方表示是由于历史原因以及他们觉得这个在某些情况下看起来更方便阅读。

DO: 文档注释应该以一句简明的话开头

  1. /// Deletes the file at [path] from the file system.
  2. void delete(String path) {
  3. ...
  4. }
  1. /// Depending on the state of the file system and the user's permissions,
  2. /// certain operations may or may not be possible. If there is no file at
  3. /// [path] or it can't be accessed, this function throws either [IOError]
  4. /// or [PermissionError], respectively. Otherwise, this deletes the file.
  5. void delete(String path) {
  6. ...
  7. }
  8. DO: 将注释的第一句与其他内容分隔开来
  1. /// Deletes the file at [path].
  2. ///
  3. /// Throws an [IOError] if the file could not be found. Throws a
  4. /// [PermissionError] if the file is present but could not be deleted.
  5. void delete(String path) {
  6. ...
  7. }
  1. /// Deletes the file at [path]. Throws an [IOError] if the file could not
  2. /// be found. Throws a [PermissionError] if the file is present but could
  3. /// not be deleted.
  4. void delete(String path) {
  5. ...
  6. }

DO: 使用方括号去声明参数、返回值以及抛出的异常

  1. /// Defines a flag with the given name and abbreviation.
  2. ///
  3. /// @param name The name of the flag.
  4. /// @param abbr The abbreviation for the flag.
  5. /// @returns The new flag.
  6. /// @throws ArgumentError If there is already an option with
  7. /// the given name or abbreviation.
  8. Flag addFlag(String name, String abbr) => ...
  1. /// Defines a flag.
  2. ///
  3. /// Throws an [ArgumentError] if there is already an option named [name] or
  4. /// there is already an option using abbreviation [abbr]. Returns the new flag.
  5. Flag addFlag(String name, String abbr) => ...

使用规范

依赖

PREFER: 推荐使用相对路径导入依赖

如果项目结构如下:

  1. my_package
  2. └─ lib
  3. ├─ src
  4. └─ utils.dart
  5. └─ api.dart

想要在 api.dart 中导入 utils.dart

  1. import 'src/utils.dart';
  1. import 'package:my_package/src/utils.dart';

赋值

DO: 使用??将null值做一个转换

在dart中 ?? 操作符表示当一个值为空时会给它赋值 ?? 后面的数据

  1. if (optionalThing?.isEnabled) {
  2. print("Have enabled thing.");
  3. }

当 optionalThing 为空的时候,上面就会有空指针异常了。

这里说明一下。 ?. 操作符相当于做了一次判空操作,只有当 optionalThing 不为空的时候才会调用 isEnabled 参数,当 optionalThing 为空的话默认返回null,用在if判断句中自然就不行了

下面是正确做法

  1. // 如果为空的时候你想返回false的话:
  2. optionalThing?.isEnabled ?? false;
  3. // 如果为空的时候你想返回ture的话:
  4. optionalThing?.isEnabled ?? true;
  1. optionalThing?.isEnabled == true;
  2. optionalThing?.isEnabled == false;

字符串

在dart中,不推荐使用 + 去连接两个字符串

DO: 使用回车键直接分隔字符串

  1. raiseAlarm(
  2. 'ERROR: Parts of the spaceship are on fire. Other '
  3. 'parts are overrun by martians. Unclear which are which.');
  1. raiseAlarm('ERROR: Parts of the spaceship are on fire. Other ' +
  2. 'parts are overrun by martians. Unclear which are which.');
  3. PREFER: 使用${}来连接字符串与变量值
  1. 'Hello, $name! You are ${year - birth} years old.';
  1. 'Hello, ' + name + '! You are ' + (year - birth).toString() + ' y...';

集合

dart中创建空的可扩展 List 有两种方法: [] 和 List();创建空的 HashMap 有三种方法: {}, Map(),和 LinkedHashMap()

如果要创建不可扩展的列表或其他一些自定义集合类型,那么务必使用构造函数。

DO: 尽可能使用简单的字面量创建集合

  1. var points = [];
  2. var addresses = {};
  1. var points = List();
  2. var addresses = Map();

当你想要指定类型的时候

  1. var points = <Point>[];
  2. var addresses = <String, Address>{};
  1. var points = List<Point>();
  2. var addresses = Map<String, Address>();

DON’T: 不要使用.lenght的方法去表示一个集合是空的

  1. if (lunchBox.isEmpty) return 'so hungry...';
  2. if (words.isNotEmpty) return words.join(' ');
  1. if (lunchBox.length == 0) return 'so hungry...';
  2. if (!words.isEmpty) return words.join(' ');
  3. CONSIDER: 考虑使用高阶方法转换序列
  4. var aquaticNames = animals
  5. .where((animal) => animal.isAquatic)
  6. .map((animal) => animal.name);

AVOID: 避免使用带有函数字面量的Iterable.forEach()

forEach()函数在JavaScript中被广泛使用,因为内置的for-in循环不能达到你通常想要的效果。在Dart中,如果要迭代序列,那么惯用的方法就是使用循环。

  1. for (var person in people) {
  2. ...
  3. }
  1. people.forEach((person) {
  2. ...
  3. });

DON’T: 不要使用 List.from() 除非你打算更改结果的类型

有两种方法去获取 Iterable,分别是List.from()和Iterable.toList()

  1. // 创建一个List<int>:
  2. var iterable = [1, 2, 3];
  3. // 输出"List<int>":
  4. print(iterable.toList().runtimeType);
  1. // 创建一个List<int>:
  2. var iterable = [1, 2, 3];
  3. // 输出"List<dynamic>":
  4. print(List.from(iterable).runtimeType);

DO: 使用 whereType()去用类型过滤一个集合

  1. var objects = [1, "a", 2, "b", 3];
  2. var ints = objects.where((e) => e is int);
  1. var objects = [1, "a", 2, "b", 3];
  2. var ints = objects.where((e) => e is int).cast<int>();
  1. var objects = [1, "a", 2, "b", 3];
  2. var ints = objects.whereType<int>();
  3. 参数
  4. DO: 使用 = 给参数设置默认值
  1. void insert(Object item, {int at = 0}) { ... }
  1. void insert(Object item, {int at: 0}) { ... }

DON’T: 不要将参数的默认值设置为 null

  1. void error([String message]) {
  2. stderr.write(message ?? '\n');
  3. }
  1. void error([String message = null]) {
  2. stderr.write(message ?? '\n');
  3. }

变量

AVOID: 避免存储可以计算的值

  1. class Circle {
  2. num _radius;
  3. num get radius => _radius;
  4. set radius(num value) {
  5. _radius = value;
  6. _recalculate();
  7. }
  8. num _area;
  9. num get area => _area;
  10. num _circumference;
  11. num get circumference => _circumference;
  12. Circle(this._radius) {
  13. _recalculate();
  14. }
  15. void _recalculate() {
  16. _area = pi * _radius * _radius;
  17. _circumference = pi * 2.0 * _radius;
  18. }
  19. }
  1. class Circle {
  2. num radius;
  3. Circle(this.radius);
  4. num get area => pi * radius * radius;
  5. num get circumference => pi * 2.0 * radius;
  6. }

成员

DON’T: 不要写没必要的getter 和 setter

  1. class Box {
  2. var contents;
  3. }
  1. class Box {
  2. var _contents;
  3. get contents => _contents;
  4. set contents(value) {
  5. _contents = value;
  6. }
  7. }

构造函数

DO: 尽可能使用简单的初始化形式

  1. class Point {
  2. num x, y;
  3. Point(num x, num y) {
  4. this.x = x;
  5. this.y = y;
  6. }
  7. }
  1. class Point {
  2. num x, y;
  3. Point(this.x, this.y);
  4. }

DON’T: 不要使用 new 来创建对象

dart中不需要new

  1. Widget build(BuildContext context) {
  2. return Row(
  3. children: [
  4. RaisedButton(
  5. child: Text('Increment'),
  6. ),
  7. Text('Click!'),
  8. ],
  9. );
  10. }
  1. Widget build(BuildContext context) {
  2. return new Row(
  3. children: [
  4. new RaisedButton(
  5. child: new Text('Increment'),
  6. ),
  7. new Text('Click!'),
  8. ],
  9. );
  10. }

DON’T: 不要使用多余的 const 修饰对象

  1. const primaryColors = [
  2. Color("red", [255, 0, 0]),
  3. Color("green", [0, 255, 0]),
  4. Color("blue", [0, 0, 255]),
  5. ];
  1. const primaryColors = const [
  2. const Color("red", const [255, 0, 0]),
  3. const Color("green", const [0, 255, 0]),
  4. const Color("blue", const [0, 0, 255]),
  5. ];

异常处理

DO: 使用 rethrow 重新抛出异常

  1. try {
  2. somethingRisky();
  3. } catch (e) {
  4. if (!canHandle(e)) throw e;
  5. handle(e);
  6. }
  1. try {
  2. somethingRisky();
  3. } catch (e) {
  4. if (!canHandle(e)) rethrow;
  5. handle(e);
  6. }

设计

AVOID: 避免为了实现流式调用而让方法返回this

  1. var buffer = StringBuffer()
  2. ..write('one')
  3. ..write('two')
  4. ..write('three');
  1. var buffer = StringBuffer()
  2. .write('one')
  3. .write('two')
  4. .write('three');

AVOID: 避免使用 FutureOr 作为返回类型

  1. Future<int> triple(FutureOr<int> value) async => (await value) * 3;
  1. FutureOr<int> triple(FutureOr<int> value) {
  2. if (value is int) return value * 3;
  3. return (value as Future<int>).then((v) => v * 3);
  4. }

AVOID: 避免将bool值直接作为输入参数

  1. new Task(true);
  2. new Task(false);
  3. new ListBox(false, true, true);
  4. new Button(false);
  1. Task.oneShot();
  2. Task.repeating();
  3. ListBox(scroll: true, showScrollbars: true);
  4. Button(ButtonState.enabled);
  5. DONT: 不要在自定义的 == operator 方法中进行判空
  1. class Person {
  2. final String name;
  3. // ···
  4. bool operator ==(other) => other is Person && name == other.name;
  5. int get hashCode => name.hashCode;
  6. }
  1. class Person {
  2. final String name;
  3. // ···
  4. bool operator ==(other) => other != null && ...
  5. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注