Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coding Convention #9

Open
jtjun opened this issue Feb 8, 2021 · 0 comments
Open

Coding Convention #9

jtjun opened this issue Feb 8, 2021 · 0 comments
Labels
doc Works about documentation
Milestone

Comments

@jtjun
Copy link
Member

jtjun commented Feb 8, 2021

Follow the Effective Dart's Style

PascalCase

  • class name & type
class SliderMenu { ... }

class HttpRequest { ... }

typedef Predicate<T> = bool Function(T value);
  • class used in metadata annotation with parameters
class Foo {
  const Foo([arg]);
}

@Foo(anArg)
class A { ... }

@Foo()
class B { ... }
  • extension
extension MyFancyList<T> on List<T> { ... }

extension SmartIterable<T> on Iterable<T> { ... }

camelCase

  • identifier include instance of class
var item;

HttpRequest httpRequest;

void align(bool clearItems) {
  // ...
}
  • constant
const pi = 3.14;
const defaultTimeout = 1000;
final urlScheme = RegExp('^([a-z]+):');

class Dice {
  static final numberGenerator = Random();
}
  • class used in metadata annotation with no parameters
const foo = Foo();

@foo
class C { ... }

snake_case

  • library, package, directory, source file
library peg_parser.source_scanner;

import 'file_system.dart';
import 'slider_menu.dart';
  • name import prefix (import ~ as snake_case)
import 'dart:math' as math;
import 'package:angular_components/angular_components'
    as angular_components;
import 'package:js/js.dart' as js;

Additional

  • Do use _, __, etc. for unused callback parameters.
  • Don't use a leading underscore for identifiers that aren't private.
    -> use a leading underscore for private identifiers.
  • Don't use prefix letters.
  • Do place "dart:" imports before other imports.
  • Do place "package:" import before relative imports.
import 'dart:async';
import 'dart:html';

import 'package:bar/bar.dart';
import 'package:foo/foo.dart';
  • Do specify exports in a separate section after all imports.
  • Do sort sections alphabetically.
  • Do format your code using dartfmt.
  • Avoid lines longer than 80 characters.
  • Do use curly braces for all flow control statements.
    *except: if statement with no else, in a single line.

Object Oriented

  • Do use this.attrName when using attributes.
X attr
O this.attr
@jtjun jtjun added the doc Works about documentation label Feb 8, 2021
@jtjun jtjun modified the milestones: Environment, Video & Docs Mar 8, 2021
@jtjun jtjun modified the milestones: Video & Docs, Environment Apr 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Works about documentation
Projects
None yet
Development

No branches or pull requests

1 participant