Question

Consider a poorly-designed interface in which there are two functions (say foo() and bar()) that can be called independently, but in order to get some expected behavior (or in order to keep the object status consistent) the users have to call function bar() each time they call function foo().


Some examples:

interface SearchBinaryTree<T> {
    public add(T item): void;
    public rebalance(): void;
}

If add() just inserts an item in the tree this interface is not properly designed, because the responsibility to rebalance the tree is left to the user which may forget to call rebalance() after.

Another example:

interface VideoGame {
    public reset(): void;
    public play_level(level_no: number): void;
}

play_level() should work even if reset() was not called before, otherwise this interface is not properly designed.

Note that this time the problem is not the existence of the function reset() itself, rather than the fact that play_level() does not call it internally.


I'm interested in this kind of error: computer science students often make this mistake when designing their interfaces or API mostly because they don't understand how programming by contract works and they think that defining methods in the interfaces is just a way to split the code.

UX designers sometimes make this mistake too, for example by requiring to manually press the a button (say reset) before doing some other action (say insert the level number and click play) that must be done after pressing that button. The correct interaction in this case should be that the user just inserts the level number and clicks play; the application internally resets its variables and then loads the level.

Is there a name for this kind of error?

Was it helpful?

Solution

This is called temporal coupling. A phenomenon that occurs when two (or more) methods have to be called in a specific order.

Licensed under: CC-BY-SA with attribution
scroll top