It’s standard practice to name component instances referenced in code. It’s also helpful to name components that appear in the tab order. Typically a name consists of a description and a type abbreviation.
There is some debate, although I’ll argue it’s unfounded, as to whether the abbreviation should be added as a suffix or a prefix. See the article: “Delphi Standards: The Case for Suffixes” for a complete discussion.
When naming a component instance the two main goals are to:
In the past, a name also helped to identify the specific type. This is less important today. With Delphi’s tooltip symbol insight you get type information by pointing at an identifier. As you’ll see later, this fact will influence our abbreviation choices.
Knowing that consistency is important; how do we get everyone on a development team to abbreviate in a consistent manner? The standard approach has been to make a list of abbreviations for each type. You can find several examples on the web.
The problem with lists
At a glance, lists seem to be a good way to gain consistency, but practice doesn’t bear this out. Developers often find lists more bother than they’re worth. Lists don’t work because they:
While lists are often initially adopted with conviction, teams
usually slip away from them as projects progress. The larger the team, the larger
the project, and the higher the pressure the more likely slippage is to occur.
A core set of abbreviations usually sticks, but inconsistency becomes common
as new and uncommon types are introduced.
A better solution: rules based naming
Instead of a long list of all the abbreviations, using a set of rules to create names provides a simpler and more flexible approach. The rules will be complemented with a small list of just the exceptions.
Also important is minimizing the number of abbreviations to only those that are absolutely necessary. As previously mentioned, we don’t need to be too specific because tooltip symbol insight will help when we need more information. Also note that longer and more specific abbreviations are harder to quickly interpret, making the code less scanable.
As you name components in your application, apply the following rules in order:
*This results in a lengthier but usually clearer abbreviation.
**Remember be stingy about this! Keep the exceptions list small.
A few examples:
| Type | Abbreviation | Rule Applied |
| TButton | Btn | 1 a,e,f |
| TEdit | Edt | 1 a,f |
| TDBEdit | Edt | 1 a,c,f |
| TMainMenu | Mnu | 1 a,d,f |
| TcxShellListView | ShellListView | 3 |
As you review the examples, it bears repeating that having an abbreviation that provides specific type information is not a goal. Tooltip symbol insight is available if we need more detail. The lack of specificity helps reduce the number of abbreviations so you’ll remember many without ever applying the rules.
Note that the rules are meant to yield improved consistency, not perfect consistency. Some sacrifice is needed to keep things simple. Without it, the rules could become just as unwieldy as the lists they sought to replace. Keep ease of use in mind as you make adjustments to better fit your team.
A rule based system for naming component instances provides more consistency than a static list. Being only as specific as is needed for uniqueness and readability is a key to making the naming system simple and successful. To get started, gather a team consensus around a simple set of rules to name your component instances.
Questions or Comments for the author
Copyright ©2003 Rick Ross