Why no non-null smart pointers?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Why no non-null smart pointers?

Ross MacGregor
I just created a very small smart pointer libary for our project. It will
contain two types of shared pointers: SharedHandle and NullableSharedHandle.
SharedHandle will be a shared pointer that cannot be null by ensuring a
non-null construction while NullableSharedHandle will basically be a typedef
for boost::shared_ptr.

I firmly believe that code can be made more reliable by minimizing the use
of nullable pointers. To this end, I think it is important that we start
using non-null pointer types. Ideally the compiler would be able to
understand a non-null pointer and catch bugs at compile time, but I think we
can still improve things with a few runtime checks. If we make sure that a
shared pointer is constructed with a non-null pointer and ban any reset()
type methods, we guarentee that it points to something. This can be
beneficial for the following reasons:

1. Any function that uses this type will no longer be burdened with
precondition checks to make sure variables of this type are not null.
2. It's type is simply more explicit and better explains its function.
3. Moving the non-null check to the creation phase will reduce the
possibility of encountering null pointer exceptions at runtime (assuming
there are many more pointer references than constructor calls).

So my little pointer library seems to make a lot of sense, but if so why
hasn't this simple smart pointer variation made it into boost yet? Has this
been discussed somewhere already? My search on non-null smart pointers is
not revealing much discussion on the topic.

Regards,
Ross
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Why no non-null smart pointers?

Johan Råde
Ross MacGregor wrote:
> I just created a very small smart pointer libary for our project. It will
> contain two types of shared pointers: SharedHandle and NullableSharedHandle.
> SharedHandle will be a shared pointer that cannot be null by ensuring a
> non-null construction while NullableSharedHandle will basically be a typedef
> for boost::shared_ptr.
>
> I firmly believe that code can be made more reliable by minimizing the use
> of nullable pointers.

[snip]

Your idea certainly could be useful.
It should probably be combined with the Null Object Pattern.

However, having many different smart pointers would make it more difficult
for different software components to interact with each other.

--Johan Råde

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Why no non-null smart pointers?

Frank Mori Hess
In reply to this post by Ross MacGregor
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wednesday 02 April 2008 12:58 pm, Ross MacGregor wrote:
> I just created a very small smart pointer libary for our project. It will
> contain two types of shared pointers: SharedHandle and
> NullableSharedHandle. SharedHandle will be a shared pointer that cannot be
> null by ensuring a non-null construction while NullableSharedHandle will
> basically be a typedef for boost::shared_ptr.

If you're going to make a shared_ptr wrapper that acts like a reference (no
null, no reset, I assume no assignment) then why not complete the set and add
another that acts like a value?  That is, one that does deep copies of the
template type on assignment, and it could also have the non-null requirement.  
It would differ from an ordinary value in that you could obtain shared_ptr
(or your other wrapper types) from it which share ownership.  It would be
useful for cases where you currently have a class with a shared_ptr member
but need to do deep copies of the object owned by the shared_ptr in the
containing class' assignment/copy constructor.

- --
Frank
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFH9NmK5vihyNWuA4URAg76AJ4/j4DbkRbaiS4KsByXXN+VAoh8uQCeNjY4
KMf3tP3jIKffiOFMZzRLMpA=
=IIlH
-----END PGP SIGNATURE-----
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Why no non-null smart pointers?

Ross MacGregor
In reply to this post by Johan Råde
"Johan Råde" <[hidden email]> wrote in message
news:ft10ag$6nn$[hidden email]...

>Your idea certainly could be useful.
>It should probably be combined with the Null Object Pattern.

Yes, it would be a way to enforce the Null Object Pattern.
Although one should only use null objects where they make sense.
Null objects should not be used to pass additional state
information. If you create a find function that returns a null
object if no items are found, you really gain nothing from using
the pattern and the code will probably be more prone to error.

>However, having many different smart pointers would make it more
>difficult for different software components to interact with
>each
>other.

Yes I think this variant of smart pointer will encounter
resistance from the community because it is much easier to have
only one shared_ptr to worry about. This is why we have the
boost::shared_ptr and not a policy based one. The nullability
attribute would fit nicely into a policy based smart pointer as
the policy design avoids the complexity explosion of many
separate classes.

Thinking about this some more, I think there may be a more
fundamental problem at play here. I would suggest that smart
pointers as we have come to know them are poorly designed. It's
only natural that they evolved to reflect the capabilities of the
C++ pointer (indeed they are called smart POINTERS), but are the
semantics of C++ pointers a good idea in terms of a class
interface? I would argue that they are not. A smart pointer fails
the One Responsibility Rule regarding the its nullability.
Allowing a smart pointer to be null is an extra state carried by
the class when it could be maintained by other means.

If we remove null capability from all smart pointers, essentially
turning them into smart references, what do we do when we
absolutely need to represent a reference that does not exist (or
dare I say optional)? Well boost already has the answer. We use
boost::optional; a template for representing optional values.
Using boost::optional would free us from a proliferation of smart
pointers (nullable and not) and usability concerns.

I'm sure no one is going argue that optional<int> creates too
much interface friction because int is used commonly as component
interface type. Therefore optional<WidgetRef> should not create
problems for components that use WidgetRef.

I know it is an insane idea to suggest changing the entire
boost::smart_ptr library to a boost::smart_ref libary, but I
think it may actually be the right thing to do. Does anyone
agree?

-Ross




_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Loading...