3

flutter 修正你的 dart damn syntax 语法

 2 years ago
source link: https://segmentfault.com/a/1190000040958242
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

flutter 修正你的 dart damn syntax 语法

https://itnext.io/fix-your-da...

当我检查其他项目的时候,有些事情经常困扰着我,那就是我们大多数人不遵守 Dart 语法规则

我知道你可能来自另一种语言背景,但是你现在使用的是 Dart,而 Dart 做的有些不同。

实际上,Dart 文档完美地解释了一切,但是我们大多数人都懒得阅读整个文档。所以我决定为我们的懒虫们写一个总结。

希望你能从中受益!

文件夹/档案

lower_snake_caseNOT
FolderName
fileName
file-name
UpperCamelCase
lowerCamelCase
lowerCamelCase

extensions 扩展

UpperCamelCase

mixins 混合

UpperCamelCase

constants 常量

CAPITALIZE_EVERY_DAMN_LETTER // NO

lowerCamelCase // yes

enums 枚举

enum Name { ENUM, NAME } // WRONG!!

enum Name { enum, name } // RIGHT!!

对于未使用的回调参数常量名,最好使用 _ __

// IF YOU WON'T USE DON'T MENTION IT

futureOfVoid.then((unusedParameter) => print('Operation complete.'));

futureOfVoid.then((_) => print('Operation complete.'));

更喜欢使用字符串模板来组合字符串和值

// GOOD BOY
'Hello, $name! You are ${year - birth} years old.';

// BAD BOY
'Hello, ' + name + '! You are ' + (year - birth).toString() + ' y...';

避免使用不必要的 getterssetters

// GOOD
class Box {
  var contents;
}

// BAD
class Box {
  var _contents;
  get contents => _contents;
  set contents(value) {
    _contents = value;
  }
}

尽可能的写上类型定义

add(a,b) => a + b; // DAMN WRONG

int add(int a, int b) => a + b;  // HELL YEAH

BUT

final List<String> users = <String>[];  // THAT'S OVERKILL

final List<String> users = []; // GREAT
final users = <String>[]; // WONDERFUL

new 可以不要用了

// I'm old dude
new Container();

// I'm a brand new energetic open-minded sexy young dude
Container();

对不起,如果我有点咄咄逼人,但请立即修复您的代码,否则我会找到你。此外,我想如果我遇到新的沉船时间增加更多的提示,所以请小心。

谢谢你的阅读



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK