See what's going on with flipcode!




This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Using Typedefs To Write Maintainable Code
  Submitted by



Using typedefs to write maintainable code (when using templates and the STL)

Writing code isn't everything. Being able to maintain your code is also very important, (especially in a team environment where others may have to maintain your code, and you may have to maintain code written by others). The key to being able to maintain your code, is writing maintainable code, this tip should help you do that.



When you instantiate a template you create a new type. When you create a type it has a name, and that name is part of its interface. In fact, when you refer to a type, you often do so through its name, which makes the name a very important part of the interface.



We all know that separation of interface and implementation is very important, this use of abstraction is one of the fundamental tenants of object orientated programming, and lets us write flexible, orthogonal systems. Yet the name given to this type by the compiler describes its implementation, not its type.



Lets have look at the new type we create when we instantiated a template. We will assume we are creating, say, a list of names, for identifying players:

//...
std::vector<std::string nameList;
//... 


What we have done here is create a type that is intended to be a Name List, but when we use it in our code we refer to it as a Vector Of Strings. Not necessarily what we wanted. At a later date you may discover that the usage pattern of the Name List is not quite what you expected, and your getting a performance hit, you may want to change how you implemented it. Unfortunately you code is already littered with code that looks like this:



//...
for (vector<string::iterator i = nameList.begin(); i != nameList.end()){
  //...
}
//... 


And that's not all. You've used plenty of vectors of strings in other places in your code, and for other reasons. Perhaps you've got mixed namespace use (sometimes std is manually qualified, sometimes the names spaces has been 'opened'). Even if you do a search-and-replace, every line of code you touch should be re-tested.

But this problem is easily solved. Instead you create descriptive type names using the typedef keyword:

//...
typedef std::vector<std::string NameList;
NameList nameList;
//... 




Now where you use the container, your code will look like this:

//...
for (NameList::iterator i = nameList.begin(); i != nameList.end()){
  //...
}
//... 


But that's not all - we can take this further. Perhaps std::string wasn't the best implementation to use for names. Names after all, don't change very often. Perhaps you should have rolled your own class for that. But you've got std::string references everywhere. Same problem, same solution:

//...
typedef std::string Name;
typedef std::vector<Name NameList;
NameList nameList;
//... 


These will not only make your code easer to maintain, they will also make your code easier to read.


The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.