Constructing an Object in a Valid State

A constructor can establish the properties other methods assume, so callers receive an object ready to use under its contract.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Define an interval record
  2. Check all creation paths

Define an interval record

A duration range can require that its end be at least its start. Constructing it with start 7 and end 4 should be handled explicitly. Otherwise another method may compute a negative duration. See interval representation.

Check all creation paths

A factory, parser, or copy method should preserve the same assumption when returning an object of that type. Use preconditions to distinguish caller duties from checks the constructor itself promises.[1]

References

  1. Python Software Foundation, Python Tutorial: Classes ↩