views:

98

answers:

4

Naming classes is sometimes hard. What do you think name of the class should be?

I originally created the class to use as a cache but can see its may have other uses. Example code to use the class.

Dim cache = New NamePendingDictionary(Of String, Sample)

Dim value = cache("a", Function() New Sample())

And here is the class that needs a name.

' <summary> '
' Enhancement of <see cref="System.Collections.Generic.Dictionary"/>. See the Item property '
' for more details. '
' </summary> '
' <typeparam name="TKey">The type of the keys in the dictionary.</typeparam> '
' <typeparam name="TValue">The type of the values in the dictionary.</typeparam> '
Public Class NamePendingDictionary(Of TKey, TValue)
    Inherits Dictionary(Of TKey, TValue)

    Delegate Function DefaultValue() As TValue

    ' <summary> '
    ' Gets or sets the value associated with the specified key. If the specified key does not exist '
    ' then <paramref name="createDefaultValue"/> is invoked and added to the dictionary. The created '
    ' value is then returned. '
    ' </summary> '
    ' <param name="key">The key of the value to get.</param> '
    ' <param name="createDefaultValue"> '
    ' The delegate to invoke if <paramref name="key"/> does not exist in the dictionary. '
    ' </param> '
    ' <exception cref="T:System.ArgumentNullException"><paramref name="key" /> is null.</exception> '
    Default Public Overloads ReadOnly Property Item(ByVal key As TKey, ByVal createDefaultValue As DefaultValue) As TValue
        Get

            Dim value As TValue

            If createDefaultValue Is Nothing Then
                Throw New ArgumentNullException("createValue")
            End If

            If Not Me.TryGetValue(key, value) Then

                value = createDefaultValue.Invoke()
                Me.Add(key, value)

            End If

            Return value

        End Get

    End Property

End Class

EDIT: On Abel's advice I've named the class ValueCache.

+1  A: 

In general, it's best to name a class after its intended usages. If users later find that another usage is possible or feasible, don't run off renaming your class. A reason to rename your class should only be to make its intended use clearer.

(edit) Others have commented on new names, like CacheManager, DeferredCache, LazyCollection, AssignedValueMap etc. If the original intention is very generic, use such names. If the intended use is more specific, name it such: CookiesCache, UsersList etc.

If you find yourself in the situation where you have a generic use case for an otherwise specific class, create a more general base class, with a general name, and use a specific subclass for the specific (original) use case. That's what OO is about ;-)

Abel
"NamePendingDictionary" refers to the fact I'm waiting for a good name for the class.
Tim Murphy
Ouch, my bad. Sorry, I'll edit my answer :)
Abel
Excellent answer. I'll sleep on it before accepting the answer. On your advice I will probably name the class based on its intended use. Cache.
Tim Murphy
Because it's inherited from `Dictionary`, consider using Map, Hash or Dictionary as postfix: `CacheMap`, `CacheHash` or `CacheDictionary`. If its purpose is wider (i.e, more general) than merely serving as a lookup map, use something like `CacheManager` or `ValueCache` instead.
Abel
A: 

Call this class CacheManager and DO NOT put other functionaities in it.

medopal
+1  A: 

You could call it a LazyDictionary, since items may not be initialized until they are needed :)

What is createDefaultValue? Is this initialized as part of the constructor?

Justin Ethier
Like the name LazyDictionary. createDefaultValue is the delegate/lambda that will be invoked if the key is not in dictionary. The resulting value will be added to the dictionary.
Tim Murphy
Cool... glad you like the name.
Justin Ethier
A: 

I think as Abel suggested Map is a good term to use. I would consider naming the class something like AssignedValueMap and then use more specific variable names to make clear how you're using the AssignedValueMap in each case. So where you're using it for pending names I'd declare the associated variable as Dim pendingNamesMap = New AssignedValueMap(Of String, Sample).

bglenn
@bglenn. I think you got side tracked by @Abel original answer to my question when he thought NamePendingDictionary was leaning towards the final name of the class. Check @Abel's answer and associated comments.
Tim Murphy
Yes, I did. I see your point. My main point was that if you are going to abstract the class and use a generic name for it, that you should use a more specific variable name to qualify the way you're using it.
bglenn