Help Us Name This Property

27 commentsWritten on March 13th, 2009 by
Categories: Software Development

Some of you have seen the Disposable base class before. If not, here it is again:

    public abstract class Disposable : IDisposable

    {

        private bool disposed;

 

        public void Dispose()

        {

            Dispose(true);

            GC.SuppressFinalize(this);

        }

 

        public bool Disposed

        {

            get { return disposed; }

        }

 

        protected void Dispose(bool disposing)

        {

            if (!Disposed)

            {

                if (disposing)

                {

                    DisposeManagedResources();

                }

 

                DisposeUnmanagedResources();

                disposed = true;

            }

        }

 

        protected void ThrowExceptionIfDisposed()

        {

            if (Disposed)

            {

                throw new ObjectDisposedException(GetType().FullName);

            }

        }

 

        protected abstract void DisposeManagedResources();

        protected virtual void DisposeUnmanagedResources() { }

    }

Notice the 'Disposed' property.

One of my coworkers has a class which inherits from this, and he wants to have a 'Disposed' event, which would be triggered after the instance was disposed.

So now, we are wondering if the 'Disposed' name is better for this property, or for the event. We're not sure which would be more suitable. Thoughts and/or alternatives?

  • http://mrpmorris.blogspot.com Peter Morris

    Either go for IsDisposed, or name your event AfterDisposed and ensure you call it at the end of disposing the instance.

  • http://davybrion.com Davy Brion

    Yeah, the IsDisposed name was also suggested by a coworker… i’m not a big fan of property names that start with ‘Is’ but if most people think that it is better than ‘Disposed’ then that’s how we’re going to name it

    i’m gonna hold off with renaming it for a bit longer though ;)

  • pho

    my vote goes to IsDisposed as property name
    and i’d actually go for AfterDisposing as eventname…

  • RCE

    As always, I think PHO is right :)

  • http://www.noctovis.net/blog Laila

    I agree with Pho…

  • http://davybrion.com Davy Brion

    AfterDisposing without a BeforeDisposing just seems weird to me

  • http://huseyint.com/ Hüseyin Tüfekçilerli

    WPF APIs also use “Is” prefix for boolean properties like IsEnabled, IsSelected, etc. This makes properties less discoverable because the first intention is to look up for properties Enabled, Selected, etc. and IntelliSense doesn’t help you in these cases.

    I would go for IsDisposed property and Disposed event in this case though.

  • Chris N

    How about ‘HasBeenDisposed’ i.e. it’s a little more descriptive?

  • http://weblogs.asp.net/koenv Koen

    event: Disposed
    property: IsDisposed or WasDisposed

  • http://thinkbeforecoding.com Jérémie

    I would go for IsDisposed.
    I prepere prefixing bool property with a verb that explicit the meaning of the property.. you can then use IsDisposed, ShouldDispose, MustDispose, CanDispose without ambiguity…

  • Cornelius Gouws

    property: IsDisposed (for reasons stated by Jérémie)
    event: AfterDispose (for in case you need to add BeforeDispose later on, then you don’t need to rename it again)

  • Jeremy Wiebe

    Property: IsDisposed (agree with Jérémie)
    Event: Disposed (The .NET framework, and the design guidelines book, uses pre/post event naming as “Disposing” (before) and “Disposed” (after).

    I’m not a big fan of “BeforeDispose” and “AfterDIspose”, but that’s my $0.02.

  • Brian Dustman

    The property should have been IsDisposed from the get go. As we have seen here, ‘disposed’ is ambiguous. Does it return a boolean (ie. the object is/was disposed)? Does it indicate the completion of a ‘dispose’ operation.

  • http://blechie.com/wtilton Willie

    +1 IsDisposed :)

  • http://blechie.com/wpierce/ Bill Pierce

    Isn’t such an event a bad idea? Won’t subscribers to the event keep the disposed instance alive indefinitely?

  • Chris

    IsDisposed +1

    I also think that using this as a base class is a mistake. Better to opt for the adapter pattern here.

    public class Disposable where T: IDisposable{

    public static implicit operator T(Disposable adapter){return adapter.innerDisposable;}
    }

  • Jeremy Gray

    IsDisposed, since while the FDG book might suggest Disposed there are plenty of other sources (both within and outside of the .net space) that would suggest the “Is” version.

  • http://davybrion.com Davy Brion

    @Bill Pierce

    if subscribers don’t unsubscribe at the appropriate time (which would be a bad idea regardless of the usage pattern of this particular event), they might keep the instance alive, but they won’t keep the data contained within the instance alive

  • http://blog.dynamicprogrammer.com Hernan Garcia

    Property: IsDisposed
    Event: AfterDisposed

    I think is clear, express intent and aparently most of us agree.
    (WasDisposed is not bad, but we are using a verb for the property, so I’m not sure about it.)

  • http://www.filip.duyck.org Filip Duyck

    IsDisposed, Disposing, Disposed.

  • http://benpittoors.wordpress.com den Ben

    Whatever you do, don’t call any event AfterDisposed because that’s just bad English:) (consider AfterDispose in that case)

  • http://davybrion.com Davy Brion

    @Den Ben

    i wouldn’t do that, my english be great!

    it’ll probably be IsDisposed for the property, and Disposed for the event

  • Peter

    AfterDispose? that’s just plain engrish Ben ;-)
    if you want to make it a noun it’s AfterDisposal

  • http://davybrion.com Davy Brion

    the Framework Design Guidelines book also explicitly recommends to not use ‘Before’ or ‘After’ in the names of events and to use present and past tenses instead. So ‘Disposing’ instead of ‘BeforeDispose’ (or ‘BeforeDisposal’) and ‘Disposed’ instead of ‘AfterDispose’ (or ‘AfterDisposal’)… Besides, the whole Before/After thing looks horrible IMO.

    The ‘Is’ prefix in property names is only recommended in case where it adds value… in this case i’d argue that the added value of using IsDisposed as the property name is that it removes ambiguity and allows us to name the event correctly: Disposed :)

  • pho

    it’s potatoes versus potahtoes i guess, and it just comes down to personal taste
    i like it, because i think of the times i used the asp.net objectdatasource: it has the events BeforeSelecting, AfterSelecting, BeforeDeleting, AfterDeleting, etc.

    Having ‘Selecting’ and ‘Selected’ as eventnames instead would add a lot more room for confusion imo. adding ‘Before’ and ‘After’ might mean typing a few more letters, but it greatly increases readability, compared to using 2 verbs where you actually have to have some degree of understanding of the english tenses. And let’s face it, not everyone knows the semantic difference between ‘selected’ and ‘selecting’

    also, framework design GUIDELINES. meaning you can deviate if you want ;-)

  • http://davybrion.com Davy Brion

    @Pho,

    i think you know me well enough to know i’d never blindly follow Microsoft’s guidelines :)

  • Pingback: The Inquisitive Coder - Davy Brion’s Blog » Blog Archive » Help Us Name This Method