Overview
This is the style guide for Autonomy for all Dart code, based off:
Effective Dart https://dart.dev/effective-dart
If you are uncertain, refer to Effective Dart documentation or asks the Autonomy leads for help.
Dart Linter
TODO: research the Dart Linter https://pub.dev/packages/lints
Files
Name packages, directories, and source files using snake_case
:
Code Block | ||
---|---|---|
| ||
my_package └─ lib └─ file_system.dart └─ slider_menu.dart |
File contents end with 1 blank line:
Code Block |
---|
Amazing dart code... // Last blank line below, then end of file |
Indentation
Idents are 2 spaces long https://www.reddit.com/r/dartlang/comments/ilhr3i/why_two_spaces_instead_of_four/. If you are using Visual Studio Code this will be the default with the Dart extension installed
Blank lines do not have spaces
Strings are encoded with UTF-8 and enclosed with single quotes to maintain consistency with the Flutter style guide https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#prefer-single-quotes-for-strings
Imports
One space between each import level
Sort each import section alphabetically
Import order:
System-level imports: Anything that starts with
import 'dart:__'
Installed packages: Anything that starts with import
'package:__'
Local packages: Anything written by/for WARG (including submodules)
Name import prefixes using snake_case
:
Code Block | ||
---|---|---|
| ||
import 'dart:math' as math; import 'package:angular_components/angular_components.dart' as angular_components; import 'package:js/js.dart' as js; import 'util.dart'; |
Identifier naming
variableNames
in lowerCamelCaseconstantNames
in lowerCamelCasefunctionNames
in lowerCamelCaseClasses, enumerated types, typedefs and type parameters should be in UpperCamelCase
Code Block |
---|
class SliderMenu {...} class HttpRequest { ... } typedef Predicate<T> = bool Function(T value); |
Variables
use
final
for local variables that are not re-assigned
Code Block | ||
---|---|---|
| ||
final int constantNumber = 6; int changingNumber = 9; |
Classes
Private members (methods and attributes) are prefixed with 1 underscore
Code Block |
---|
int _my_private_member; void _my_private_function(); |
Initialize fields at their declaration when possible
Code Block |
---|
class ProfileMark { final String name; final DateTime start = DateTime.now(); ProfileMark(this.name); ProfileMark.unnamed() : name = ''; } |
Logic
Use curly braces for all flow control statements
Exception:
if
statement with noelse
clause and whole if statement fits on one line, can omit braces
Put subsequent
if
andelse
on the same line as previous block’s closing brace
Code Block | ||
---|---|---|
| ||
if (statement) { // Something } else if (statement) { // Something } else { // Something } |
Avoid using
true
orfalse
in equality operations
Code Block |
---|
if (nonNullableBool) { // True... } if (!nonNullableBool) { // False... } |
Functions
Use
=
to separate parameters from its default value
Operators
No spaces around
:
if it's used as a slicing operator, otherwise space after:One space on either side of all binary operators, including assignment operators
Ex:
+ - * / %
,+= -= *= /=
,> < == !=
One space after every comma
Strings
Use adjacent strings to concatenate string literals
Use interpolation to combine string and values
Avoid curly braces when not necessary
Misc
Lines are 100 characters long
No spaces at the end of a line
Multiline
Commenting
Format comments like sentences
Doc comments TODO: investigate dart doc
use
///
to document members and typesDart doc can find & generate comments for it
Start doc comments with single-sentence summary
Separate first sentence from rest of description
General Example
No commented out and unreachable code
Do not add code that you have commented out. It is better to just delete it. Git and GitHub will keep a record if it’s ever needed again.
Additionally, delete unreachable code (code that will never be run). For example: