That&#x27;s called &quot;Design By Contract&quot;. You formally specify pre&#x2F;post-conditions of functions and invariants of classes (such as &quot;cap &gt;= len&quot; in this case), and have the compiler enforce that automatically, e.g. by inserting runtime checks.