Skip to main content
OBJECT-ORIENTED-PROGRAMMING5 MIN READ

Sort commands from queries

Classify object methods using Command-Query Separation.

Sort each method by its observable behavior. Commands change state, queries answer without side effects, and mixed-risk methods do both or hide side effects. Command: changes observable state Query: returns information only Mixed-risk: asks and changes subscription.cancel(reason) updates status and records cancellation time invoice.balanceDue() returns a Money value without saving anything cart.total() recalculates discounts and persists the new discount rows user.isLocked() checks current lock state without changing counters order.markPaid(paymentId) changes payment state and emits a domain event session.currentUser() refreshes the session expiry as a hidden side effect emailQueue.enqueue(message) adds work to the queue dateRange.overlaps(otherRange) returns true or false only

Read the full lesson

Sign up free — one personalized lesson every day, matched to your role and goals.

Already have an account? Sign in

← Back to library
Contact us