Skip to content

State Management Antipatterns

State management antipatterns occur when application state is handled in ways that make code unpredictable, hard to debug, and impossible to test.

Shared variables that any part of the code can modify.

Conflicting, stale, or poorly managed feature flags.

State accessed through globals instead of explicit parameters.



AntipatternProblemSolution
Global Mutable StateUnpredictable changesDependency injection
Feature Flags ChaosConflicting conditionsFeature flag service

// Bad - hidden dependency
function getDiscount() {
return globalConfig.discountRate * globalUser.tier
}
// Good - explicit parameters
function getDiscount(config, user) {
return config.discountRate * user.tier
}
// Bad - mutating state
globalState.users.push(newUser)
// Good - create new state
const newState = {
...state,
users: [...state.users, newUser]
}
// Bad - state scattered everywhere
const userCache = {} // in cache.js
const userData = {} // in user.js
const userSession = {} // in session.js
// Good - centralized store
const store = createStore({
users: {},
sessions: {},
cache: {}
})