What is the most concise (yet descriptive) way of naming a subclass that only add a specific minor thing to the parent? I encountered this case a lot in WPF, where sometime I have to add a small functionality to an out-of-the-box control for specific cases.
Example: TreeView doesn’t change the SelectedItem on right-click, but I have to make one that does in my application.
Some possible names are
TreeViewThatChangesSelectedItemOnRightClick
(way too wordy and
maybe difficult to read because there is so many words concantenated
together)TreeView_SelectedItemChangesOnRightClick
(slightly
more readable, but still too wordy and the underscore also breaks the normal
convention for class names)TreeViewThatChangesSIOnRC
(non-obvious
acronym),ExtendedTreeView
(more concise, but doesn’t describe what
it is doing. Besides, I already found a class called this in the
library, that I don’t want to use/modify in my application).LouisTreeView
,MyTreeView
, etc. (doesn’t describe what it is doing).
It seems that I can’t find a name which sounds right. What do you do in situation like this?
3
If your application is supposed to use both the original, non-right-sensitive version and your own improved version, then the name absolutely has to express what the derived class does. Otherwise everyone navigating the code base will go nuts understanding what the difference is and where it applies or not. Something like RightSensitiveTreeView
is probably your best bet, even though it is already clumsy and will become much more clumsy if you ever need to express more than one extended behaviour.
But if you use the extended version exclusively, there is little harm in using a euphonious, even proud name like LouisTreeView
– it will stay accurate even if you add more enhancements later, since those presumably will also be used everywhere. The only thing you should not do is reuse the original name exactly and disambiguate by putting it into a different package path. That is just asking for confusion.
(Disclaimer: proverbially, there are only two really hard things in computer science – cache invalidation and naming things. This is my opinion, but people will probably come forward with very different views.)
3
For a single class like your TreeView subclass that just adds a small amount of functionality, it’s probably not necessary to try too hard to make a good name. Personally I would make do with CustomTreeView
or similar. Then the actual “Custom” bit can be described in some docs or comments above the class definition.
However suppose you have several of these classes, each extending TreeView
in some small way. Then a generic name like the above wouldn’t cut it. You could attempt to name each customisation descriptively but then you things could go south – “RightSensitiveTreeView” for example is a bit lengthy for a class name.
If you want to add bits of functionality to a class, consider this approach: Create one subclass in your application (or personal/company library) and call it CustomTreeView
or whatever. Make it work identically to TreeView
by default. Add properties/methods that enable/disable the extended functionality – say, a selectOnRightClick
property would do nicely for your extension. Then you can add as many customisations as you like to your one subclass, and all code that uses your subclass automatically has access to those customisations, with minimal change to code.
1
If it’s just generic functionality that will probably be wanted anytime you use such a control, such as changing items on right-click, I usually just add Ex
to the end of my class to signify it’s an extended version of the class. For example, TreeViewEx
or TabControlEx
The actual word being used doesn’t really matter, but I do try and keep it consistent throughout all my customized classes, and generic so I can add other functionality later if needed without having to rename the class (Ex
, Extended
, Custom
, etc).
Of course, if the control does something specialized that is unlikely to be wanted in other cases when using that control, then I’ll add a more descriptive suffix to the control name instead.
I also add the word at the end of the control name (TreeViewCustom
instead of CustomTreeView
) so it pops up in auto-complete when you try and use the standard control.
5