[New rule] avoid-global-state #536
Description
Please describe what the rule should do:
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.
Sorry if my explanations are not solid, just hope that you get the idea and the reasons to avoid global state.
If your rule is inspired by other please provide link to it:
Nope.
What category of rule is this? (place an "X" next to just one item)
[x] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)
Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):
All examples below assume that variables are defined at the top level file scope.
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;
}
Are you willing to submit a pull request to implement this rule?
Maybe