Click here to Skip to main content
13,147,182 members (31,238 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 9 Feb 2014

Improve Code Clarity with Typedef

, 9 Feb 2014
Rate this:
Please Sign up or sign in to vote.
How to improve code clarity with Typedef

The concept of selecting descriptive variable names is a lesson that seems to start almost the moment you pick up your first programming book. This is sound advice, and I do not contest this. However, I think that the basis could be improved by creating and using the most appropriate type for the task at hand. Do you believe that you already use the most appropriate type for each job? Read on and see if there is possibly more that you could do to improve the readability, maintainability or your programs, as well as more simply express your original intent.

Before I demonstrate my primary point, I would like to first discuss a few of the other popular styles that exist as an attempt to introduce clarity into our software.

Hungarian Notation

First a quick note about the Hungarian Notation naming convention. Those of us who started our careers developing Windows applications are all aware of this convention. This convention encodes the type of the variable in the name using the first few letters to mean a variable type code. Here is an example list of the prefixes, the types they represent and a sample variable:


bool    bDone		
char    cKey;		
int     nLen;
long    lStyle;		
float   fPi;		
double  dPi;		
// Here are some based on the portable types defined 
// and used throughout the Win32 API set.
BYTE    bCount;
WORD    wParam;
DWORD   dwSize;
SIZE    szSize;		
LPCSTR  psz;		
LPWSTR  pwz;

Some of the prefixes duplicate, such as the bool and byte types, which both use b. It's quite common to see n used as the prefix for an integer when the author would like to create a variable to hold a count. Then we reach the types that have historical names, that no longer apply. LPCSTR, LPWSTR and all of the other types that start with LP. The LP stands for Long Pointer, and was a necessary discriminator with 16-bit Windows and the segmented memory architecture of the x86 chips. This is an antiquated term that is no longer relevant with the 32-bit and 64-bit systems. If you want more details, this article on x86 memory segmentation should be a good starting point.

I used to develop with Hungarian Notation. Over time, I found that variables were littered through the code marked with the incorrect type prefix. I would find that a variable would be better suited as a different type. This meant that a global search and replace was required to properly change the type, because the name of the variable would need to be changed as well.

Why is the Type Part of the Name?

This thought finally came to mind when I was recovering from a variable type change. Why do I need to change every instance of the name, simply because I change its type? I suppose this makes sense when I think back to what the development tools were like when I first started programming. IDEs were a little more than syntax highlighting editors that also had hooks to compile and debug software.

It was not until the last decade that features like Intellisense and programs like VisualAssist appeared that improved our programming proficiency. We now have the ability to move the cursor over a variable and have both its type and value be displayed in-place in the editor. This is such a simple and yet valuable addition. These advancements have made the use of Hungarian Notation an antiquated practice. If you still prefer Notepad, may God have mercy on your soul.

Naming Conventions

Wow! Naming conventions huh?! My instinct desperately inclines me to simply skip this topic. This is a very passionate subject for almost every developer. Even people that do not write code feel the need to weigh in with an opinion. Let's simply say for this discussion, variable conventions should be simple with a limited number of rules.

Even though I no longer use Hungarian Notation, I still like to lightly prefix variables in specific contexts, such as a 'p' prefix to indicate a pointer, 'm_' for my member variables, and 'k_' for constants. The 'm_' gives a hint to ownership in an object context, and it simplifies the naming of sets of variables. Anything that helps eliminate superfluous choices can help me focus on the important problems that I am trying to solve. One last prefix I almost forgot is the use of 'sp' for a smart or shared pointer. These are nice little hints for how the object will be used or behaviors that you can expect. The possibility always remains that these types will change, however I have found, in fact, that variables in these contexts rarely do change.

Increase the Clarity of Intent

Developing code is the expression of some abstract idea that is translated into a form the computer can comprehend. Before it even reaches that point, we the developers need to understand the intention of the idea that has been coded. Using simple descriptive names for variables is a good start. However, there are other issues to consider as well.


double velocity 
double sampleRate

Unit Calculations

There is a potential problem lurking within all code that performs physical calculations. The unit type of a variable must be carefully tracked, otherwise a function expecting meters may receive a value in millimeters. This problem can be nefarious, and generally elusive. When your lucky, you catch the factor of 1000 error, and make the proper adjustment. When things do not work out well, you may find that one team used the metric system and the other team used the imperial system for their calculations, then an expensive piece of equipment could crash onto mars. Hey! It can happen.

One obvious solution to help avoid this is to include the units in the name of the variable.


double planeVelocityMetersPerSecond
double missileVelocity_M_s
long   planeNavSampleRatePerSsecond
long   missileNavSampleRate_sec

I believe this method falls a bit short. We are now encoding information in the variable once again. It definitely is a step in the right direction, because the name of the variable is less likely to be ignored compared to a comment at its declaration that indicates the units of the variable. It is also possible for the unit of the variable to change, but the variable name is not updated to reflect the correct unit.

Unfortunately, the best solution to this problem is only available in C++ 11. It is called user-defined specifiers. The suffixes that we are allowed to add to literal numbers to specify the actual type that we desire, can now be defined by the user. We are no longer limited to unsigned, float, short, long, etc. This sort of natural mathematical expression is possible with user-defined specifiers:


// based on the user specifiers appended to each value. // The result type will be Meters per Second; // A user-defined conversion has been implemented // for this to become the type Velocity. Velocity Speed = 100M / 10S; // The compiler will complain with this expression. // The result type will be Meter Seconds. // No conversion has been created for this calculation. Velocity invalidSpeed = 100M * 10S;

It is now possible to define a units-based system along with conversion operations to allow a meter type to be divided by a time type, and the result is a velocity type. I will most likely write another entry soon to document the full capabilities of this feature. I should also mention that Boost has a units library that provides very similar functionality for managing unit data with types. However, the user-defined specifiers are not part of the Boost implementation, it's just not possible without a change to the language.

Additional Context Information for Types

The other method to improve your type selection for your programs is to use typedef to create types that are descriptive and help indicate your intent. I have come across this simple for loop statement thousands of times:


int maxCount = max;	
for (int index = 0; 
index < maxCount; 
    DoWork( data[index]);		

Although there is nothing logically incorrect with the previous block, because of the type of index is declared as a signed integer, future problems could creep in over time with maintenance changes. In this next sample, I have added two modifications that are particularly risky when using a signed variable for indexing into an array. One of these examples modifies the index counter, which is always dangerous. The other change does not initialize the index explicitly, rather a function call a function call whose return value is not verified initializes the index. Both changes are demonstrated below:


for (int index = find(data, "start"); 
index < maxCount; 
if (!DoWork( data[index]))
index += WorkOffset(data[index]);

The results could be disastrous. If the find call were to return a negative value, an out of bounds access would occur. This may not crash the application, however it could definitely corrupt memory in a subtle way. This creates a bug that is very difficult to track down because the origin of the cause is usually nowhere near the actual manifestation of the negative side-effects. The other possibility is that modifying the counting index could also result in a negative index based on how WorkOffset is defined. A corollary conclusion to take away from this example is that it is not good practice to modify the counter in the middle of an active loop.

If a developer was stubborn and wanted to keep the integer type for their index, the loop terminator test should be written to protect from spurious negative values from corrupting the data:


for (int index = 0; 
index < maxCount && index >= 0; 

Improved Approach

Since the indexing into the data array will always be positive, why not simply choose a type that is unsigned?! This will explicitly enforce the invariant > 0. Unless an explicit test is put in place to enforce a desired invariant, at best, it can only remain an assumption that the invariant will hold true. Assumptions leave the door open for risk to turn into problems. Here is a better definition of the loop above that now uses an unsigned integer type:


size_t maxCount = max;
for (size_t index = 0; 
index < maxCount; 

For this loop, I have chosen the size_t type, which is defined in many header files, the most common is <cstddef>. The type, size_t, represents the largest addressable number on your compiled platform. Therefore if you have a 32-bit address space, size_t will be a 32-bit number. This type is also just a typedef, an alias, for whatever type is required to make it the appropriate size for your platform. This type is a portable way to specify sizes and use numbers that will succeed on different platforms. The very act of using this type also declares your intent to have a count or size of some item. While I was using the variable name index in the loop examples above, that only indicates what the variable is. The type size_t gives an extra bit of context information to indicate what can be expected from the use of the index.

Use a Meaningful Name for Types

Let's elaborate on the Improved Approach from the previous section. Let's consider the size_t type for just a moment. It's true purpose is to provide portability across platforms by defining a type based on the address size of the platform. However, we found a new use for it, represent any type of variable that is a count of size of something. This should also be considered a valid reason for declaring a new type. Consider this list of variables declared somewhere in a function. Is it immediately clear what they might be used for? Are all of the types chosen correctly?


long  src;
short src_id;
long  dest;
short dest_id;
char* pBuffer;
int   length;
int   index;
long  bytesRead;

Here is a simple example for how to add extra context to a set of variables that could easily be lost in the mix. Consider IP addresses and port id pairs. I have seen both of these variables be called a number of different names. In some cases, it seems just convenient to reassign a value to a variable and use that variable in a function call that makes no sense. This adds to the confusion that is experienced when trying to understand a block of logic. To prevent this sort of abuse, make the types seem special.


// Generally an IPv4 address will be 
// placed into a 32-bit unsigned integer.
// Try this for those situations:
typedef uint32_t         ip_addr_t;
// Similarly, port ids are placed 
// in unsigned 16-bit integers.
typedef uint16_t         port_t;

Now when this block of code is encountered, it may help keep control of what your variables are used for, and how they are used. Here is an example of a jumbled block of code with the new types:


ip_addr_t  src;
port_t     src_id;
ip_addr_t  dest;
port_t     dest_id;
char*      pBuffer;
size_t     length;
size_t     index;
long       bytesRead;

Simplify Template Types

I recently wrote an entry on Template Meta-Programming, and I have been pleasantly surprised how well it has been received. This includes the number of times the article has been read. Up until recently, I was starting to believe that developers had an aversion to the angle brackets < >. I know from historical experience that the first introduction of templates was not as smooth and portable as desired. Over the years, the designers of C++ have recognized how templates could be improved. Now they are an indispensable part of C++.

There is still one thing that seems to vex many developers, the damn < >. There is a very simple solution to tuck those away and still benefit from the generality and power of templates, give them an alias with typedef.


> // Need to add template syntax highlighting
typedef std::vector<int>            IntVector;
typedef std::map<int, std::string>  StringMap;

Unfortunately, it is not possible to typedef partially specialized templates until C++11 and greater. I am very grateful for this feature in the new specification of the language. Up until this point, it has not been possible to create simplified typedef aliases for partially specified templates such as this:


template <typename T, typename U, size_t SizeT>
class CompoundArray;
// This syntax is illegal in C++03
template <typename T>
typedef CompoundArray<t , int, 10>  CompoundIntArray;

The new standard now allows this alias to be defined with the using keyword for the template above to simplify usage:


// The way to define a template alias with C++11
template <typename T>
using CompoundIntArray = CompoundArray<t , int, 10>

// The new usage:
CompoundIntArray<double>  object;

// Creates an object instance equivalent to:
CompoundArray<double , int, 10> object;


Continuous development in software is a cumulative effort. Every change that is made to the software is built upon the exist set of code. If the code is difficult to understand and modify to begin with, chances are that it will only continue to get worse. One way to simply and clarify your intentions is by choosing both meaning types and names for the variables that you use. We have been primarily taught to use variables to communicate intent. In this article, I showed how it is possible to use the type system and the typedef operator to create aliases for your variable types. This gives you another tool to write clean and maintainable code.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paul M Watt
United States United States
I am a software architect and I have been developing software for nearly two decades. Over the years I have learned to value maintainable solutions first. This has allowed me to adapt my projects to meet the challenges that inevitably appear during development. I use the most beneficial short-term achievements to drive the software I develop towards a long-term vision.

C++ is my strongest language. However, I have also used x86 ASM, ARM ASM, C, C#, JAVA, Python, and JavaScript to solve programming problems. I have worked in a variety of industries throughout my career, which include:
• Manufacturing
• Consumer Products
• Virtualization
• Computer Infrastructure Management
• DoD Contracting

My experience spans these hardware types and operating systems:
• Desktop
o Windows (Full-stack: GUI, Application, Service, Kernel Driver)
o Linux (Application, Daemon)
• Mobile Devices
o Windows CE / Windows Phone
o Linux
• Embedded Devices
o VxWorks (RTOS)
o Greenhills Linux
o Embedded Windows XP

I am a Mentor and frequent contributor to with tutorial articles that teach others about the inner workings of the Windows APIs.

I am the creator of an open source project on GitHub called Alchemy[^], which is an open-source compile-time data serialization library.

I maintain my own repository and blog at[^], because code maintenance does not have to be a living hell.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Eric M Zhu24-Feb-14 3:13
memberEric M Zhu24-Feb-14 3:13 
QuestionMy Volte of 5 Pin
kanalbrummer16-Feb-14 23:47
memberkanalbrummer16-Feb-14 23:47 
AnswerRe: My Volte of 5 Pin
Paul Watt17-Feb-14 13:56
mentorPaul Watt17-Feb-14 13:56 
GeneralMore fun with typedef. Pin
Eric A. Carter10-Feb-14 6:56
memberEric A. Carter10-Feb-14 6:56 
First up, great article; the use of typedef to define types should be a no-brainer, but a lot of C++ code I have seen has clearly missed the point and been very difficult to comprehend as a result, not to mention issues associated with 'invalid' types. TLDR, the following may also be no-brainer material for many, but I thought I'd share some of my own typedef-ining fun.

In my own code I've tended to take it further by defining complete class/struct templates and using typedef to predefine the commonly-used flavours; the (somewhat abridged) example below is for one of a set of four vertex structs I use for defining vertices passed to OpenGL (amongst other things):
// Predefine the templates so each can see the other:
template <typename T,typename OT> struct VTX1_S;
template <typename T,typename OT> struct VTX1N_S;
template <typename T,typename OT> struct VTX2_S;
template <typename T,typename OT> struct VTX2N_S;
// Now each template is defined in full like so (note I've removed a lot here for brevity):
template <typename T,typename OT> struct VTX2N_S
  typedef VEC2T<T> V2; typedef VEC3T<T> V3; //...
  V3 org; V2 uvc[2]; V3 nrm;
  inline static UINT UVCOUNT(void) { return 2; }
  inline static bool NORMAL(void) { return true; }
  inline static UINT PRECISION(void) { return (sizeof(T)==4)?1:(sizeof(T)==8)?2:0; }
  VTX2N_S(const VTX1_S<T,OT> &vtx) { /*...*/ }
  VTX2N_S(const VTX1N_S<T,OT> &vtx) { /*...*/ }
  VTX2N_S(const VTX2_S<T,OT> &vtx) { /*...*/ }
  VTX2N_S(const VTX2N_S<T,OT> &vtx) { /*...*/ }
  VTX2N_S(const VTX1_S<OT,T> &vtx) { /*...*/ }
  VTX2N_S(const VTX1N_S<OT,T> &vtx) { /*...*/ }
  VTX2N_S(const VTX2_S<OT,T> &vtx) { /*...*/ }
  VTX2N_S(const VTX2N_S<OT,T> &vtx) { /*...*/ }
// Finally, define the real types:
typedef VTX2N_S<double,float> VTX2ND;
typedef VTX2N_S<float,double> VTX2NF;

With this I can create complex vertex definitions which transparently convert between the various types (including between double/float versions) with everything effectively 'hidden' behind typedef'd names, whilst my (possibly oddball) naming convention assures I know exactly what the type is on sight; VTX1F is clearly not the same type as VTX2ND, but either can at least initialise the other (using default values where required). Note the use of scoped typedefs within the template to ensure readability; in fact even UINT is a typedef in case it ever needs to be changed in future.

Even as a purely hobbyist coder, the power, versatility and readability of typedef'd templates has been a huge part of creating and maintaining code of a quality I am happy with.
Ooh, hey up, I've got some code for summat like that I wrote about 20 years ago...somewhere... [rifles through boxes of 3.5" disks and ancient HDDs]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 10 Feb 2014
Article Copyright 2014 by Paul M Watt
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid