Frequently, I settle for a general-purpose type when I really want a problem specific one. For example, I might choose
int when I really want an
id_number that shouldn't support arithmetic, or choose
std::map<int, std::string> when an ideal type would express a problem-specific invariant.
A general-purpose approach is often the best choice because the cost of writing a special-purpose type is far too high to be justified by the problem's scope.
As an alternative, I could loosely hint at the domain-specific meaning of a more general type by giving it an alias:
1 2 3
|
using symbol = int;
using bitcode = std::string;
using bitcode_table = std::map<symbol, bitcode>;
|
However, I do not typically do this: these force the programmer to remember the
real type beneath the alias, and otherwise serve only as comments with no associated semantics.
I also also found the cost of looking up the definition of a name frequently exceeds the benefit it provides - such code often feels over-factored.
Some style guides recommend creating these kinds of aliases. Am I correct to avoid them? If not, do you create them in your own code base? If so, when?
Thanks :)