How To Name Enumerated Types and Structs

I

Immortal Nephi

I do not find good naming specific types of data. Sometimes, two
variables have same name. I cannot determine which variable name is
to be from either enum or struct. I have to add prefix before
variable name.
The variable name will be EColor and SColor. I should name
ColorTypes in enum and Color in struct.
Option 1 shows prefix Color_ on each color name. Option 2 is
similar, but shows prefix e_. Option 3 may be better. Also, compare
option 4 and option 5 in struct.
Please tell me what you are suggesting how to name variable.

// Option 1
enum Color { // or enum ColorTypes
Color_Black,
Color_Gray,
Color_Write
};

Color color_t = Color_White;


// Option 2
enum Color {
e_Black,
e_Gray,
e_Write
};

Color color_t = e_White;
or
Color color_t = Color::e_White;


// Option 3
enum Color {
Black,
Gray,
Write
};

Color color_t = Color::White;


// Option 4
struct Color {
static const int Black = 0;
static const int Gray = 1;
static const int White = 2;
};

int selColor = Color::Black;


// Option 5
struct Color {
static const int s_Black = 0;
static const int s_Gray = 1;
static const int s_White = 2;
};

int selColor = Color::s_Black;


I want to name my own data types. How do I name keyword in typedef?
I can’t tell which is non-const or const.

typedef unsigned int color_t;
typedef const color_t COLOR_T;

color_t red = 5;
COLOR_T fixedWhite = 10;

I know which typedef I name is non-const and const.
 
J

Jonathan Lee

        I do not find good naming specific types of data.  Sometimes, two
variables have same name.  I cannot determine which variable name is
to be from either enum or struct.  I have to add prefix before
variable name.
        The variable name will be EColor and SColor.  I should name
ColorTypes in enum and Color in struct.
        Option 1 shows prefix Color_ on each color name.  Option 2 is
similar, but shows prefix e_.  Option 3 may be better.  Also, compare
option 4 and option 5 in struct.
        Please tell me what you are suggesting how to name variable.

For enums, my usual pattern is to throw the enum in a related class.
If no such class is available, then a namespace. The class/namespace
then act as a prefix of sorts. ex.,

namespace Tshirt {
enum Colour { White, Black, Red };
}

Tshirt::Colour col = Tshirt::Red;
        I want to name my own data types.  How do I name keyword in typedef?
I can’t tell which is non-const or const.

typedef unsigned int color_t;
typedef const color_t COLOR_T;

For me, I usually just prefix with a "c_". So, often I'll do this:

typedef std::vector<double>::const_iterator c_iterator;

It suggests to me the "cv_" notation often found in the standard.

--Jonathan
 
Ö

Öö Tiib

        I do not find good naming specific types of data.  Sometimes, two
variables have same name.  I cannot determine which variable name is
to be from either enum or struct.  I have to add prefix before
variable name.

        The variable name will be EColor and SColor.  I should name
ColorTypes in enum and Color in struct.

There you are probably sailing in bad water. Agreement how to name
things can be reached within smaller group. No way you can reach
universal agreement for all C++ developers.

Enumerators are dangerous since these secretly (commonly called
"implicitly") convert into int constants in most situations. If you
use enum somewhere for something else but for convenient way to name a
pile of (int) values then you are doing something that is likely error-
prone.

Int values have no global meaning. The meaning matters in context of
some class or namespace. It is good idea to always define the enum
inside that class or namespace where its enumerators have meaning.

Plural names like "ColorTypes" or "Color::Types" indicate container
types in naming convention i use, so i avoid such for enum.
        Option 1 shows prefix Color_ on each color name.  Option 2 is
similar, but shows prefix e_.  Option 3 may be better.  Also, compare
option 4 and option 5 in struct.
        Please tell me what you are suggesting how to name variable.

// Option 1
enum Color { // or enum ColorTypes
        Color_Black,
        Color_Gray,
        Color_Write

};

Color color_t = Color_White;

// Option 2
enum Color {
        e_Black,
        e_Gray,
        e_Write

};

Color color_t = e_White;
or
Color color_t = Color::e_White;

// Option 3
enum Color {
        Black,
        Gray,
        Write

};

Color color_t = Color::White;

// Option 4
struct Color {
        static const int Black = 0;
        static const int Gray = 1;
        static const int White = 2;

};

int selColor = Color::Black;

// Option 5
struct Color {
        static const int s_Black = 0;
        static const int s_Gray = 1;
        static const int s_White = 2;

};

int selColor = Color::s_Black;


