original

https://itnext.io/fix-your-dart-damn-syntax-b3d3474373bd

refer to

text

When I check other projects, something that often bothers me is that most of us don’t follow the Dart grammar rules

I know you may come from another language background, but you are now using Dart, and Dart does something different.

In fact, the Dart document explains everything perfectly, but most of us don't bother to read the entire document. So I decided to write a summary for our slackers.

Hope you can benefit from it!

Folder/file

lower_snake_caseNOT
FolderName
fileName
file-name

kind

UpperCamelCase

function

lowerCamelCase

variable

lowerCamelCase

extensions

UpperCamelCase

mixins

UpperCamelCase

constants

CAPITALIZE_EVERY_DAMN_LETTER // NO

lowerCamelCase // yes

enums

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

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

For unused callback parameter constant names, it is best to use _ __

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

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

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

Prefer to use string templates to combine strings and values

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

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

Avoid using unnecessary getters and setters

// GOOD
class Box {
  var contents;
}

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

Write type definitions as much as possible

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 can not be used

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

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

Sorry if I am a bit aggressive, but please fix your code immediately or I will find you. In addition, I would like to add more tips if I encounter a new shipwreck time, so please be careful.

Thanks for reading


© Cat brother


独立开发者_猫哥
669 声望129 粉丝