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

[New rule] avoid-global-state #536

Closed
@roman-petrov

Description

@roman-petrov

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

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions