State Management Antipatterns
Overview
Section titled “Overview”State management antipatterns occur when application state is handled in ways that make code unpredictable, hard to debug, and impossible to test.
Common Problems
Section titled “Common Problems”1. Global Mutable State
Section titled “1. Global Mutable State”Shared variables that any part of the code can modify.
2. Feature Flag Chaos
Section titled “2. Feature Flag Chaos”Conflicting, stale, or poorly managed feature flags.
3. Hidden Dependencies
Section titled “3. Hidden Dependencies”State accessed through globals instead of explicit parameters.
Articles in This Section
Section titled “Articles in This Section”- Global Mutable State - Shared state chaos
- Feature Flags - Flag management gone wrong
Quick Comparison
Section titled “Quick Comparison”| Antipattern | Problem | Solution |
|---|---|---|
| Global Mutable State | Unpredictable changes | Dependency injection |
| Feature Flags Chaos | Conflicting conditions | Feature flag service |
Key Principles
Section titled “Key Principles”1. Explicit is Better Than Implicit
Section titled “1. Explicit is Better Than Implicit”// Bad - hidden dependencyfunction getDiscount() { return globalConfig.discountRate * globalUser.tier}
// Good - explicit parametersfunction getDiscount(config, user) { return config.discountRate * user.tier}2. Immutable When Possible
Section titled “2. Immutable When Possible”// Bad - mutating stateglobalState.users.push(newUser)
// Good - create new stateconst newState = { ...state, users: [...state.users, newUser]}3. Single Source of Truth
Section titled “3. Single Source of Truth”// Bad - state scattered everywhereconst userCache = {} // in cache.jsconst userData = {} // in user.jsconst userSession = {} // in session.js
// Good - centralized storeconst store = createStore({ users: {}, sessions: {}, cache: {}})