Skip to content

2.0 Coding Standards

Cohen Adair edited this page Nov 7, 2021 · 30 revisions

General

  • Effective Dart linter is enabled. The compiler will give you warnings when rules are violated.
  • Use Cmd + Opt + L to run dartfmt tool on selected code.
  • Collections are never null, but initialized to an empty collection.
  • Pass full entity objects to widgets that listen to entity updates. When the widget tree is (re)built, fetch the latest version or fallback on the original passed in if the latest doesn't exist (i.e. it's been deleted).

Classes

  • Use private static constants over final variables for data that is static:

    static const _tableName = "catch_report";
  • Constants declarations do not include type unless needed:

    Do:

    static const _name = "name";

    Don't:

    static const String _name = "name";
  • Like constants are named by "category" first.

    Do:

    static const _tableNameCatch = "catch";
    static const _tableNameBait = "bait";

    Don't:

    static const _catchTableName = "catch";
    static const _baitTableName = "bait";
  • Class coding structure

    class Catch {
      // Public static methods
      // Public static variables
      // Private static variables
      // Abstract methods
      // Protected instance variables
      // Private instance variables
      // Default constructor
      // Named Constructors
      // "Manager" getters, i.e. BaitManager get _baitManager
      // Other getters
      // Abstract method implementations
      // Instance method implementations
    }
    

File Structure

From the top, down:

  • Imports
  • Public typedefs
  • Public enums
  • Main class
  • State class, if applicable
  • Public helper classes
  • Private typedefs
  • Private enums
  • Private classes
  • Private extensions

Functions/Methods

  • Only use named parameters for building widgets, and methods with a lot of parameters or a lot of optional parameters. This prevents common uses like _catchManager.entity(id: catchId), where the name "id:" is redundant.

    Do

    String formatDateRange(DateRange dateRange) {
    }
    
    String formatDateRange(DateRange dateRange, [
      clock = const Clock(),
    ]) {
    }

    Don't

    String formatDateRange({
      DateRange dateRange,
    }) {
    }
  • If a parameter has a default value, the type isn't included in the declaration.

    Do

    String formatDateRange(DateRange dateRange, [
      clock = const Clock(),
    ]) {
    }

    Don't

    String formatDateRange(DateRange dateRange, [
      Clock clock = const Clock(),
    ]) {
    }
  • Always use assert(condition) to verify parameter values. Assertions, in Flutter, are to signal programming errors and are ignored in production environments.

Clone this wiki locally