Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Strong Bad Programming

Strong Bad Programming

Scheduled Pinned Locked Moved The Lounge
tutorial
33 Posts 19 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • realJSOPR Offline
    realJSOPR Offline
    realJSOP
    wrote on last edited by
    #1

    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

    L R N G D 10 Replies Last reply
    0
    • realJSOPR realJSOP

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It's just that you did vent only a little :-D The tigress is here :-D

      realJSOPR 1 Reply Last reply
      0
      • realJSOPR realJSOP

        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

        R Offline
        R Offline
        Roger Wright
        wrote on last edited by
        #3

        It was probably written by a frustrated MBASIC programmer who couldn't find an equivalent for 100 DATA 4,0,4,25,0,0,256 120 READ A,B,C,D,E,F,G I've felt much better since I gave up hope.

        1 Reply Last reply
        0
        • realJSOPR realJSOP

          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

          N Offline
          N Offline
          Navin
          wrote on last edited by
          #4

          Wow, that's a great tip, I'm going to have to start using enums in that way. :-D Sometimes I feel like I'm a USB printer in a parallel universe.

          1 Reply Last reply
          0
          • realJSOPR realJSOP

            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

            G Offline
            G Offline
            Gary Wheeler
            wrote on last edited by
            #5

            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 enumeration enum 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;

            realJSOPR R 2 Replies Last reply
            0
            • realJSOPR realJSOP

              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

              D Offline
              D Offline
              Daniel Turini
              wrote on last edited by
              #6

              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

              realJSOPR 1 Reply Last reply
              0
              • realJSOPR realJSOP

                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

                A Offline
                A Offline
                Alvaro Mendez
                wrote on last edited by
                #7

                :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.

                T 1 Reply Last reply
                0
                • D Daniel Turini

                  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

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  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

                  D 1 Reply Last reply
                  0
                  • realJSOPR realJSOP

                    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

                    D Offline
                    D Offline
                    Daniel Turini
                    wrote on last edited by
                    #9

                    John Simmons / outlaw programmer wrote: Actually, it's proprietary code Well, I wouldn't want to be the owner... :) I see dumb people

                    1 Reply Last reply
                    0
                    • G Gary Wheeler

                      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 enumeration enum 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;

                      realJSOPR Offline
                      realJSOPR Offline
                      realJSOP
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • A Alvaro Mendez

                        :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.

                        T Offline
                        T Offline
                        Tim Smith
                        wrote on last edited by
                        #11

                        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.

                        A 1 Reply Last reply
                        0
                        • L Lost User

                          It's just that you did vent only a little :-D The tigress is here :-D

                          realJSOPR Offline
                          realJSOPR Offline
                          realJSOP
                          wrote on last edited by
                          #12

                          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

                          L R 2 Replies Last reply
                          0
                          • T Tim Smith

                            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.

                            A Offline
                            A Offline
                            Alvaro Mendez
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • realJSOPR realJSOP

                              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

                              C Offline
                              C Offline
                              cmk
                              wrote on last edited by
                              #14

                              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

                              1 Reply Last reply
                              0
                              • G Gary Wheeler

                                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 enumeration enum 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;

                                R Offline
                                R Offline
                                Ravi Bhavnani
                                wrote on last edited by
                                #15

                                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

                                G 1 Reply Last reply
                                0
                                • realJSOPR realJSOP

                                  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

                                  G Offline
                                  G Offline
                                  Gabriel 2
                                  wrote on last edited by
                                  #16

                                  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: ... } }

                                  R A M 3 Replies Last reply
                                  0
                                  • G Gabriel 2

                                    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: ... } }

                                    R Offline
                                    R Offline
                                    Roger Wright
                                    wrote on last edited by
                                    #17

                                    Yuck!:omg: Though to be honest, I've seen/coded worse...:-O I've felt much better since I gave up hope.

                                    1 Reply Last reply
                                    0
                                    • realJSOPR realJSOP

                                      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

                                      M Offline
                                      M Offline
                                      Michael Dunn
                                      wrote on last edited by
                                      #18

                                      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 ----

                                      N 1 Reply Last reply
                                      0
                                      • G Gabriel 2

                                        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: ... } }

                                        A Offline
                                        A Offline
                                        Andy Brummer
                                        wrote on last edited by
                                        #19

                                        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.

                                        R J 2 Replies Last reply
                                        0
                                        • G Gabriel 2

                                          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: ... } }

                                          M Offline
                                          M Offline
                                          markkuk
                                          wrote on last edited by
                                          #20

                                          It looks like an attempt to implement coroutines in C macros. It's actually a respectable programming technique (it's described in Knuth), but not fashionable in modern environments.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups