Rules / Style / boolean-prefixes

boolean-prefixes

lint/style/boolean-prefixes
recommended
Same as boolean_prefixes · Pyramid Lint

Flags boolean identifiers that lack a conventional interrogative prefix.

A boolean named visible reads as a value, whereas isVisible reads as the yes/no question it answers; a consistent prefix (is, has, can, ...) makes conditions self-documenting. Only variables and fields initialized with a boolean literal, and bool-returning methods, getters, and functions, are checked — parameters and uninitialized fields are not. A single leading underscore is stripped before matching, and @override members are exempt because they cannot rename an inherited declaration.

Options

valid_prefixes (list of strings, default: ["is", "are", "was", "were", "has", "have", "had", "can", "should", "will", "do", "does", "did"]) — accepted boolean-name prefixes. User-provided entries extend the defaults rather than replacing them.

Invalid

example.dartdart
// Violations: boolean-literal declarations and bool-returning members whose
// names lack a valid prefix.

class Flags {
  bool active = true;
  bool loading = false;
  bool _enabled = true;

  // A non-override method returning bool without a valid prefix.
  bool getStatus() {
    return true;
  }

  // A getter returning bool without a valid prefix.
  bool get empty => !active;

  void example() {
    bool complete = false;
    print(complete);
  }
}

// A top-level function returning bool without a valid prefix.
bool validate() => true;
Boolean should be named with a valid prefix.
4class Flags {
5 bool active = true;
Boolean should be named with a valid prefix.
5 bool active = true;
6 bool loading = false;
Boolean should be named with a valid prefix.
6 bool loading = false;
7 bool _enabled = true;
Boolean should be named with a valid prefix.
9 // A non-override method returning bool without a valid prefix.
10 bool getStatus() {
Boolean should be named with a valid prefix.
14 // A getter returning bool without a valid prefix.
15 bool get empty => !active;
Boolean should be named with a valid prefix.
17 void example() {
18 bool complete = false;
Boolean should be named with a valid prefix.
23// A top-level function returning bool without a valid prefix.
24bool validate() => true;

Valid

example.dartdart
// pyramid_lint only inspects variable/field declarations initialised with a
// boolean *literal*, and bool-returning methods/getters/functions. Parameters,
// uninitialised fields, and non-literal initialisers are all out of scope.

class Flags {
  bool isActive = true;
  bool hasPermission = false;
  bool canEdit = true;
  bool shouldRefresh = false;
  bool _isDisposed = false; // leading underscore stripped -> isDisposed

  // Uninitialised bool fields have no boolean literal, so they are not checked.
  final bool active;
  late bool loading;

  // A non-literal initialiser (`!kDebugMode`) is out of scope.
  bool enabled = !kDebugMode;

  Flags(this.active);

  // Parameters are never checked, even with a boolean-literal default.
  void configure(bool ready, {bool fatal = false}) {
    print(ready);
    print(fatal);
    bool isDone = true; // local with a valid prefix
    print(isDone);
  }

  bool get isEmpty => !isActive;
  bool hasValue() => isActive;
}

// An @override method inherits its name from the supertype and is exempt.
class Sub extends Flags {
  Sub() : super(false);

  @override
  bool validate() => true;
}

How to configure

Set the severity of boolean-prefixes in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "boolean-prefixes": "error"
      }
    }
  }
}