typedef every where

G

Gary Wessle

Hi
my toy project is spanning few files already, typing is becoming an
issue, is it acceptable practice to do the following globally?
typedef vector<double> vd;
typedef vector<pair<char,double> > vpcd;

thanks
 
P

Phlip

Gary said:
my toy project is spanning few files already, typing is becoming an
issue, is it acceptable practice to do the following globally?
typedef vector<double> vd;
typedef vector<pair<char,double> > vpcd;

Name the typedef, like any identifier, after its intent. Don't fold typedefs
together just because their internal types are the same. To whit:

typedef std::vector<double> shoeSizes_t;
typedef std::vector<double> seismograph_t;

And put them inside the class or namespace that uses them.
 
H

Howard

Gary Wessle said:
Hi
my toy project is spanning few files already, typing is becoming an
issue, is it acceptable practice to do the following globally?
typedef vector<double> vd;
typedef vector<pair<char,double> > vpcd;

Depends on who it needs to be acceptable to, I guess. :)

One issue I'd have is that those names are completely unintelligible unless
you know in advance what they mean. Come back next year, and you'll wonder
"what the heck is a 'vpcd'?".

Something like DoubleVector and CharDoublePairVector might be more
descriptive. Of course, if all you're trying to gain is less typing, then
that's not saving much. But I rarely base decisions on how much or little
I'll have to type; instead, I go for clarity.

Also, a vector of recorded race times and a vector of space-time coordinates
(for example) may each be a vector of doubles, but wouldn't it make more
sense to have (say) a RaceTimesVector type and a CoordinatesVector type?
It's like a class: you don't call your class "csi" simply because it's a
class containing a string and an int. You name it what it is, something
more like "Competitor" or "PlanetDescription".

-Howard
 
R

Roland Pibinger

Hi
my toy project is spanning few files already, typing is becoming an
issue, is it acceptable practice to do the following globally?
typedef vector<double> vd;
typedef vector<pair<char,double> > vpcd;

Using typedefs to shorten names is IMHO bad style. You just lose the
type information.

Best wishes,
Roland Pibinger
 
P

Phlip

Roland said:
Using typedefs to shorten names is IMHO bad style. You just lose the
type information.

Typedeffing template instantiations is excellent style. Not only does it
convert the type information (that the compiler will enforce anyway) into a
meaningful name, but it also defines the template's instatiation point. Such
a typedef is more than just an alias; it also locks in all the identifiers
that the template definitions see.
 
H

Howard

Phlip said:
Typedeffing template instantiations is excellent style. Not only does it
convert the type information (that the compiler will enforce anyway) into
a meaningful name, but it also defines the template's instatiation point.
Such a typedef is more than just an alias; it also locks in all the
identifiers that the template definitions see.

Provided the name actually _is_ meaningful. And 'vpcd' definitely is not.
:)

-Howard
 
R

Roland Pibinger

Typedeffing template instantiations is excellent style. Not only does it
convert the type information (that the compiler will enforce anyway) into a
meaningful name,

I beg to differ. 'vector<double>' is certainly more meningful than
'vd'. Typdedefs make sense when they have a conceptual purpose, eg.
the typedefs for iterators in STL containers, not to merely 'shorten'
names.
but it also defines the template's instatiation point.
??

Such
a typedef is more than just an alias; it also locks in all the identifiers
that the template definitions see.

I'm not familiar with all atrocities of the C++ Standard but I guess a
typedef doesn't 'lock in' more than a (forward) declaration (otherwise
it would even be an argument against 'typedef everywhere').

Best wishes,
Roland Pibinger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top