Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Latest commit

 

History

History
37 lines (25 loc) · 895 Bytes

avoid-global-state.mdx

File metadata and controls

37 lines (25 loc) · 895 Bytes

import RuleDetails from '@site/src/components/RuleDetails';

The rule should violate on not final and non-const top-level variables.

Having many mutable global variables inside application is a pretty bad practice:

  • application state becomes distributed between multiple files
  • application state is not protected: it can be modified in almost any place
  • it might be hard to debug such applications

So the common practice is to use state management solutions instead of mutable global variables.

Example {#example}

❌ Bad:

var answer = 42; // LINT
var evenNumbers = [1, 2, 3].where((element) => element.isEven); // LINT

class Foo {
  static int? bar; // LINT
}

✅ Good:

const answer = 42;
final evenNumbers = [1, 2, 3].where((element) => element.isEven);

class Foo {
  static int bar = 42;
}