// Good
raiseAlarm(
'ERROR: Parts of the spaceship are on fire. Other '
'parts are overrun by martians. Unclear which are which.');
// Bad
raiseAlarm('ERROR: Parts of the spaceship are on fire. Other ' +
'parts are overrun by martians. Unclear which are which.');
PREFER 使用插值来组合字符串和值
Linter规则:prefer_interpolation_to_compose_strings
// Good
'Hello, $name! You are ${year - birth} years old.';
// Bad
'Hello, ' + name + '! You are ' + (year - birth).toString() + ' y...';
AVOID 在不需要时使用花括号进行插值
Linter规则:unnecessary_brace_in_string_interps
// Good
'Hi, $name!'
"Wear your wildest $decade's outfit."
'Wear your wildest ${decade}s outfit.'
// Bad
'Hi, ${name}!'
"Wear your wildest ${decade}'s outfit."
// Good
if (lunchBox.isEmpty) return 'so hungry...';
if (words.isNotEmpty) return words.join(' ');
// Bad
if (lunchBox.length == 0) return 'so hungry...';
if (!words.isEmpty) return words.join(' ');
var copy1 = iterable.toList();
var copy2 = List.from(iterable);
最明显的区别是第一个更短。重要的区别是,第一个保留了原始对象的类型参数:
// Good
// Creates a List:
var iterable = [1, 2, 3];
// Prints "List":
print(iterable.toList().runtimeType);
// Bad
// Creates a List:
var iterable = [1, 2, 3];
// Prints "List":
print(List.from(iterable).runtimeType);
如果要更改类型,则调用List.from()很有用:
var numbers = [1, 2.3, 4]; // List.
numbers.removeAt(1); // Now it only contains integers.
var ints = List.from(numbers);
// Good
List singletonList(int value) {
var list = [];
list.add(value);
return list;
}
// Bad
List singletonList(int value) {
var list = []; // List.
list.add(value);
return list.cast();
}
这是在访问时转换每个元素:
// Good
void printEvens(List
这是使用List.from()
// Good
int median(List objects) {
// We happen to know the list only contains ints.
var ints = List.from(objects);
ints.sort();
return ints[ints.length ~/ 2];
}
// Bad
int median(List objects) {
// We happen to know the list only contains ints.
var ints = objects.cast();
ints.sort();
return ints[ints.length ~/ 2];
}
// Good
int _nextId;
class LazyId {
int _id;
int get id {
if (_nextId == null) _nextId = 0;
if (_id == null) _id = _nextId++;
return _id;
}
}
// Bad
int _nextId = null;
class LazyId {
int _id = null;
int get id {
if (_nextId == null) _nextId = 0;
if (_id == null) _id = _nextId++;
return _id;
}
}
// Bad
class Circle {
num radius;
num area;
num circumference;
Circle(num radius)
: radius = radius,
area = pi * radius * radius,
circumference = pi * 2.0 * radius;
}
// Bad
class Circle {
num _radius;
num get radius => _radius;
set radius(num value) {
_radius = value;
_recalculate();
}
num _area;
num get area => _area;
num _circumference;
num get circumference => _circumference;
Circle(this._radius) {
_recalculate();
}
void _recalculate() {
_area = pi * _radius * _radius;
_circumference = pi * 2.0 * _radius;
}
}
要编写、维护、调试和读取的代码实在太多了。相反,您的第一个实现应该是:
class Circle {
num radius;
Circle(this.radius);
num get area => pi * radius * radius;
num get circumference => pi * 2.0 * radius;
}
// Good
class Folder {
final String name;
final List contents = [];
Folder(this.name);
Folder.temp() : name = 'temporary';
}
// Bad
class Folder {
final String name;
final List contents;
Folder(this.name) : contents = [];
Folder.temp() : name = 'temporary'; // Oops! Forgot contents.
}
当然,如果字段依赖于构造函数参数,或者由不同的构造函数进行不同的初始化,则本指南不适用
构造函数
DO 尽可能使用初始化形式
Linter规则:prefer_initializing_formals
// Good
class Point {
num x, y;
Point(this.x, this.y);
}
// Bad
class Point {
num x, y;
Point(num x, num y) {
this.x = x;
this.y = y;
}
}
// Good
Future logValue(FutureOr value) async {
if (value is Future) {
var result = await value;
print(result);
return result;
} else {
print(value);
return value as T;
}
}
// Bad
Future logValue(FutureOr value) async {
if (value is T) {
print(value);
return value;
} else {
var result = await value;
print(result);
return result;
}
}
Mark Roberge是HubSpot的首席财务官,在招聘销售职位时使用了大量数据分析。但是科技并没有挤走直觉。
大家都知道数理学家实际上已经渗透到了各行各业。这些热衷数据的人们通过处理数据理解商业流程的各个方面,以重组弱点,增强优势。
Mark Roberge是美国HubSpot公司的首席财务官,HubSpot公司在构架集客营销现象方面出过一份力——因此他也是一位数理学家。他使用数据分析
@echo off
: host=服务器证书域名或ip,需要和部署时服务器的域名或ip一致 ou=公司名称, o=公司名称
set host=localhost
set ou=localhost
set o=localhost
set password=123456
set validity=3650
set salias=s