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: 1) am I truly alone in disliking this? 2) 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 typedef
are 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 a vector
or a list
or a deque
and 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:
typedef std::list<Foobar> FoobarList;
typedef std::map <string, FoobarList> GizmosToFoobarsMap;
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
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With