473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_containe d=3;

std::vector<xyz _tripletpick_po ints(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,co ntained}.

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 2534
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_containe d=3;

std::vector<xyz _tripletpick_po ints(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,co ntained}.
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,co ntained}.

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_containe d=3;

std::vector<xyz _tripletpick_po ints(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_containe d=3;

std::vector<xy z_tripletpick_p oints(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,co ntained}.
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_containe d=3;

std::vector<xy z_tripletpick_p oints(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,co ntained}.


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********@warp mail.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
Euarchontoglire s.
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
2985
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. I have a list in a particular order that I want to split into two lists: the list of every nth item, and the list of remaining items. It's important to maintain the original order in both lists. So the first list is simple:
9
5800
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 held in memory and therefore fast, without risk of damaging real files, and with none of the messiness of leftover files after testing. Googling the archives and the web suggests that only I and Remy Blank have done much along these lines. I...
5
16927
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 elder colleagues utilize this feature of C++ in their implementations based on diverse programming styles in diverse application domains. Thank you for your interest.
10
6716
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 "r+" mode, where the file will open for reading "+ also writing", but it will not create a new file if one isn't already there; it fails with an error. POSIX also has this behavior with it's 'open' call, you just leave the 'create' bit out, and...
8
10637
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 the same time and set different values? Thank you for suggestions!
32
3787
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 it, and impossible to guarantee that it won't happen. Therefore, i think it's surprising that this function has not been deprecated. The C++98 Standard keeps it from the C89 standard. The C99 Standard has kept it :-o.
388
21632
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 worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
8
32764
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 where it's useful to know the number of image modes (to set the size of bitsets, etc.) Currently I just use a const int to hold this size: enum Modes {mode_a, mode_b, mode_c};
29
1984
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. Can someone tell me why "->" exists? The compiler knows the difference between a structure and a pointer to a structure, so why can't it just let me write "foo.bar" in both cases and not have to go back and rewrite things when I later decide I want...
0
8240
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8625
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.