Sub topic 11
Mistake-proofing in software: types, states, and confirmations
This chapter assumes you understand a process as a sequence of inputs, steps, and outputs. It also assumes you know the difference between an error and a defect, and can read short Python code. The focus here is how software can prevent an error or stop it before it becomes a defect.
Turn invalid actions into impossible actions
Software mistake-proofing, or software poka-yoke, uses program design to restrict what users and other programs can do. A useful first question is:
> What invalid value or action should the software make impossible?
A restricted type limits values to an approved set. An enumeration, often called an enum, is a type with named values. It is safer than accepting arbitrary text such as "paid", "Paid", or "payed".
A state-transition check controls which changes are valid. A state is the current condition of an object. A transition is a move from one state to another. For example, an order may move from DRAFT to PAID and then to SHIPPED. It should not move directly from DRAFT to SHIPPED.
from enum import Enum
class OrderStatus(Enum):
DRAFT = "draft"
PAID = "paid"
SHIPPED = "shipped"
ALLOWED_TRANSITIONS = {
OrderStatus.DRAFT: {OrderStatus.PAID},
OrderStatus.PAID: {OrderStatus.SHIPPED},
OrderStatus.SHIPPED: set(),
}
def change_status(current: OrderStatus, new: OrderStatus) -> OrderStatus:
if new not in ALLOWED_TRANSITIONS[current]:
raise ValueError(f"Cannot move from {current.value} to {new.value}")
return new
status = OrderStatus.DRAFT
status = change_status(status, OrderStatus.PAID)
# change_status(status, OrderStatus.DRAFT) raises ValueErrorThe check acts as a control device. It stops the invalid operation instead of allowing the defect to travel downstream. The transition table also makes the rule visible and testable.
Prevent positional and destructive mistakes
Two arguments may have the same data type but different meanings. A function that accepts two strings can accidentally receive a source account where it expects a destination account. Keyword-only arguments make names part of the call.
def transfer(
*,
source_account: str,
destination_account: str,
amount_cents: int,
) -> str:
if source_account == destination_account:
raise ValueError("Source and destination must differ")
if amount_cents <= 0:
raise ValueError("Amount must be positive")
return (
f"Moved {amount_cents} cents from {source_account} "
f"to {destination_account}"
)
transfer(
source_account="A-100",
destination_account="B-200",
amount_cents=2500,
)For destructive actions, a confirmation can require the operator to retype the target name. This is stronger than a preselected checkbox because the operator must read the value.
def delete_environment(name: str, confirm_name: str) -> str:
if confirm_name != name:
raise ValueError("Confirmation does not match; nothing was deleted")
return f"Deleted {name}"
delete_environment("staging", "staging")Design and verify the control
Start with the specific error, not with a favorite programming technique. Identify the action, its invalid outcome, and the earliest point where software knows enough to block it. Prefer a restricted type or state rule when the invalid action has no legitimate use. Use confirmation for rare, high-impact actions when blocking all use would be too strict.
Test both valid and invalid paths. Also test boundary values, repeated requests, stale state, and direct calls to the underlying API. A user interface check is easy to bypass if the server or domain layer accepts the same invalid operation.
Exercise
An application lets users archive an account by clicking a button. What software poka-yoke would you add before the archive operation, and where should it be enforced?
Show answer
Require an exact account-name confirmation for this high-impact action, then enforce the check in the server-side service as well as the user interface.