J
Jacek Dziedzic
Hi!
I often find that my programs need to store information on
"current mode of something" with two or at most several
mutually exclusive "modes" to choose from, e.g.
- datafile: is it in a) read-only mode or b) write-only mode,
- a function picking points a) above, b) below or c) contained
on a plane in 3D,
etc.
This "current mode" usually needs to be stored somewhere,
often also needs to be passed to a function. What is, from
the point of view of experienced programmers, the most
convenient way of expressing such modes?
Generally I can't decide between an enum and integer
constants, such as
// datafile open modes
const int mode_readonly=1;
const int mode_writeonly=2;
// direction selection for the pick_points() function
const int points_above=1;
const int points_below=2;
const int points_contained=3;
std::vector<xyz_triplet> pick_points(plane p, int mode) {
//...
}
Usually there are only two or three such modes for some
property, so I tend towards the "const int" version,
because somehow I'm convinced that creating an extra type
for such a thing is overkill, but perhaps I'm wrong?
Now the parameter "mode" usually needs some explanation
attached to it, because it isn't immediately obvious
what it means, as it is an int. On the other hand it seems
bad to me if my program is cluttered with a lot of types
like enum TPlaneMode {above,below,contained}.
Also, in what scope should such things be placed?
In my programs they usually wind up at the namespace scope,
so in a typical namespace I have a bunch of classes,
usually a few static variables a few of such constants.
Is it the correct way to go?
TIA,
- J.
I often find that my programs need to store information on
"current mode of something" with two or at most several
mutually exclusive "modes" to choose from, e.g.
- datafile: is it in a) read-only mode or b) write-only mode,
- a function picking points a) above, b) below or c) contained
on a plane in 3D,
etc.
This "current mode" usually needs to be stored somewhere,
often also needs to be passed to a function. What is, from
the point of view of experienced programmers, the most
convenient way of expressing such modes?
Generally I can't decide between an enum and integer
constants, such as
// datafile open modes
const int mode_readonly=1;
const int mode_writeonly=2;
// direction selection for the pick_points() function
const int points_above=1;
const int points_below=2;
const int points_contained=3;
std::vector<xyz_triplet> pick_points(plane p, int mode) {
//...
}
Usually there are only two or three such modes for some
property, so I tend towards the "const int" version,
because somehow I'm convinced that creating an extra type
for such a thing is overkill, but perhaps I'm wrong?
Now the parameter "mode" usually needs some explanation
attached to it, because it isn't immediately obvious
what it means, as it is an int. On the other hand it seems
bad to me if my program is cluttered with a lot of types
like enum TPlaneMode {above,below,contained}.
Also, in what scope should such things be placed?
In my programs they usually wind up at the namespace scope,
so in a typical namespace I have a bunch of classes,
usually a few static variables a few of such constants.
Is it the correct way to go?
TIA,
- J.