The Psychology of Quality and More |
CHAPTER 9 : Data Usage
9.3 Using 'typedef'Many Pascal programmers coming to C have bemoaned its lack of typing. typedef, to some extent, corrects this situation, allowing more explicitness in the declaration of variables and more understandability of the code. 9.3.1 Typing variablesWhere a variable has a clear purpose, it is easy to assign a type to it:
typedef WEEKDAY unsigned int;
A general philosophy of typedef-ing variables, can make the intent of data usage clearer, although when the typedef is not within visual scope (for example in a header file), there can be a danger of misunderstanding and incorrect use, such as a variable being typedef'd as an unsigned char, but then being assigned a number greater than 255. Over-use of typedef's can also result in reduced readability, as there are just too many different types to remember. For example, it would probably be going too far to declare:
typedef LOOPCOUNT unsigned int; ----------------------------------------------------------------- A compromise is to restrict the use of typedef's to a manageable set of common types used in the program. 9.3.2 Typing numbersAnother common use of typedef is to aid portability by redefining numbers:
typedef INT8 signed char;
Note that the numbers do not define actual integer size. On most computers char is at least 8 bits, int is at least 16 bits and long is at least 32 bits. signed and unsigned are used to explicitly show the importance of the signedness (it is necessary with char). Note that this cannot be used for all integers, for example some standard library functions return an int, which may be of different sizes on different machines. In this case, it is more portable to not use typedef. Boolean variables are widely used, and it is worth including a typedef (also see 9.10.3):
typedef BOOL int;
9.3.3 'typedef' vs. '#define'It is possible to replace typedef with #define in many situations:
typedef WEEK int; ..or.. #define WEEK int
Consider the underlying message: Using typedef is saying, "Weekday is of type WEEK, which happens to be represented by an int," whilst using #define is saying, "Weekday is an int." Using typedef is thus being more explicit. Using #define can also result in some nasty problems:
#define WEEKPTR int *
There are, occasionally, good reasons for using #define, such as where void is reserved, but not implemented, identifier (in which case typedef would cause an error). 9.3.4 'typedef' vs. 'struct Tag'Structures may be defined using tags:
struct MESSAGE
This explicitly informs the reader that a structure is being used. On the other hand, it is also being explicit to use the typing paradigm, in which case a typedef is more appropriate:
typedef struct
The only time that a structure tag must be used is in recursive references:
struct NODE
|
Site Menu |
Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive | |
And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact | |
Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate | |
You can buy books here |
And the big |