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.
  • 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
                      • realJSOPR realJSOP

                        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 Offline
                        L Offline
                        l a u r e n
                        wrote on last edited by
                        #21

                        :laugh:


                        "there is no spoon"
                        biz stuff   about me

                        1 Reply Last reply
                        0
                        • M Michael Dunn

                          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 Offline
                          N Offline
                          Nemanja Trifunovic
                          wrote on last edited by
                          #22

                          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.

                          1 Reply Last reply
                          0
                          • A Andy Brummer

                            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 Offline
                            R Offline
                            Roger Wright
                            wrote on last edited by
                            #23

                            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.

                            C 1 Reply Last reply
                            0
                            • realJSOPR realJSOP

                              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

                              R Offline
                              R Offline
                              Rick York
                              wrote on last edited by
                              #24

                              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.

                              realJSOPR 1 Reply Last reply
                              0
                              • R Rick York

                                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.

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

                                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

                                1 Reply Last reply
                                0
                                • R Roger Wright

                                  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.

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

                                  :-D:) thanks for the grin. ...cmk Save the whales - collect the whole set

                                  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

                                    J Offline
                                    J Offline
                                    James Pullicino
                                    wrote on last edited by
                                    #27

                                    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.

                                    G 1 Reply Last reply
                                    0
                                    • R Ravi Bhavnani

                                      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 Offline
                                      G Offline
                                      Gary Wheeler
                                      wrote on last edited by
                                      #28

                                      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;

                                      realJSOPR 1 Reply Last reply
                                      0
                                      • G Gary Wheeler

                                        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;

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

                                        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

                                        G 1 Reply Last reply
                                        0
                                        • realJSOPR realJSOP

                                          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

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

                                          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;

                                          realJSOPR 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