Thread: What would be the best way to name these typedefs?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    What would be the best way to name these typedefs?

    Hello,
    I have some complicated typedefs defined inside my "C_html4_elements" class.
    I was wondering if anyone has any advice on the best names these types can have.

    Here is what the typedefs currently look like:

    Code:
    typedef pair<string, double> PR_str_double_T;
    typedef vector<string> VECTOR_str_T;
    typedef bool BOOL_required_T;
    typedef bool BOOL_deprecated_T;
    typedef pair<BOOL_required_T, BOOL_deprecated_T> PR_BOOL_required_T_BOOL_deprecated_T_T;
    typedef pair<VECTOR_str_T, PR_BOOL_required_T_BOOL_deprecated_T_T> PR_VECTOR_str_T_PR_BOOL_required_T_PR_BOOL_deprecated_T_T_T;
    Any suggestions are welcome. Thanks.
    Last edited by Programmer_P; 02-20-2011 at 10:14 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Name them after what they're going to be used for instead of the types?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Name them after what they're going to be used for instead of the types?
    Well, the problem with that approach is some of them are going to be used for multiple things, so I can hardly write every single thing down, as the names would be too long. But note that for "BOOL_required_T" and "BOOL_deprecated_T", I did a combination of both.
    Last edited by Programmer_P; 02-20-2011 at 10:16 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But the problem is that you're not going to understand WTH those typedefs are anyway. Typedefs are better named for what they're going to be used for.
    Make multiple typedefs if necessary. Clarity before obscurity.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    Well, the problem with that approach is some of them are going to be used for multiple things, so I can hardly write every single thing down, as the names would be too long.
    In that case do not bother with the typedefs at all, other than as a convenience to shorten the names. Otherwise, have a typedef for each use, even if you say, end up with ten typedefs for bool.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    But the problem is that you're not going to understand WTH those typedefs are anyway. Typedefs are better named for what they're going to be used for.
    Make multiple typedefs if necessary. Clarity before obscurity.
    Hmm...I suppose you're right. Of course, if I had continued to use the above approach, I would have documented my naming convention for users of the class, so it wouldn't have been too big of a deal, I don't think.

    But, I decided to change the above typedefs to this:

    Code:
    typedef pair<string, double> browser_and_version_T;
    typedef bool required_T;
    typedef bool deprecated_T;
    typedef pair<required_T, deprecated_T> required_and_deprecated_T;
    typedef vector<string> list_of_strings_T;
    typedef pair<list_of_strings_T, required_and_deprecated_T> list_of_strings_AND_required_and_deprecated_T;
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's nice of you to name them after what they contain, but this is really obfuscation of the real types. These typedefs add nothing new.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    In that case do not bother with the typedefs at all, other than as a convenience to shorten the names. Otherwise, have a typedef for each use, even if you say, end up with ten typedefs for bool.
    Well, I figured out a way to use Elysia's approach. I used the generic name "list_of_strings_T" for the vector<string> typedef. That was the one I need for multiple things.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    It's nice of you to name them after what they contain, but this is really obfuscation of the real types. These typedefs add nothing new.
    If you're referring to the two bool typedefs, I used typedefs because I wanted to be able to easily tell which side of the pair object inside the map<string, NAME_OF_PAIR_TYPE> for the attrs returned by the member functions of the class contained the "required" bool value and which one contained the "deprecated" bool value.

    But if you have a better approach to do the above, by all means, speak up.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That generic name really adds little value. It's easier, and better, to type std::vector<std::string> rather than list_of_strings_T (see how similar they are?).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    If you're referring to the two bool typedefs, I used typedefs because I wanted to be able to easily tell which side of the pair object inside the map<string, NAME_OF_PAIR> for the attrs returned by the member functions of the class contained the "required" bool value and which one contained the "deprecated" bool value.

    But if you have a better approach to do the above, by all means, speak up.
    You might be able to write:
    Code:
    struct Status {
        bool required;
        bool deprecated;
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    That generic name really adds little value. It's easier, and better, to type std::vector<std::string> rather than list_of_strings_T (see how similar they are?).
    That's great, until you do something insane, and need an iterator or something else stupid for your heavily templated user type, in a map of string vector pairs.

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    That generic name really adds little value. It's easier, and better, to type std::vector<std::string> rather than list_of_strings_T (see how similar they are?).
    Well, I made a typedef for that one because originally I attempted to have the attrs stored inside a map<string, vector<string>> object (the string side is for a browser string key, so that I could retrieve a different list of supported attrs based on a particular browser), but the compiler gave out some kind of error, so I changed "vector<string>" to a "vector_str_t", and then after creating this thread, as per your suggestions, I changed it to a "list_of_strings_T".
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    That's great, until you do something insane, and need an iterator or something else stupid for your heavily templated user type, in a map of string vector pairs.
    I did not say avoid typedefs. I said give them a meaningful name.
    As a comparison:

    std::vector<std::string>
    list_of_strings_T

    This will not shorten the amount of typing a lot, in your heavily templated code.

    Quote Originally Posted by Programmer_P View Post
    ...and then after creating this thread, as per your suggestions, I changed it to a "list_of_strings_T".
    My suggestion was to give it a meaningful name, ie a name that goes with its usage. Where do you use it? For what purpose do you use it? Use that in your typedef.
    Last edited by Elysia; 02-20-2011 at 10:39 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    Well, I made a typedef for that one because originally I attempted to have the attrs stored inside a map<string, vector<string>> object (the string side is for a browser string key, so that I could retrieve a different list of supported attrs based on a particular browser), but the compiler gave out some kind of error, so I changed "vector<string>" to a "vector_str_t", and then after creating this thread, as per your suggestions, I changed it to a "list_of_strings_T".
    I guess that you are not mapping required to deprecated, so my Status aggregate struct idea could work. If so, and using my personal preference for naming style, I might suggest:
    Code:
    typedef pair<string, double> BrowserVersionPair;
    
    struct Status {
        bool required;
        bool deprecated;
    };
    
    typedef vector<string> AttributeList;
    typedef pair<AttributeList, Status> AttributeListStatusPair;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows Typedef's
    By valaris in forum Windows Programming
    Replies: 2
    Last Post: 04-25-2009, 09:32 AM
  2. pointer to function and typedefs
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2006, 07:37 AM
  3. Standard typedefs ...
    By twomers in forum C++ Programming
    Replies: 6
    Last Post: 02-16-2006, 10:07 AM
  4. how to use typedefs with structs?
    By Yourhighness in forum C Programming
    Replies: 9
    Last Post: 06-03-2003, 04:43 AM
  5. Using TypeDefs
    By peking1983 in forum C Programming
    Replies: 2
    Last Post: 03-14-2003, 01:26 AM