Options 2 and 3 are buggy. Enum does not form something namespace-like
in C++.
I use:
// option 6?
class Color
{
public:
enum Code
{
Abbey = 207,
AbsintheGreen = 120,
AbsintheYellow = 105,
// and so on
};
public:
// ...
private:
// ...
};

// When i need it outside Color class at some place then:
Color::Code colorCode = Color::AbsintheYellow;

Notice that it goes long. That is cool, since it indicates to lazy me
that i am dealing with something that i am probably not supposed to,
it is likely business of Color class.
        I want to name my own data types.  How do I name keyword in typedef?
I can’t tell which is non-const or const.

typedef unsigned int color_t;
typedef const color_t COLOR_T;

color_t red = 5;
COLOR_T fixedWhite = 10;

        I know which typedef I name is non-const and const.

I usually use explicit const keyword itself.

Color::Code const colorCode = Color::AbsintheYellow;

When there is separate immutable and mutable types then i use prefixes
like "Const" or "Immutable" or "Fixed" for immutable. More logical
would be to have immutability as common and mutable types as exception
but C++ is made so that constness needs to be indicated.
 
I

Immortal Nephi

There you are probably sailing in bad water. Agreement how to name
things can be reached within smaller group. No way you can reach
universal agreement for all C++ developers.

Enumerators are dangerous since these secretly (commonly called
"implicitly") convert into int constants in most situations. If you
use enum somewhere for something else but for convenient way to name a
pile of (int) values then you are doing something that is likely error-
prone.

Int values have no global meaning. The meaning matters in context of
some class or namespace. It is good idea to always define the enum
inside that class or namespace where its enumerators have meaning.

Plural names like "ColorTypes" or "Color::Types" indicate container
types in naming convention i use, so i avoid such for enum.

I agree, but you still want to use specialization. Look at the
example below.

template < typename type >
struct Type_Traits {
};

template<>
struct Type_Traits< char > {
};

template<>
struct Type_Traits< short > {
};

template<>
struct Type_Traits< long > {
};

You will want to rename left word “Type” to any name before
“_Traits”. You can insert data members and member functions in each
struct. The specialization is not always enough. The enumerated type
is the option.
You can’t insert enumerated type inside class, but namespace is good
option. You might have 20 different types and have different constant
data values (static const data members).
You can do that below.

enum Data_Types {
char_type,
short_type,
long_type,
// ….
// ….
// ….
// your own type definition
your_type
};

template < typename type >
struct Type_Traits {
};

template<>
struct Type_Traits< char_type > {
};

template<>
struct Type_Traits< short_type > {
};

template<>
struct Type_Traits< long_type > {
};

template<>
struct Type_Traits< your_type > {
};

Please suggest how you write good enumerated type name.

Can be Data_Types::char_type? Or… eCharType

template<>
struct Type_Traits< Data_Types::char_type > {
};

or…

template<>
struct Type_Traits< eCharType > {
};

The readable written code is necessary when you read good name and
you know where name comes from either enumerated type or struct.

Options 2 and 3 are buggy. Enum does not form something namespace-like
in C++.
I use:
 // option 6?
 class Color
 {
 public:
     enum Code
     {
         Abbey = 207,
         AbsintheGreen = 120,
         AbsintheYellow = 105,
         // and so on
     };
 public:
     // ...
 private:
     // ...
 };

 // When i need it outside Color class at some place then:
 Color::Code colorCode = Color::AbsintheYellow;

Notice that it goes long. That is cool, since it indicates to lazy me
that i am dealing with something that i am probably not supposed to,
it is likely business of Color class.







I usually use explicit const keyword itself.

 Color::Code const colorCode = Color::AbsintheYellow;

When there is separate immutable and mutable types then i use prefixes
like "Const" or "Immutable" or "Fixed" for immutable. More logical
would be to have immutability as common and mutable types as exception
but C++ is made so that constness needs to be indicated.- Hide quoted text -


Why?

You can do that below.

typedef char char_type; // inside class or struct
typedef short short_type;
// …

How do I name constant type?

typedef const char_type const_char_type;

typedef const char_type const_chartype;

typedef const char_type c_char_type;

typedef const char_type cchar_type;

Which one of four lines above is good name? Notice two ‘c’
characters in the fourth line above is so confusing. How do you want
to shorten typedef name?

typedef const char_type const_char_t;
or….
typedef const char_type t_const_char;


Always look at prefix ‘t’ as type is easier to read. What do you
suggest good typedef name?

And to add. How do you shorten typedef struct name?

