コーディングスタイル - 1m-llc/Flutter-KtoK GitHub Wiki
https://dart.dev/guides/language/effective-dart/style
- UpperCamelCase names capitalize the first letter of each word, including the first.
- lowerCamelCase names capitalize the first letter of each word, except the first which is always lowercase, even if it’s an acronym.
- lowercase_with_underscores use only lowercase letters, even for acronyms, and separate words with _.
例:https://dart.dev/guides/language/effective-dart/style#identifiers
OK
library peg_parser.source_scanner;
import 'file_system.dart';
import 'slider_menu.dart';
NG
library pegparser.SourceScanner;
import 'file-system.dart';
import 'SliderMenu.dart';
OK
import 'dart:math' as math;
import 'package:angular_components/angular_components'
as angular_components;
import 'package:js/js.dart' as js;
NG
import 'dart:math' as Math;
import 'package:angular_components/angular_components'
as angularComponents;
import 'package:js/js.dart' as JS;
OK
const pi = 3.14;
const defaultTimeout = 1000;
final urlScheme = RegExp('^([a-z]+):');
class Dice {
static final numberGenerator = Random();
}
NG
const PI = 3.14;
const DefaultTimeout = 1000;
final URL_SCHEME = RegExp('^([a-z]+):');
class Dice {
static final NUMBER_GENERATOR = Random();
}
OK
import 'src/error.dart';
import 'src/foo_bar.dart';
export 'src/error.dart';
NG
import 'src/error.dart';
export 'src/error.dart';
import 'src/foo_bar.dart';
OK
if (overflowChars != other.overflowChars) {
return overflowChars < other.overflowChars;
}
NG
if (overflowChars != other.overflowChars)
return overflowChars < other.overflowChars;
OK
import 'package:bar/bar.dart';
import 'package:foo/foo.dart';
import 'foo.dart';
import 'foo/foo.dart';
NG
import 'package:foo/foo.dart';
import 'package:bar/bar.dart';
import 'foo/foo.dart';
import 'foo.dart';
https://github.com/dart-lang/dart_style/wiki/Formatting-Rules
- Spaces after , and : when used in a map or named parameter.
function(a, b, named: c)
[some, list, literal]
{map: literal}
- Spaces around in, and after each ; in a loop.
for (var i = 0; i < 100; i++) { ... }
for (final item in collection) { ... }
- Space after flow-control keywords.
while (foo) { ... }
try {
// ...
} catch (e) {
// ...
}
- No space after (, [, and {, or before ), ], and }.
var numbers = <int>[1, 2, (3 + 4)];
- Space before { in function and method bodies.
getEmptyFn(a) {
return () {};
}
- Place the opening curly brace ({) on the same line as what it follows.
class Foo {
method() {
if (someCondition) {
// ...
} else {
// ...
}
}
}
- Place the . on the next line in a multi-line expression.
someVeryLongVariableName.withAVeryLongPropertyName
.aReallyLongMethodName(args);
- Format constructor initialization lists with each field on its own line.
MyClass()
: firstField = 'some value',
secondField = 'another',
thirdField = 'last' {
// ...
}
- Indent block and collection bodies two spaces.
if (condition) {
print('hi');
}
var compoundsWithLongNames = [
buckminsterfullerene,
dodecahedrane,
olympiadane
];
- Indent continued lines with at least four spaces.
someVeryLongVariableName.aReallyLongMethodName(
arg, anotherArg, wrappedToNextLine);
- 文字列はシングルクォート、ダブルクォートのどちらを使ってもよい。
- オブジェクトや関数呼び出しの引数で、末尾のカンマは省略せずに記述する。
- 引数が1つの場合は、1行で書いてもよい。引数が2つ以上のWidgetは複数行で書く。
- // と /* ... */ はどちらを使ってもよい。コメント開始 // とコメント本文の間は半角スペースひとつ開ける。
- インスタンス変数へのアクセスは、不要な場合は this を使わない。
- オブジェクト生成時のnew は省略する。const は省略しない。
- 初期化時の代入以後で変更(再代入)しない変数は、finalで宣言する(推奨)。
- ダイアログは閉じるアクションのみを行う
- そもそも基本として定義されているスタイルから逸脱するものは、個別に定義する
- 複数の箇所で使うものがあるのであれば、基本スタイルとして定義する
- 少なくとも、サイズと太さは変更してはならない