-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Requirement
Add a general-purpose class wrapping a generically typed object and binding it to a validation condition. The setter of the generically typed object should always call validate before assignment. Concretise this class for nonnegative and positive integer types. Add an implicit type conversion operator allowing the wrapper type to be treated as the raw wrapped type.
Tests should be written for the concrete types.
The abstract class should be public, so that it can be extended by external code.
Value Proposition
Validating the value as soon as possible catches a bug at the source, rather than carrying it around like a ticking time bomb through multiple function calls, which then explodes into an exception with a long stack trace and the true bug buried somewhere in the middle of it. Another advantage of a validated type with proper access control modifiers means that we can always guarantee that an instance of this type satisfies the associated condition. This allows us to follow DRY: we don't have to re-check the condition when using the raw value from one of these types.
There are many use cases where we want a positive or nonnegative integer. It makes sense then to define these as standard validated types.
Design Ideas
- Add a
Validated<T>abstract generic type which has an abstract functionbool validate(T t). Concretise this class with specific validate functions for the various int types. - Add a Validated package for keeping all the validated types. Add a numeric package beneath this and an integer package beneath that again.