Hi,
I recently started using typedefs and am curious if they are ever
considered evil.
In particular, I'm thinking of a situation where a typedef is used in
a file but defined in another, included file. Someone reading the
code might have to track down the original definition to make sense of
the code, and thus it reduces readability.
I didn't find anything int the C++ FAQ denouncing typedefs, though, so
I'm guessing they're not considered bad form. Anyone have thoughts on
the subject?
Thanks,
cpp 11 1933
cppaddict wrote: I recently started using typedefs and am curious if they are ever considered evil.
They are good. Code should follow Don't Repeat Yourself. That rule's most
important for behavior (the stuff inside {} on methods). Consider types as
structure, which is less important to not duplicate, but every little bit
helps. For example:
typedef std::map<string, string> params_t;
bool
Product(params_t & testCase)
{
double num (strtod(testCase["Input1"].c_str(), NULL));
double den (strtod(testCase["Input2"].c_str(), NULL));
double quo (strtod(testCase["Product()" ].c_str(), NULL));
return num * den == quo;
}
I'm about to fold the lines that generally say double quo
(strtod(testCase["Product()" ].c_str(), NULL)); into a single reuseful
function. When I do, the typedef params_t will appear in its arguments. This
lets us upgrade params_t in only one place, and not write all the
std::map<etc etc> all over the place.
Further, the exact point where a template instantiates into a class tends to
conjoin that template with the types seen at the instantiation point.
Repeating std::map<etc etc> all over the place could generate subtle
differences between each instance, so instantiating with a typedef reduces
this risk, and instantiating in a header reduces it more. (It can't
eliminate it though, because users could #include their headers in any
order.)
In particular, I'm thinking of a situation where a typedef is used in a file but defined in another, included file. Someone reading the code might have to track down the original definition to make sense of the code, and thus it reduces readability.
There are many ways to reduce readability by stretching out modules, so
their declarations are far away from their definitions, both physically and
cognitively far. To promote readability, write simple code in short methods,
and give everything the narrowest scope possible. If another module
somewhere needs a std::map<string, string>, I would _not_ put params_t into
a header that both its modules can see. I would write another typedef in
that module, with a different name.
Naming things after what their intent always helps readability. params_t
intends to store parameters to methods, as maps of strings. Another module
would name their typedef differently.
To help navigating even this short distance, with VC++, single-click on
params_t and tap <F12>.
--
Phlip http://industrialxp.org/community/bi...UserInterfaces
cppaddict wrote: I recently started using typedefs and am curious if they are ever considered evil.
In particular, I'm thinking of a situation where a typedef is used in a file but defined in another, included file. Someone reading the code might have to track down the original definition to make sense of the code, and thus it reduces readability.
I didn't find anything int the C++ FAQ denouncing typedefs, though, so I'm guessing they're not considered bad form. Anyone have thoughts on the subject?
Typedefs are as important in C++ as abbreviations in English. They
add about as much confusion, as well. However, the most important
part (I believe) relates to the use of certain aliases in the Standard
Library, and I mostly refer to such typedefs like 'value_type',
'difference_type', 'iterator_category' for iterators, for example.
Without them there would be no clean interface between template functions
in the Library and custom iterators designed by other people. The same
goes for 'value_type' for containers, 'result_type' for functions and
so on.
Victor
Philip,
Thanks for your reply. I'd like clarification on one point, if
possible... If another module somewhere needs a std::map<string, string>, I would _not_ put params_t into a header that both its modules can see. I would write another typedef in that module, with a different name.
Or even the same name. I just tried redefining a typedef when the
same one had already been defined, and it compiled without errors.
Doing this would be one way to resolve my initial concern. Or is that
evil? what do you think?
Thanks,
cpp Without them there would be no clean interface between template functions in the Library and custom iterators designed by other people.
Victor,
Thanks for your reply. I'm intrigued by the above statement, but
don't really understand it. Could you elaborate?
Thanks,
cpp
cppaddict wrote: Without them there would be no clean interface between template functions in the Library and custom iterators designed by other people.
Victor,
Thanks for your reply. I'm intrigued by the above statement, but don't really understand it. Could you elaborate?
If you write a custom iterator that is supposed to be used in some
kind of library function that expects a, say, forward iterator, and
makes use of what the iterator refers to, then you have to define
a special type in your iterator class that would have the name (or
an alias) 'value_type'. You will of course adhere to other rules
as well, like defining operator++(int) for your iterator class, etc.
You could solve the need to define 'value_type' as a nested type,
or, especially if you have your iterator a template as well, you
might want to have 'value_type' an _alias_ for something else, like
template<typename T> class myiterator {
typedef T value_type;
....
};
Now, if there were no typedefs, the way to define 'value_type' to
be a member of 'myiteratore' would be to actually name the argument
that:
template<typename value_type> class myiterator {
....
};
That's fine for a generic template. What if you need to specialise
your 'myiterator' for, say, "int"?
template<> class myiterator<int> {
....
};
Now, it doesn't have "value_type" member any longer. And there is
no way to introduce "value_type" for 'myiterator<int>' specialisation
without a typedef:
template<> class myiterator<int> {
typedef int value_type;
....
};
Victor
cppaddict wrote: Philip,
Thanks for your reply. I'd like clarification on one point, if possible...
If another module somewhere needs a std::map<string, string>, I would _not_ put params_t into a header that both its modules can see. I would write another typedef in that module, with a different name.
Or even the same name. I just tried redefining a typedef when the same one had already been defined, and it compiled without errors.
That I don't think I understand (or even believe).
typedef int INT;
typedef unsigned INT; // will cause error
Doing this would be one way to resolve my initial concern. Or is that evil? what do you think?
Victor typedef int INT; typedef unsigned INT; // will cause error
I should have been more clear. What I did was:
typedef int INT;
typedef int INT;
Only makes sense, of course, when the statements are in different
files. Would that be evil?
Thanks,
cpp
cppaddict wrote: typedef int INT; typedef unsigned INT; // will cause error
I should have been more clear. What I did was:
typedef int INT; typedef int INT;
Only makes sense, of course, when the statements are in different files. Would that be evil?
Evil, no. If it's allowed by the Standard, how can it be evil?
Redundant, yes. The problem comes when in one file you change
it to something and forget to change in the other. Then somebody
else will never know which one is the intended type (without
looking at the version control system and seeing when the change
was made, or without talking to the person who made the change).
V
cppaddict wrote: typedef int INT; typedef unsigned INT; // will cause error
I should have been more clear. What I did was:
typedef int INT; typedef int INT;
Only makes sense, of course, when the statements are in different files. Would that be evil?
Not using include guards would be non-good.
BTW please read /The C++ Programming Language/, and /{Design & Evolution |
Ruminations | Exceptional | Effective | Accelerated} C++/ for extra credit
here...
--
Phlip http://industrialxp.org/community/bi...UserInterfaces
cppaddict wrote: Hi,
I recently started using typedefs and am curious if they are ever considered evil.
In particular, I'm thinking of a situation where a typedef is used in a file but defined in another, included file. Someone reading the code might have to track down the original definition to make sense of the code, and thus it reduces readability.
I didn't find anything int the C++ FAQ denouncing typedefs, though, so I'm guessing they're not considered bad form. Anyone have thoughts on the subject?
Thanks, cpp
typedef double INT ;
That would be fairly evil.
Alan
cppaddict posted: In particular, I'm thinking of a situation where a typedef is used in a file but defined in another, included file. Someone reading the code might have to track down the original definition to make sense of the code, and thus it reduces readability.
As is exactly the case with:
class Monkey
struct Ape
union Primate
typedef Mammal
-JKop This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: joe |
last post by:
hi,
after reading some articles and faq,
i want to clarify myself what's correct(conform to standard) and
what's not?
or what should be correct but it isn't simply because compilers don't...
|
by: Chris |
last post by:
I've lurked around long enough... Time to interract =)
I'm trying to make sense of the following. I can't quite wrap my head
around what this is actually doing:
-------------
typedef enum {...
|
by: Merrill & Michele |
last post by:
typedef struct
{
WORD versionNumber;
WORD offset;
}
MENUITEMTEMPLATEHEADER;
This is from vol 5 of unnamed platform's programmer's reference. I could
make this conforming by enclosing...
|
by: burn |
last post by:
Hello,
i am writing a program under linux in c and compile my code with make
and gcc.
Now i have 4 files: init.c/h and packets.c/h. Each header-file contains
some:
init.h:
struct xyz
{
|
by: Peter O'Reilly |
last post by:
The news of Nick Berg is shocking enough. Having his video released and
made widely available, in its _totality, including the decapitation, makes
those web site operators accomplices in this...
|
by: vvv |
last post by:
Hi All,
Do we have anything in .NET which is equivalent to C++'s Typedef .
Regards,
Vasanth
|
by: Alex |
last post by:
Hello people,
I am getting errors from VS2003 when working with typedef'ed types.
For example, assume that I have a type T, defined in a 3rd party include file based on some condition
#if...
|
by: Ian Bush |
last post by:
Hi All,
I'm a bit confused by the following which is causing one of
our user's codes fail in compilation:
typedef struct SctpDest_S;
1) Is this standard ?
2) If so ( or even if not so ! )...
|
by: Immortalist |
last post by:
Various aquisition devices that guide learning along particular
pathways towards human biases. And as E.O. Wilson might say mental
development appears to be genetically constrained.
(1) Language...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |