Enumerations

  • Thread starter Christopher Benson-Manica
  • Start date
M

Mark A. Gibbs

JKop said:
Because it can be done. If you write a function that takes
a month, then you've to check that it's >= 1 and <= 12.

"because it can be done" is not a good reason for doing something. i
fail to see what would inspire an otherwise level-headed programmer to
randomly construct an illegal enumeration. why explicitly construct at
all? it makes little sense. and if you decide you *must* explicitly
construct an enumerator, why pick a raondom value like 13? why not just
use an existing enumerator value, like "feb"?

do you hang around programmers that randomly assign spurious values to
obviously logically incompatible types (there is no month "13", just
like there is no month "6" or "100", but there is a "may")?
Therefore, what use has an enum? Why not just use constant
variables?

namespace Month{
const unsigned char jan = 1,
feb = 2,
mar = 3,
apr = 4,
may = 5,
jun = 6,
//and so on

because, as i showed you before, they are less safe than enumerators. no
one ever claimed enumerations cannot be broken, but at least when you
break an enumerator, it's not likely to be by accident. on the other hand:

unsigned char c = 42;
// lots of code in between
month m = c; // not a squeak from the compiler
// the only way you could break this with enums is
// if you did month m = static_cast<month>(c);
// or at least month m = month(c);
// which is pretty hard to miss

or:

namespace a {
typedef unsigned char month;
static const unsigned char jan = 0;
static const unsigned char feb = 1;
}
namespace b {
typedef unsigned char month;
static const unsigned char jan = 1;
static const unsigned char feb = 2;
}

b::month get_month()
{
using namespace a;
return jan; // but which jan?
// an enum version would not compile
}

and finally, let's assume that i thought that it would be reasonable for
a programmer to write month(13). in the enum version, modern ide's would
show you (via a tooltip or something) the name of the enumerator if it
was valid, and a number otherwise. in other words, you can debug just by
hovering the mouse over your suspect variable. you can't do that with
unsigned char's.

but i'll tell you what, i'll be open-minded about this. show me one (1)
real world error caused by any practical usage of enum that would be
fixed if the code were *identical* except the enum type were unsigned
char and the enumerators were static constants. by my count i've shown
you four code sample that demonstrate that enums are safer. all i ask
from you is one.

mark
 
J

JKop

I got around this by writing a Month class that validates
input on assignment and construction.


-JKop
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top