Dr. Dobb's Journal - February 2009 - (Page 38) State of the Art DECOUPLING C HEADER FILES The solution lies in C’s distinction between declarations and definitions discussion, the important thing to keep in mind is that a typedef is simply an alias, so it may be used interchangeably with the structure name. Using this equivalence, you rewrite the SMTP header from Listing One to Listing Two. C allows multiple declarations of a structure, but it can only be defined once. This is true even if all the definitions are exactly identical. So if you have two separate header files that define the same named structure and both are included in a single translation unit, the compiler will issue an error; see Listing Four. But change one or both of the definitions to declarations and the problem goes away (Listing Five). While C limits what can be done with declared but undefined structures, C does let you use pointers to undefined structures (although such pointers cannot be dereferenced). Combining that with the ability to declare the same structure multiple times lets you further rewrite the SMTP header. Once a structure declaration has been added to the SMTP header file, there is no further need to include the LTDS header; see Listing Three. The SMTP header can now be included with or without the LTDS header file. Common Problems And Solutions When transitioning source code to use such a technique, there are some common problems you may run into. The first is anonymous structures that only have a typedef name. This is usually a simple problem to solve—simply give the structure a name; see Listing Six. Since the structure was only referred to by its typedef name, adding a name should not cause any backwards compatibility problems. The one issue that could arise when giving a structure a name is that some other structure may already be using that name. Hopefully, the other structure is a copy of the structure that you are adding a name to and can be merged. But in the case where you have two completely unrelated structures where the typedef name of one is practically identical to the structure name of the other, I suggest changing them to more unique names to prevent confusion. Another issue that arises is when a structure already has both a name and a typedef name that are very different. From the compiler’s point of view, this is perfectly legal, although it can be hard for developers to remember that two distinct names are actually the same type (and will definitely cause developers to curse at the screen as they try to learn such an API). The best recommendation here is to refrain from such a practice in the future. In general, I highly recommend using the same or similar names for both the structure and the typedef name. While this is not required, the easier you make it for developers to be able to derive the typedef name from the structure name and vice versa, the easier it is to apply the transformations required by this technique. P.O. Box 42363 Portland, OR 97242 USA TEL 503-802-0810 FAX 206-309-0304 E-mail: sales@abxsoft.com Conclusion That’s all there is to it. While the benefits are not huge, the technique is so easy to apply it quickly becomes second nature. DDJ 38 Dr. Dobb’s Journal l www.ddj.com l February 2009 http://www.abxsoft.com http://www.abxsoft.com http://www.ddj.com
Table of Contents Feed for the Digital Edition of Dr. Dobb's Journal - February 2009 Dr. Dobb's Journal - February 2009 Contents Friday Night Fish Fry Alia Vox Developer Diaries Conversations Computing in the Clouds Software Development in the Cloud Videos and Oracle Forms 10g Parallel LINQ Decoupling C Header Files Effective Concurrency Disciplined Agility Swaine’s Flames Dr. Dobb's Journal - February 2009 Dr. Dobb's Journal - February 2009 - (Page BB1) Dr. Dobb's Journal - February 2009 - (Page BB2) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page Cover1) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page Cover2) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page 1) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page 2) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page 3) Dr. Dobb's Journal - February 2009 - Contents (Page 4) Dr. Dobb's Journal - February 2009 - Contents (Page 5) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 6) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 7) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 8) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 9) Dr. Dobb's Journal - February 2009 - Alia Vox (Page 10) Dr. Dobb's Journal - February 2009 - Alia Vox (Page 11) Dr. Dobb's Journal - February 2009 - Developer Diaries (Page 12) Dr. Dobb's Journal - February 2009 - Developer Diaries (Page 13) Dr. Dobb's Journal - February 2009 - Conversations (Page 14) Dr. Dobb's Journal - February 2009 - Conversations (Page 15) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 16) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 17) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 18) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 19) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 20) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 21) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 22) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 23) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 24) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 25) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 26) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 27) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 28) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 29) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 30) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 31) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 32) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 33) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 34) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 35) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 36) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 37) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 38) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 39) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 40) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 41) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 42) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 43) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 44) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 45) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 46) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 47) Dr. Dobb's Journal - February 2009 - Swaine’s Flames (Page 48) Dr. Dobb's Journal - February 2009 - Swaine’s Flames (Page Cover3) Dr. Dobb's Journal - February 2009 - Swaine’s Flames (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.