While concluding the study of the popLast example, the Contracts chapter recommends removing the explicitly documented precondition. I think this is worth reconsidering, as there might be any of several behaviors that a caller could expect from empty.popLast() - Will it throw? Terminate? March off into UB? Return a null or default value? Some of these behaviors won't be possible in some languages, but an explicit precondition tells me: "This is an assumption that the caller is responsible for fulfilling; don't count on any particular behavior if it's not met." Omitting the precondition makes the function feel under-documented, and leaves the width/narrowness of the contract something that must be interpreted/inferred (which is probably a higher cognitive load than reading a few extra words).
An argument could be made that a caller should not "reasonably expect" some of the above behaviors if they aren't documented. I think that perspective is "technically correct", but not necessarily productive: Explicitly documenting the precondition better guards against "unreasonable" (i.e., tired, under-a-deadline, quick-reading, etc) callers.
While concluding the study of the
popLastexample, the Contracts chapter recommends removing the explicitly documented precondition. I think this is worth reconsidering, as there might be any of several behaviors that a caller could expect fromempty.popLast()- Will it throw? Terminate? March off into UB? Return a null or default value? Some of these behaviors won't be possible in some languages, but an explicit precondition tells me: "This is an assumption that the caller is responsible for fulfilling; don't count on any particular behavior if it's not met." Omitting the precondition makes the function feel under-documented, and leaves the width/narrowness of the contract something that must be interpreted/inferred (which is probably a higher cognitive load than reading a few extra words).An argument could be made that a caller should not "reasonably expect" some of the above behaviors if they aren't documented. I think that perspective is "technically correct", but not necessarily productive: Explicitly documenting the precondition better guards against "unreasonable" (i.e., tired, under-a-deadline, quick-reading, etc) callers.