慣例:大文字で始める(UpperCamelCase)
理由:型とインスタンスを視覚的に区別しやすくするため
例:
class UserProfile { ... }
enum UserStatus { active, inactive, banned }
慣例:小文字で始め、単語の区切りはキャメルケース(lowerCamelCase)
例:
int userAge;
String userName;
bool isLoggedIn;
慣例:変数と同じく lowerCamelCase
例:
void sendMessage(String message) { ... }
String getUserName() { ... }
bool isUserActive() { ... }
慣例:通常は lowerCamelCase(Dart公式)
ただし、全て大文字+アンダースコアも見かけることがある(特に移植コード)
例:
const double piValue = 3.14159;
final String defaultUserName = 'Guest';
慣例:小文字、単語の区切りはアンダースコア(snake_case)
例:
package:my_app/models/user.dart
package:my_app/utils/file_helper.dart
慣例:小文字・アンダースコア(snake_case)
例:
user_profile.dart
network_service.dart
慣例:名前の先頭に _ を付ける
例:
class UserProfile {
String _password; // プライベートフィールド
void _calculateScore() { ... } // プライベートメソッド
}
慣例:1文字大文字(T, K, Vなど)
例:
class Box<T> {
T content;
Box(this.content);
}
Map<K, V> getMap<K, V>() { ... }
慣例:UpperCamelCase
例:
extension StringExtensions on String {
String capitalize() => this[0].toUpperCase() + substring(1);
}
慣例:UpperCamelCase
例:
@Deprecated('Use newMethod instead')
void oldMethod() { ... }
UpperCamelCase:型、クラス、enum、アノテーション、extension
lowerCamelCase:変数、関数、メソッド、定数(公式推奨)
snake_case:ファイル名、パッケージ、ディレクトリ名
_(先頭アンダースコア):プライベートメンバー
1文字大文字:ジェネリック型パラメータ