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
:omg: I hope those contants are not actually named a, b, c, d, etc. You're right though, constant values should be declared with
const int
. I faced this issue recently in C# where enums cannot used in place of ints (without casting). Regards, Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
-
John Simmons / outlaw programmer wrote: IMHO, this is a wholly inappropriate use of the enum construct. So, you now know the "cool! it compiled!" programmer. John Simmons / outlaw programmer wrote: identifier names changed to protect the innocent This is not the old JS we all know... I see dumb people
Actually, it's proprietary code, so I couldn't use the actual variable names... ------- 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
-
Actually, it's proprietary code, so I couldn't use the actual variable names... ------- 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
John Simmons / outlaw programmer wrote: Actually, it's proprietary code Well, I wouldn't want to be the owner... :) I see dumb people
-
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;
You're right - it's a stretch. For what it's worth, templates don't come into play in this particular instance. And it's a pain in the ass to have to search for the value of something when it's used to control incrementing/decrementing, especially if you are expecting to find it defined as a constant, and not an enum... ------- 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
-
:omg: I hope those contants are not actually named a, b, c, d, etc. You're right though, constant values should be declared with
const int
. I faced this issue recently in C# where enums cannot used in place of ints (without casting). Regards, Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
But that doesn't work well with some types of classes, such as H file only classes where you can't just define the value of the const int anywhere. This is another case where the ivory tower and the real world are in conflict. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
It's just that you did vent only a little :-D The tigress is here :-D
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
-
But that doesn't work well with some types of classes, such as H file only classes where you can't just define the value of the const int anywhere. This is another case where the ivory tower and the real world are in conflict. Tim Smith I'm going to patent thought. I have yet to see any prior art.
Tim Smith wrote: But that doesn't work well with some types of classes, such as H file only classes where you can't just define the value of the const int anywhere. That's true. A workaround for those cases is to declare and initialize the constant outside the class (at namespace scope). Regards, Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
-
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.