[Development] isValid() as a property

Tony Van Eerd tvaneerd at blackberry.com
Sat Nov 30 00:03:42 CET 2013

> On Fri, 29 Nov 2013, Tony Van Eerd wrote:
> > in the cases I am looking at, if isValid() == false, then all the
> other
> > properties are invalid, and return "", -1, etc.  It really does mean
> the
> > whole object is invalid.
> >
> > (except maybe an 'id', in the case of, say, a Battery object - the id
> > telling you which battery, and then isValid() saying that battery
> > doesn't (currently) exist.)
> Why not use exists, present, active or something like it?

'present' was in fact my first choice (for battery at least).  But since the behaviour seems to match that of isValid(), why not isValid()? If we could (ie if we didn't care about compatibility), would we rename all the other isValid() functions on existing Qt classes?  If not, what is the difference?

If there is a difference, and a clear rule when to use isValid() and when not to, I'd be quite happy.  But I want to understand the difference first.

> But a C++ object
> for a physical object that does not exist... why is it there in the
> first
> place?

Essentially because we don't throw exceptions in constructors.

	BatteryInfo battery(5); // the 5th battery.
	if (battery.exists())....

We could have a battery-manager that instead hands out the batteries (so never hands out an invalid one) but:

- BatteryInfo is a QObject (for signals, like the battery level changing) and I'd prefer not to return allocated objects (and then need to deal with ownership). It would be fine if they were value-types, but they aren't.
- even if the BatteryInfo started out existing, it could be removed while still running (on some devices)
> Harri.
This transmission (including any attachments) may contain confidential information, privileged material (including material protected by the solicitor-client or other applicable privileges), or constitute non-public information. Any use of this information by anyone other than the intended recipient is prohibited. If you have received this transmission in error, please immediately reply to the sender and delete this information from your system. Use, dissemination, distribution, or reproduction of this transmission by unintended recipients is not authorized and may be unlawful.

More information about the Development mailing list