Assume an interface that allows queries on a spatial tree
public ISpatialTree
{
int FindChildIndex(Vector point);
bool IsLeaf { get; }
ICollection<ISpatialTree> Children { get; }
}
and another one that allows changing the tree:
public ISpatialTreeWriter : ISpatialTree
{
void Add(Vector point);
bool Remove(Vector point);
void Clear();
}
I am looking for a name to denote the writeable interface. Ideally it would be reasonably short and readable word. It’s not only for this example, but I would like to use a convention for all code in the project because this is a common case.
Does anyone know of a good convention for this problem? What conventions are used in other projects?
Words that were discussed include Writer (ambiguous, often used in other contexts), Mutator (confusing), Rw (unreadable, not a word).
A good solution would be a single word, not longer than 6-8 letters and that makes it obvious that this is the writer interface.
7
Writer and Mutator are a bad choice because they imply mutating something else. If you change the object itself, the proper forms are Writable and Mutable.
If you have a readonly interface but the underlying object can be changed through another interface, that object isn’t immutable. This implies that mutability vs. immutability is not the distinction you want to make here.
So only ReadOnly, ReadWrite or Writable remain. Since you don’t have WriteOnly access at all you don’t need to distinguish ReadWrite from WriteOnly. ReadWrite isn’t a proper word, so I’d rather go with the simpler Writable.
Following the convention of the existing collections in .NET, the appropriate choices would be IReadOnlySpatialTree
and ISpatialTree
, where the latter is writable.
If most of your code is only reading and write access is the exception, you could deviate from this convention and use ISpacialTree
as the readonly interface and IWritableSpacialTree
as the writable interface.
4
I would suggest that you specify a read-only interface style, such as:
interface IReadOnlySpatialTree
{
}
and then the writeable interface extends this with its name only, ie.:
interface ISpatialTree : IReadOnlySpatialTree
{
}
You will rarely use a writeable interface without also needing the readonly part. If you’re not careful you’ll end up with at least 3 interfaces for every type (readonly/ writeonly and readwrite), which isn’t particularly useful and is a waste of energy to maintain.
Having a readonly interface is useful, as these methods are presumably pure, which simplifies caching and other aspects. It also means one can ensure that writes do not happen unintentionally. However, for a writeable interface, being also to read also doesn’t really matter- the readonly interface doesn’t have side-effects.
There is the rather weird method List<T>.AsReadOnly in the .NET framework, as MS didn’t consider readonly in the first place.
2