As part of the overhaul of the WilsonORWrapper, I’m adding a cache service. To do this right, I need to implement an
ICacheClient interface. This interface will define the methods and properties which any cache client will need to implement.
Sounds simple on the surface, but I quickly ran into a problem. What do you call your methods? Consider the following possibilities for naming four core methods.
- Retrieving an item from the cache:
- Placing an item in the cache:
- Determining if an item exists in the cache:
- Remove an item from the cache:
- Clear all items from the cache:
The worst part of determining a naming convention is that you are generally stuck with it once you choose it, unless you want to implement breaking changes into your application.
Most of the names above were found in some cache API currently in existence today. (I did a Google search to find as many as possible.) Some of them are not recommended because their usage is uncommon (such as
Fetch, unless you have a
DogCacheClient). I’d also stay away from the *All methods — they sound good on paper, but the difference between
RemoveAll is quite big, and you can call the wrong one if not paying sufficient attention.
With those left over, you can organize them into groups based on similarity of use in well-known applications, languages, and APIs.
- SQL Style:
This style is good if you expect to have separate
Insertcan only add new items, and
Updateis used to change existing items.
- Collection Style:
The common naming used in Microsoft and Java collections.
- Simple Style:
Short and sweet. You can arguably combine
Clearif you’re brave.
Which one should I use for the WilsonORWrapper? One thought is to use the SQL style, since this is a wrapper for an O/R mapper, which is closely tied to SQL. However, I don’t want separate
Update methods. Also, the point of the wrapper is to allow people to write in code, not in SQL, so the relationship to SQL should not dictate style.
That leaves us with the simple style and the collections style. NHibernate uses the simple style in their Cache API. Microsoft’s System.Collections namespace uses the collection style. I’ll go with a combined version:
Clear, mostly for the reason that users of WilsonORWrapper are probably more familiar with Microsoft Collections than NHibernate, and because I prefer
Have you seen, or used, other naming conventions in your travels?
0 thoughts on “Choosing method names for a cache interface”
You are quite busy my friend. I still have not even started on my own blog. I look forward to reading more of your blog. Things seem to be falling into place here. Not bad… not bad at all.
Yes it is coming together. I try to copy a few posts each day from the old site to the new site. According to Google Analytics, the original domain name still gets about five times the traffic, but that’s likely because Google is pointing to those pages. Once I move all the posts to this site, I’ll permanently redirect from demarzo.net to sidesofmarch.com.
Alfredo Ramos says:
I would suggest, to just follow the standards: