473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

what is the best practice to store "modes": enum, int constant orwhat?


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_tripletpick_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.
Jul 22 '06 #1
9 2511
Jacek Dziedzic wrote:
>
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_tripletpick_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?
Why is it an overkill? It comes with zero cost and several benefits.
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}.
Why? Give the enumerations explanatory names and you make your code a
lot clearer.

--
Ian Collins.
Jul 22 '06 #2
Ian Collins wrote:
> 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?

Why is it an overkill? It comes with zero cost and several benefits.
I see.
>>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}.

Why? Give the enumerations explanatory names and you make your code a
lot clearer.
OK, I'll go for enums then. What about scoping? Should I leave
them at the namespace level?

thanks,
- J.
Jul 23 '06 #3

Jacek Dziedzic wrote:
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?
Well, it depends. Is there a lot of stuff like:

switch (mode)
{
case MODEX:
... break;
case MODEY:
... break;
}
If so then you are probably a LOT better off making a new type and
using the state pattern, or possibly template method.

Even if not then you might be better off with a new type if it makes
your code clearer and/or breaks dependencies. You have to take each
situation as it arrives.

Jul 23 '06 #4

Jacek Dziedzic wrote:
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_tripletpick_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,
For a choice of two I would use 'bool'. For a choice of three, I would
use 'wide_bool'.

Jul 23 '06 #5
In message <30**************************@news.chello.pl>, Jacek Dziedzic
<jacek@no_spam.tygrys.no_spam.netwrites
>
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,
[snip]
>
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_tripletpick_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?
I think you're wrong. Having an extra type means increased type-safety:
the compiler will tell you if you try to pass an inappropriate mode as a
parameter. The compiler will also ensure your enumeration constants are
distinct, whereas if you accidentally typed "const int points_below=3;"
above, you're setting yourself up for a really obscure bug.
>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.
If you use a well-named enum, the name of the type _is_ the
explanation. So it's self-documenting as well.
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}.
Saying what you mean, by having distinct types for distinct purposes,
and connecting the set of possible values directly to the type, is
usually considered a *good* thing. It helps those who have to maintain
the code, and also helps the compiler protect you from yourself.
Cluttering your program with a lot of (syntactically) unrelated
constants does neither.
>
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?
Depends. If the enumerated type only applies to a single class, you
could declare it inside the class.

--
Richard Herring
Jul 24 '06 #6
Richard Herring wrote:
In message <30**************************@news.chello.pl>, Jacek Dziedzic
<jacek@no_spam.tygrys.no_spam.netwrites
>>
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,


[snip]
>>
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_tripletpick_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?


I think you're wrong. Having an extra type means increased type-safety:
the compiler will tell you if you try to pass an inappropriate mode as a
parameter. The compiler will also ensure your enumeration constants are
distinct, whereas if you accidentally typed "const int points_below=3;"
above, you're setting yourself up for a really obscure bug.
>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.


If you use a well-named enum, the name of the type _is_ the
explanation. So it's self-documenting as well.
>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}.


Saying what you mean, by having distinct types for distinct purposes,
and connecting the set of possible values directly to the type, is
usually considered a *good* thing. It helps those who have to maintain
the code, and also helps the compiler protect you from yourself.
Cluttering your program with a lot of (syntactically) unrelated
constants does neither.
>>
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?
Depends. If the enumerated type only applies to a single class, you
could declare it inside the class.
Thanks a lot to you and others who responded!

- J.
Jul 24 '06 #7
Jacek Dziedzic wrote:
Thanks a lot to you and others who responded!
The best way to store modes is in some variation of the State Design
Pattern. You should have a pointer or reference to a parent class, with a
virtual method called "doMyMode()". Put the behavior specific to this mode
into derived classes, in their overrides of "doMyMode()".

This situation, sometimes called "replace conditional with polymorphism", is
the heart of OO design, and it will make your situation much more flexible
than an 'enum'. Regardless whether the enum itself is typesafe, you will
often scatter 'switch' statements around your code, based on the mode.
Replacing all these conditionals with a polymorphic type will make new types
easier to add.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 24 '06 #8
da********@warpmail.net wrote:
For a choice of two I would use 'bool'. For a choice of three, I would
use 'wide_bool'.
What's wide_bool? What do you do when you have a choice of 4?

PS. This reminds me of
<http://thedailywtf.com/forums/thread/80146.aspx>...

Jul 24 '06 #9
Rat Monkey Hybrid wrote:

BTW rats and monkeys are more closely related to each other than to other
mammals. The parent group of rabbits, rodents, shrews, and primates are the
Euarchontoglires.
PS. This reminds me of
<http://thedailywtf.com/forums/thread/80146.aspx>...
Now /that/ is just priceless!

enum Bool
{
True,
False,
FileNotFound
};

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Steven Bethard | last post by:
I feel like this has probably been answered before, but I couldn't find something quite like it in the archives. Feel free to point me somewhere if you know where this has already been answered. ...
9
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is...
5
by: Razmig K | last post by:
Dear mates, This is just a small survey for the common (and uncommon) nontrivial uses of the aforementioned C++ construct. It's posted by an average C++ programmer who's curious about how his...
10
by: Dave O'Hearn | last post by:
I want to open a file for both reading and writing, but when I do "ios::in|ios::out" with an fstream, it creates the file if it doesn't already exist. I don't like that last part. C's fopen has the...
8
by: Lian | last post by:
Hi all, It is a newbie's question about html tag "img". The attributes "title" and "alt" for "img" seems having the same function. So what is the main difference between them? Can i use them at...
32
by: Marcus | last post by:
We all know that the "gets" function from the Standard C Library (which is part of the Standard C++ Library) is dangerous. It provides no bounds check, so it's easy to overwrite memory when using...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
8
by: Mark P | last post by:
I'm working on a project where I have something like enum Modes {mode_a, mode_b, ...}; Due to project spec changes, the number of Modes has increased several times. There are a few places...
29
by: not.here.now | last post by:
A quick search of this group and its FAQ, and elsewhere, have not answered this question to my satisfaction. Apologies if I missed something obvious, either in the literature or my reasoning. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.