I have coworkers who occasionally use typedef to avoid typing. For example:
typedef std::list<Foobar> FoobarList;
...
FoobarList GetFoobars();
Personally, I always hate coming across code like this, largely because it forces me to go lookup the typedef so I can tell how to use it. I also feel like this sort of thing is a potential slippery slope… If you do this, why aren’t you doing it more? (pretty soon, your code is totally obfuscated). I found this SO question regarding this issue:
When should I use typedef in C++?
I have two questions:
- am I truly alone in disliking this?
- If the vast majority of people think this sort of typedef use is OK, what criteria do you use to determine whether to typedef a type?
The two big arguments for this type of
typedefare the reduced typing, which you’ve already mentioned, and the ease of changing over to a new type of container. A FoobarList could be backed by avectoror alistor adequeand switching would often just require changing a typedef.Your dislike of them when it comes to looking them up, is quite reduced when dealing with IDEs, since I can just hover over the type name, and the IDE tells me what it’s defined as.
The more useful situations are when you have nested containers – you can give the names some semantic meaning without having to define entire classes:
You can also save a LOT of typing when dealing with iterators of these types (although that’s reduced now that C++0x has
auto.)