typedef Type_Traits< char_type > Char_Traits;
typedef Type_Traits< short_type > Short_Traits;
typedef Type_Traits< long_type > Long_Traits;
typedef Type_Traits< your_type > YourType_Traits;

Please let me know if these names are good. Sometimes, I don’t find
good naming variable guideline. I need your feedback.
 
Ö

Öö Tiib

        I agree, but you still want to use specialization.  Look at the
example below.

template < typename type >
struct Type_Traits {

};

template<>
struct Type_Traits< char > {

};

template<>
struct Type_Traits< short > {

};

template<>
struct Type_Traits< long > {

};

You mean boost::type_traits (or TR1)? Plural there means that there
are lot of traits in it like:
is_empty
is_pod
has_trivial_constructor
has_trivial_copy

.... and so on, these are not enums but classes, each is a class.
enumerators are not inside enum but inside its surrounding namespace
in C++, enumerators have type of that enum. That is why i say calling
enum with plural is not how i see it.
        You will want to rename left word “Type” to any name before
“_Traits”.  You can insert data members and member functions in each
struct.  The specialization is not always enough.  The enumerated type
is the option.

I did not understand what you are saying here.
        You can’t insert enumerated type inside class, but namespace is good
option.  You might have 20 different types and have different constant
data values (static const data members).
        You can do that below.

enum Data_Types {

I would prefer "Data_Type", but okay.
        char_type,
        short_type,
        long_type,
//      ….
//      ….
//      ….
//      your own type definition
        your_type

};

template < typename type >

you probably meant here:

template < Data_Types type >

I do not like you use that there are enumerator values. Use real type
names.
struct Type_Traits {
};

template<>
struct Type_Traits< char_type > {

};

template<>
struct Type_Traits< short_type > {

};

template<>
struct Type_Traits< long_type > {

};

template<>
struct Type_Traits< your_type > {

};

        Please suggest how you write good enumerated type name.

        Can be Data_Types::char_type?  Or… eCharType

For me the whole issue goes too far from what i have used too.
template<>
struct Type_Traits< Data_Types::char_type > {

};

or…

template<>
struct Type_Traits< eCharType > {

};

        The readable written code is necessary when you read good name and
you know where name comes from either enumerated type or struct.

I do not really care. I for example use PascalCase for both
enumerators and types. I do not usually use enumerators at places
where everybody would expect types and other way around types where
everybody would expect values.


Why what? Why it is more logical to have things immutable by default?
Because most things do not change after creation and before
destruction of them. They stay same for their whole lifetime. Most
things that do change during their life-time do it immediately after
construction, so could do it at construction as well and so are also
immutable. That becomes obvious after being software developer for
some years.
        You can do that below.

typedef char char_type; // inside class or struct
typedef short short_type;
// …

        How do I name constant type?

typedef const char_type const_char_type;

typedef const char_type const_chartype;

typedef const char_type c_char_type;

typedef const char_type cchar_type;
        Which one of four lines above is good name?  Notice two ‘c’
characters in the fourth line above is so confusing.  How do you want
to shorten typedef name?

"char" is fine and "char const" is fine for me so i do not see the
point to typedef, especially i do not like that "type".
typedef const char_type const_char_t;
or….
typedef const char_type t_const_char;

        Always look at prefix ‘t’ as type is easier to read.  What do you
suggest good typedef name?

I do not use naming convention of stl or boost, that use "_t" suffix
for some primitive types. Possibly it was actually C99 who started it.
I do not like it. I name typedefs like all classes CamelCase. No
prefix or suffix. If it is typedef for container it is usually plural.
Meaning matters.

class FruitBasket
{
public:
typedef std::vector<Fruit> Fruits;
// ...
private:
Fruits fruits_;
};

That way i may later change the typedef to some else container or
replace with a Fruits class of my own.
        And to add.  How do you shorten typedef struct name?

typedef Type_Traits< char_type > Char_Traits;
typedef Type_Traits< short_type > Short_Traits;
typedef Type_Traits< long_type > Long_Traits;
typedef Type_Traits< your_type > YourType_Traits;

        Please let me know if these names are good.  Sometimes, I don’t find
good naming variable guideline.  I need your feedback.

Hmm... but still i do not understand why you do not like TypeTraits<
char > itself? Char_Traits sounds like special set of genetically
determined characteristics or conditions for char types. So probably
template accepting char, wchar_t, char16_t and char32_t as arguments.
These traits have only meaning for types of char. Short_Traits sounds
like some features or qualities that are short about something (so
kind of nonsense).
 

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
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top