Strong Bad Programming
-
My use of enums has always been to describe unique items (for example - days of the week, or months of the year). I was crusinging through some existing code at work, and found this in a class definition (identifier names changed to protect the innocent): class MyClass : public { public: enum { a=4, b=0, c=4, d=25, e=0, f=0, g=256}; }; I suspect this was done because it was considered a "clever" way to initialize some constants. IMHO, this is a wholly inappropriate use of the enum construct. Just thought I'd vent a little. ------- sig starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- sig ends
Not sure i agree with it being bad code. However, like all such statements, it really depends on the context in which it is used. If you have a variable that you want to be in one of several states you use an enum. If there is a calculation that uses a variable that has a 1:1 relationship with a state then i don't see a problem with casting the enum to a long. The alternative is to switch on the enum - which is far less elegant. There is a reason the language allows you to assign values to enum elements. I've seen this many times over the past 15yrs. ...cmk Save the whales - collect the whole set
-
Using
enum
in this way lets you have constants that are elements of a named type, which is useful with templates. You can also use this to define non-uniform sequences. For example, define an enumerationenum NonUnif { A = 5, B = -1, C = 10 };
. You could then define auto-increment and auto-decrement operators for the type to give you the sequence. I agree that this is a bit of a stretch, but it's still useful.
Software Zen:
delete this;
Gary Wheeler wrote: You could then define auto-increment and auto-decrement operators for the type to give you the sequence. But only if the sequence didn't contain duplicates. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com
-
My use of enums has always been to describe unique items (for example - days of the week, or months of the year). I was crusinging through some existing code at work, and found this in a class definition (identifier names changed to protect the innocent): class MyClass : public { public: enum { a=4, b=0, c=4, d=25, e=0, f=0, g=256}; }; I suspect this was done because it was considered a "clever" way to initialize some constants. IMHO, this is a wholly inappropriate use of the enum construct. Just thought I'd vent a little. ------- sig starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- sig ends
That's nothing. I've seen the following code, which hasn't been superated by any other stupid idea. This guy implemented a way to return from a function (in the middle of it, so that if the function is called again, execution will continue from where it returned from the previous time. The code was something like this:
#define START_FUNC_WITH_MEMORY() static lastLine; switch (lastLine){case 0: #define RETURN_HERE(x) {line = x; return;} case x: #define END_FUNC_WITH_MEMORY() } ProcessData () { START_FUNCTION_WITH_MEMORY() ... if (moreDataRequired) RETURN_HERE(1); ... if (moreDataRequired) RETURN_HERE(2); ... if (moreDataRequired) RETURN_HERE(3); ... if (moreDataRequired) RETURN_HERE(4); ... END_FUNCTION_WITH_MEMORY() }
Which, replacing macros becomes:ProcessData () { static lastLine; switch (lastLine) { case 0: ... if (moreDataRequired) {lastLine=1; return;} case 1: ... if (moreDataRequired) {lastLine=2; return;} case 2: ... if (moreDataRequired) {lastLine=3; return;} case 3: ... } }
-
That's nothing. I've seen the following code, which hasn't been superated by any other stupid idea. This guy implemented a way to return from a function (in the middle of it, so that if the function is called again, execution will continue from where it returned from the previous time. The code was something like this:
#define START_FUNC_WITH_MEMORY() static lastLine; switch (lastLine){case 0: #define RETURN_HERE(x) {line = x; return;} case x: #define END_FUNC_WITH_MEMORY() } ProcessData () { START_FUNCTION_WITH_MEMORY() ... if (moreDataRequired) RETURN_HERE(1); ... if (moreDataRequired) RETURN_HERE(2); ... if (moreDataRequired) RETURN_HERE(3); ... if (moreDataRequired) RETURN_HERE(4); ... END_FUNCTION_WITH_MEMORY() }
Which, replacing macros becomes:ProcessData () { static lastLine; switch (lastLine) { case 0: ... if (moreDataRequired) {lastLine=1; return;} case 1: ... if (moreDataRequired) {lastLine=2; return;} case 2: ... if (moreDataRequired) {lastLine=3; return;} case 3: ... } }
Yuck!:omg: Though to be honest, I've seen/coded worse...:-O I've felt much better since I gave up hope.
-
My use of enums has always been to describe unique items (for example - days of the week, or months of the year). I was crusinging through some existing code at work, and found this in a class definition (identifier names changed to protect the innocent): class MyClass : public { public: enum { a=4, b=0, c=4, d=25, e=0, f=0, g=256}; }; I suspect this was done because it was considered a "clever" way to initialize some constants. IMHO, this is a wholly inappropriate use of the enum construct. Just thought I'd vent a little. ------- sig starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- sig ends
That is actually pretty standard practice, because (as was already mentioned) it in effect creates a scoped constant. If you meet a language lawyer he'll probably argue that this is "the right way":
class CFoo
{
public:
static const int a = 4;
static const int b = 0;
// and so on...
};however that syntax (initializing a static member in the header) doesn't work on all compilers, most notably VC 6. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ----
-
That's nothing. I've seen the following code, which hasn't been superated by any other stupid idea. This guy implemented a way to return from a function (in the middle of it, so that if the function is called again, execution will continue from where it returned from the previous time. The code was something like this:
#define START_FUNC_WITH_MEMORY() static lastLine; switch (lastLine){case 0: #define RETURN_HERE(x) {line = x; return;} case x: #define END_FUNC_WITH_MEMORY() } ProcessData () { START_FUNCTION_WITH_MEMORY() ... if (moreDataRequired) RETURN_HERE(1); ... if (moreDataRequired) RETURN_HERE(2); ... if (moreDataRequired) RETURN_HERE(3); ... if (moreDataRequired) RETURN_HERE(4); ... END_FUNCTION_WITH_MEMORY() }
Which, replacing macros becomes:ProcessData () { static lastLine; switch (lastLine) { case 0: ... if (moreDataRequired) {lastLine=1; return;} case 1: ... if (moreDataRequired) {lastLine=2; return;} case 2: ... if (moreDataRequired) {lastLine=3; return;} case 3: ... } }
I was forced to convert some VB6 code written in 2002 where the developer used *LINE NUMBERS*. In fact he started to tell me how much better they make coding, and why VB was great before I could chew my own arm off to escape.
Though, I did have one developer ask me how to tell if an integer was negative. Stunned, it took me a while to answer less then zero and they went off pleased whith their new found knowlege. I made it a point to never go near their code ever. My goal is to look at code like a chessmaster looks at a chessboard to see positions and possibilites beyond lines and characters. -
That's nothing. I've seen the following code, which hasn't been superated by any other stupid idea. This guy implemented a way to return from a function (in the middle of it, so that if the function is called again, execution will continue from where it returned from the previous time. The code was something like this:
#define START_FUNC_WITH_MEMORY() static lastLine; switch (lastLine){case 0: #define RETURN_HERE(x) {line = x; return;} case x: #define END_FUNC_WITH_MEMORY() } ProcessData () { START_FUNCTION_WITH_MEMORY() ... if (moreDataRequired) RETURN_HERE(1); ... if (moreDataRequired) RETURN_HERE(2); ... if (moreDataRequired) RETURN_HERE(3); ... if (moreDataRequired) RETURN_HERE(4); ... END_FUNCTION_WITH_MEMORY() }
Which, replacing macros becomes:ProcessData () { static lastLine; switch (lastLine) { case 0: ... if (moreDataRequired) {lastLine=1; return;} case 1: ... if (moreDataRequired) {lastLine=2; return;} case 2: ... if (moreDataRequired) {lastLine=3; return;} case 3: ... } }
-
The guyy that wrote this code hasn't worked here for some time - otherwise, I'd offer to stick a fork in his head. :) ------- sig starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- sig ends
-
That is actually pretty standard practice, because (as was already mentioned) it in effect creates a scoped constant. If you meet a language lawyer he'll probably argue that this is "the right way":
class CFoo
{
public:
static const int a = 4;
static const int b = 0;
// and so on...
};however that syntax (initializing a static member in the header) doesn't work on all compilers, most notably VC 6. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ----
Michael Dunn wrote: If you meet a language lawyer he'll probably argue that this is "the right way": Words from the Master himself[^]: I tend to use the "enum trick" because it's portable and doesn't tempt me to use non-standard extensions of the in-class initialization syntax.
-
I was forced to convert some VB6 code written in 2002 where the developer used *LINE NUMBERS*. In fact he started to tell me how much better they make coding, and why VB was great before I could chew my own arm off to escape.
Though, I did have one developer ask me how to tell if an integer was negative. Stunned, it took me a while to answer less then zero and they went off pleased whith their new found knowlege. I made it a point to never go near their code ever. My goal is to look at code like a chessmaster looks at a chessboard to see positions and possibilites beyond lines and characters.andy brummer wrote: I did have one developer ask me how to tell if an integer was negative It takes very advanced pschoanalysis skills to determine whether an integer is truly negative, or merely having a bad day. Most programmers never learn these skills, though supremely gifted ones, like yourself, develop an uncanny intuition about coding such tests over time. As a fallback, he can simply square the integer - if the result is positive, there's a 50% chance that it was negative. I've felt much better since I gave up hope.
-
The guyy that wrote this code hasn't worked here for some time - otherwise, I'd offer to stick a fork in his head. :) ------- sig starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- sig ends
-
Ah yes, right back in character. ;) PS - I think I have a skin or two of your's on some of my converted cars. :) __________________________________________ a two cent stamp short of going postal.
Converted cars? Converted to what? ------- sig starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- sig ends
-
andy brummer wrote: I did have one developer ask me how to tell if an integer was negative It takes very advanced pschoanalysis skills to determine whether an integer is truly negative, or merely having a bad day. Most programmers never learn these skills, though supremely gifted ones, like yourself, develop an uncanny intuition about coding such tests over time. As a fallback, he can simply square the integer - if the result is positive, there's a 50% chance that it was negative. I've felt much better since I gave up hope.
-
My use of enums has always been to describe unique items (for example - days of the week, or months of the year). I was crusinging through some existing code at work, and found this in a class definition (identifier names changed to protect the innocent): class MyClass : public { public: enum { a=4, b=0, c=4, d=25, e=0, f=0, g=256}; }; I suspect this was done because it was considered a "clever" way to initialize some constants. IMHO, this is a wholly inappropriate use of the enum construct. Just thought I'd vent a little. ------- sig starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- sig ends
In one situation I know of, initializing constants in this way is the only way:
template <int n> class a { enum {m_n = n}; };
Now, the value of n is more accessible than it was. -
Gary Wheeler wrote: You could then define auto-increment and auto-decrement operators for the type to give you the sequence. But only if the sequence didn't contain duplicates. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com
Ravi Bhavnani wrote: But only if the sequence didn't contain duplicates <:~ dangerous_approach_to_talking_about_programming_in_the_Lounge:~ > True. I was simply pointing out to John that assigned
enum
values had their uses. </:~ dangerous_approach_to_talking_about_programming_in_the_Lounge:~ >
Software Zen:
delete this;
-
Ravi Bhavnani wrote: But only if the sequence didn't contain duplicates <:~ dangerous_approach_to_talking_about_programming_in_the_Lounge:~ > True. I was simply pointing out to John that assigned
enum
values had their uses. </:~ dangerous_approach_to_talking_about_programming_in_the_Lounge:~ >
Software Zen:
delete this;
This isn't a programming question - it's a discussion about technique that started as a rant about non-standard use of a construct. I still think it's an unintended use of enum. Granted, it might have an alternative use, but 99 times out of 100, an enum is used to uniquely identify groups of assoicated objects. (Before today, I would have said 100 times out of 100.) ------- sig starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- sig ends
-
This isn't a programming question - it's a discussion about technique that started as a rant about non-standard use of a construct. I still think it's an unintended use of enum. Granted, it might have an alternative use, but 99 times out of 100, an enum is used to uniquely identify groups of assoicated objects. (Before today, I would have said 100 times out of 100.) ------- sig starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- sig ends
I agree with your rant. Based on your description, it sounds like
enum
's were being misused, or at least were being used as part of some sloppy programming. BTW, I wasn't accusing you of posting a programming question; I was alluding to the possibility that my post was treading on dangerous ground. The no_programming_questions_in_the_Lounge death squad has been rather trigger-happy lately. Wait a second. John Simmons accused of posting a programming question in the Lounge? Incommminng! :-D
Software Zen:
delete this;
-
In one situation I know of, initializing constants in this way is the only way:
template <int n> class a { enum {m_n = n}; };
Now, the value of n is more accessible than it was.Sorry, but Visual C++ 6.0 has problems handling templates with int data type. It's a well known VC6 bug, which is not solved by any service pack. Templates defined this way will only be implemented once, so there's no reason to use it in a programm. For example, if you use your class as shown:
template <int n> class a { enum {m_n = n}; }; void main (void) { a<5> a1; a<7> a2; a<9> a3; ... }
a1, a2 and a3 will be implemented with the same code, so m_n will have the same value (I don't remember if it was the first or the last appearance) in all cases. -
I agree with your rant. Based on your description, it sounds like
enum
's were being misused, or at least were being used as part of some sloppy programming. BTW, I wasn't accusing you of posting a programming question; I was alluding to the possibility that my post was treading on dangerous ground. The no_programming_questions_in_the_Lounge death squad has been rather trigger-happy lately. Wait a second. John Simmons accused of posting a programming question in the Lounge? Incommminng! :-D
Software Zen:
delete this;
Derivative posts regarding a programming rant which stray into posting code examples or resulting in a programming question are exempt from the "no programming questions" rule because the question was posed as a result of said rant. You're safe, and if anyone screws with you about it, send them my way. I'll be happy to help them understand. :) ------- sig starts "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "You won't like me when I'm angry..." - Dr. Bruce Banner Please review the Legal Disclaimer in my bio. ------- sig ends
-
I was forced to convert some VB6 code written in 2002 where the developer used *LINE NUMBERS*. In fact he started to tell me how much better they make coding, and why VB was great before I could chew my own arm off to escape.
Though, I did have one developer ask me how to tell if an integer was negative. Stunned, it took me a while to answer less then zero and they went off pleased whith their new found knowlege. I made it a point to never go near their code ever. My goal is to look at code like a chessmaster looks at a chessboard to see positions and possibilites beyond lines and characters.andy brummer wrote: Though, I did have one developer ask me how to tell if an integer was negative.
bool IsIntNegative(int val) { char buffer[12]; sprintf(buffer, "%d", val); return buffer[0] == '-'; }
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke