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:
Get
,Retrieve
,Load
,Fetch
,Read
,Select
. - Placing an item in the cache:
Put
,Set
,Add
,Insert
,Save
,Update
. - Determining if an item exists in the cache:
Contains
,Exists
,Has
. - Remove an item from the cache:
Remove
,Delete
,Clear
. - Clear all items from the cache:
Flush
,Clear
,ClearAll
,DeleteAll
,RemoveAll
.
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 Remove
and 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:
Select
,Insert
,Update
,Exists
,Delete
,Clear
.
This style is good if you expect to have separateInsert
andUpdate
methods, whereInsert
can only add new items, andUpdate
is used to change existing items. - Collection Style:
Get
,Add
,Contains
,Remove
,Clear
.
The common naming used in Microsoft and Java collections. - Simple Style:
Get
,Put
,Has
,Remove
,Clear
.
Short and sweet. You can arguably combineRemove
andClear
if 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 Insert
and 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: Get
, Set
, Contains
, Remove
, Clear
, mostly for the reason that users of WilsonORWrapper are probably more familiar with Microsoft Collections than NHibernate, and because I prefer Set
over Add
.
Have you seen, or used, other naming conventions in your travels?
Louis says:
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.
brian says:
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:
https://jsr-107-interest.dev.java.net/javadoc/javax/cache/package-summary.html
Regards,
Alfredo