Hello,
I have seen code that does the following:
enum Letter { X, Y, Z, numLetters };
Is this considered good or bad code? On one hand, the code seems more
maintainable because no matter whether letters are added or removed
the value of numLetters will be correct (as long as no letter is
given a number with the equal sign which makes this untrue).
On the other hand, this forces the need for needless default
cases in switch statements since the compiler will complain
if in a switch numLetters above is not taken care of.
So, is this good or bad?
Opinions welcome,
Regards,
Neil 5 1323
"Neil Zanella" <nz******@cs.mu n.ca> wrote in message
news:b6******** *************** **@posting.goog le.com... Hello,
I have seen code that does the following:
enum Letter { X, Y, Z, numLetters };
Is this considered good or bad code? On one hand, the code seems more maintainable because no matter whether letters are added or removed the value of numLetters will be correct (as long as no letter is given a number with the equal sign which makes this untrue).
As long as all the values have sequential values
beginning with zero, as is the default. Some or
all of these values could be explicitly specified
if desired. On the other hand, this forces the need for needless default cases in switch statements since the compiler will complain if in a switch numLetters above is not taken care of.
What do you mean? It needs no 'taking care of'.
A 'switch' statement will only do what you tell
it to with particular values, and nothing with
others. What error message are you getting,
with what specific code?
enum Letter value = Y;
switch(value)
{
case X:
/* whatever */
break;
}
Note that only one value is processed by the switch.
This should not result in a compiler error. So, is this good or bad?
Using 'one more' value in a list of sequential enumeration
values is sometimes used to maintain a 'count' of the
values, yes. Whether it's 'good' or 'bad' really depends
upon what you're doing.
-Mike
"Neil Zanella" <nz******@cs.mu n.ca> wrote Hello,
I have seen code that does the following:
enum Letter { X, Y, Z, numLetters };
Is this considered good or bad code?
I consider it to be bad because it breaks the cohesion of the type. The type
'letter' now represents two separate concepts: letters AND the number of
possible letters. If you had a set (in the generic sense, not the standard
container) of letters, this type indicates that the set could contain
'numLetters' as a valid member, which is completely absurd. 'numLetters'
should be a separate constant. Both the enum and the constant -- along with
various utility functions -- could be kept in a common namespace or class if
they need to be packaged together, but never in the same enum.
On one hand, the code seems more maintainable because no matter whether letters are added or removed the value of numLetters will be correct (as long as no letter is given a number with the equal sign which makes this untrue).
It's a lot more maintainable to change one constant if the enum is changed
than to track down errors because the enum is incorrectly designed.
On the other hand, this forces the need for needless default cases in switch statements since the compiler will complain if in a switch numLetters above is not taken care of.
At the very least.
So, is this good or bad?
Definitely bad.
Claudio Puviani
"Neil Zanella" <nz******@cs.mu n.ca> wrote in message
news:b6******** *************** **@posting.goog le.com... Hello,
I have seen code that does the following:
enum Letter { X, Y, Z, numLetters };
Is this considered good or bad code? On one hand, the code seems more maintainable because no matter whether letters are added or removed the value of numLetters will be correct (as long as no letter is given a number with the equal sign which makes this untrue).
On the other hand, this forces the need for needless default cases in switch statements since the compiler will complain if in a switch numLetters above is not taken care of.
So, is this good or bad?
Bad because:
void foo(Letter);
foo(numLetters) ; // OK?
Also many compilers will issue a warning if you create a switch(Letter)
statement that includes
neither numLetters nor a default case.
A better variation (for the usual enums without initializers) is:
enum Letter { X, Y, Z, FIRST_LETTER=X, LAST_LETTER=Z };
This way you don't introduce a value that is outside the proper range and
FIRST_LETTER is sometimes useful when you want to pass them as ints and
use 0 as an undefined value (if you init X=1) Opinions welcome,
Regards,
Neil
"Neil Zanella" <nz******@cs.mu n.ca> wrote in message
news:b6******** *************** **@posting.goog le.com... Hello,
I have seen code that does the following:
enum Letter { X, Y, Z, numLetters };
Is this considered good or bad code? On one hand, the code seems more maintainable because no matter whether letters are added or removed the value of numLetters will be correct (as long as no letter is given a number with the equal sign which makes this untrue).
On the other hand, this forces the need for needless default cases in switch statements since the compiler will complain if in a switch numLetters above is not taken care of.
So, is this good or bad?
Bad because:
void foo(Letter);
foo(numLetters) ; // OK?
Also many compilers will issue a warning if you create a switch(Letter)
statement that includes
neither numLetters nor a default case.
A better variation (for the usual enums without initializers) is:
enum Letter { X, Y, Z, FIRST_LETTER=X, LAST_LETTER=Z };
This way you don't introduce a value that is outside the proper range and
FIRST_LETTER is sometimes useful when you want to pass them as ints and
use 0 as an undefined value (if you init X=1) Opinions welcome,
Regards,
Neil
In message <4n************ *********@news4 .srv.hcvlny.cv. net>, Claudio
Puviani <pu*****@hotmai l.com> writes "Neil Zanella" <nz******@cs.mu n.ca> wrote Hello,
I have seen code that does the following:
enum Letter { X, Y, Z, numLetters };
Is this considered good or bad code? I consider it to be bad because it breaks the cohesion of the type. The type 'letter' now represents two separate concepts: letters AND the number of possible letters.
But number-of-letters is just a way of thinking. Rename the extra item:
enum Letter { X, Y, Z, endOfLetters };
Now it's just an example of the all-pervasive C++ one-past-the-end
paradigm.
If you had a set (in the generic sense, not the standard container) of letters, this type indicates that the set could contain 'numLetters' as a valid member, which is completely absurd.
But if you had, say, std::vector<cha r> v, you wouldn't complain because
v.end() didn't point to a letter, would you?
'numLetters' should be a separate constant. Both the enum and the constant -- along with various utility functions -- could be kept in a common namespace or class if they need to be packaged together, but never in the same enum.
On one hand, the code seems more maintainable because no matter whether letters are added or removed the value of numLetters will be correct (as long as no letter is given a number with the equal sign which makes this untrue). It's a lot more maintainable to change one constant if the enum is changed
Even more maintainable not to have to change anything else...
than to track down errors because the enum is incorrectly designed.
If you consider that to be an incorrect design, of course ;-) On the other hand, this forces the need for needless default cases in switch statements since the compiler will complain if in a switch numLetters above is not taken care of. At the very least.
But not all enums are used in switches. So, is this good or bad?
Definitely bad.
_Sometimes_ bad.
--
Richard Herring This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Charles Law |
last post by:
When displaying the Color Picker for a web page (in the VS IDE), Background
is listed on the System Colors tab.
Also,
Color.FromName("Background").IsNamedColor
returns True.
However, Background is not a member of the SystemColors class, and it is not
|
by: PaulUK |
last post by:
When I use the following C# code:
string colorNames = Enum.GetNames(typeof(KnownColor));
colorNames does not contain the full list of member names detailed in the
System.Drawing.KnownColor enumeration, but just a subset. Why is this?
Paul.
|
by: Neil Zanella |
last post by:
Hello,
I have seen code that does the following:
enum Letter { X, Y, Z, numLetters };
Is this considered good or bad code? On one hand, the code seems more
maintainable because no matter whether letters are added or removed
the value of numLetters will be correct (as long as no letter is
given a number with the equal sign which makes this untrue).
|
by: aevans1108 |
last post by:
Greetings
I can't seem to inherit enumerated values from a globally defined type
in my XML schema. XmlSchema.Compile() doesn't like it.
Here's the schema.
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
|
by: Stephan Steiner |
last post by:
Hi
I'm not sure where to post this, so since I came to it when searching the
..net API documentation in c# mode I'll post it here.
Have a look at the 4 items below. It appears to me that two descriptions
have been interchanged:
ReceiveBuffer Send low water mark.
ReceiveLowWater Receive low water mark.
| |
by: Brett Kelly |
last post by:
Hello all,
I'm in a situation where I need to retrieve a member from the
System.Data.SqlDbType enumeration knowing only the type name.
At this point, I'm just trying to get reflection to work... This is
what I've got so far:
using System;
using System.Reflection;
|
by: aarklon |
last post by:
Hi all,
in the article
http://www.c-faq.com/struct/enumvsdefine.html
it is written as
Some advantages of enumerations are that the numeric values are
automatically assigned, that a debugger may be able to display the
symbolic values when enumeration variables are examined, and that they
obey block scope. (A compiler may also generate nonfatal warnings when
|
by: braratine |
last post by:
Hello,
I have to following issue.
A type is declared based on an enum:
"
enum TpAddressPlan {
P_ADDRESS_PLAN_NOT_PRESENT = 0,
P_ADDRESS_PLAN_UNDEFINED = 1,
P_ADDRESS_PLAN_IP = 2,
P_ADDRESS_PLAN_MULTICAST = 3,
P_ADDRESS_PLAN_UNICAST = 4,
|
by: SQACSharp |
last post by:
I'm using the EnumChildWindows API with an EnumChildWndProc callback
to populate the treeview. The output will be something similar to spy+
+
How can I specify the parent when adding a new node ??
When adding a new node is there any way to get an handle or something
else to be able add the childs to the correct parent ?
Thanks!
|
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...
|
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,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |