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. Other Discussions
  3. The Weird and The Wonderful
  4. switch statement

switch statement

Scheduled Pinned Locked Moved The Weird and The Wonderful
12 Posts 5 Posters 4 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.
  • O Oshtri Deka

    2200 lines of cases? Impressive, at least to me, humble employee of small companies.

    T Offline
    T Offline
    Tony Wesley
    wrote on last edited by
    #3

    Oshtri Deka wrote:

    2200 lines of cases? Impressive, at least to me, humble employee of small companies

    I'm not happy with 2200 lines of cases. I'm seeking to clean this up and reduce complexity. Things tend to just keeping growing. I'm curious, do you see the problem with my posted code?

    O X 2 Replies Last reply
    0
    • T Tony Wesley

      I was cleaning up the code in a parser. There is a switch/case statement that spans over 2,200 lines. I removed a bunch of exits from various case statements and moved it up as a global test on the errorCode. It just didn't want to exit when the errorCode was set. :-O

      state=1;
      do
      {
      result = token( tokstr, FALSE, NULL, FALSE);
      switch(state)
      {
      if (errorCode != 0)
      return FALSE;
      case 1:
      switch(result)
      {
      // 2000+ lines snipped
      } /* switch(state) */

      } while( result != Keyword::EOFTOK ); /* do */

      “Although distant in time, Homobonus does in fact figure as a saint for the Church and society of our time.” -- Pope John Paul II

      D Offline
      D Offline
      DavidNohejl
      wrote on last edited by
      #4
            switch(state)
            {
               if (errorCode != 0)
                  return FALSE;
               case 1:
                  switch(result)
                  {
      

      My guess is that execution jumps to one of case labels and skip the if statement entirely?


      [My Blog]
      "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
      "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

      L 1 Reply Last reply
      0
      • T Tony Wesley

        Oshtri Deka wrote:

        2200 lines of cases? Impressive, at least to me, humble employee of small companies

        I'm not happy with 2200 lines of cases. I'm seeking to clean this up and reduce complexity. Things tend to just keeping growing. I'm curious, do you see the problem with my posted code?

        O Offline
        O Offline
        Oshtri Deka
        wrote on last edited by
        #5

        I was sarcastic, though I am impressed that someone wrote so many lines of code for one switch-case block. What does that application do? checking before first case?

        T 1 Reply Last reply
        0
        • D DavidNohejl
                switch(state)
                {
                   if (errorCode != 0)
                      return FALSE;
                   case 1:
                      switch(result)
                      {
          

          My guess is that execution jumps to one of case labels and skip the if statement entirely?


          [My Blog]
          "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
          "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

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

          My compiler woud throw exception

          T 1 Reply Last reply
          0
          • T Tony Wesley

            I was cleaning up the code in a parser. There is a switch/case statement that spans over 2,200 lines. I removed a bunch of exits from various case statements and moved it up as a global test on the errorCode. It just didn't want to exit when the errorCode was set. :-O

            state=1;
            do
            {
            result = token( tokstr, FALSE, NULL, FALSE);
            switch(state)
            {
            if (errorCode != 0)
            return FALSE;
            case 1:
            switch(result)
            {
            // 2000+ lines snipped
            } /* switch(state) */

            } while( result != Keyword::EOFTOK ); /* do */

            “Although distant in time, Homobonus does in fact figure as a saint for the Church and society of our time.” -- Pope John Paul II

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

            why is there a switch inside switch This style of progrmaing introduce more compiling errors than the bugs

            T 1 Reply Last reply
            0
            • O Oshtri Deka

              I was sarcastic, though I am impressed that someone wrote so many lines of code for one switch-case block. What does that application do? checking before first case?

              T Offline
              T Offline
              Tony Wesley
              wrote on last edited by
              #8

              Oshtri Deka wrote:

              I was sarcastic, though I am impressed that someone wrote so many lines of code for one switch-case block. What does that application do?

              That module has to do with parsing a parameter file. It probably was state-of-the-art in 1983. It has just grown and grown. We need an option at to handle FOOBAR? No problem, add a #DEFINE FOOBAR 1742 and one more case statement. Continue the process for almost a quarter century. The application classifies inpatient hospital records into disease categories and stages of the disease. The portion of the program that does that has been modernized.

              checking before first case?

              Yeah, that code was unreachable. I checked it into source control. It worked great if there were no errors. :sigh:

              1 Reply Last reply
              0
              • L Lost User

                My compiler woud throw exception

                T Offline
                T Offline
                Tony Wesley
                wrote on last edited by
                #9

                bsaksida wrote:

                My compiler woud throw exception

                Are you sure? I think it's legal C++ albeit stupid programming. I wish it had thrown an exception. But Visual Studio was happy with it, as were the compliers on AIX, Solaris, and HP/UX.

                1 Reply Last reply
                0
                • L Lost User

                  why is there a switch inside switch This style of progrmaing introduce more compiling errors than the bugs

                  T Offline
                  T Offline
                  Tony Wesley
                  wrote on last edited by
                  #10

                  bsaksida wrote:

                  why is there a switch inside switch

                  Well, it fits the logic. When the parser is starting a statement, the state is 1. Then it does a switch statement based on the first token. The code all works, I'm just not happy with how complicated it is. I used SourceMonitor[^] to get a metric on the complexity. Mike Swanson[^] suggests that Anything with a greater complexity than 10 or so is an excellent candidate for simplification and refactoring. Well, the code in question has a complexity number of 638 as measured by SourceMonitor. You don't go from 638 to 10 in one step. So one of the things I did was to have a single exit with the error code at the top of the case statement. Or at least, I tried to. My logic error was a little embarrassing. But an error like that is too good to keep to myself. I had to share it.

                  1 Reply Last reply
                  0
                  • T Tony Wesley

                    Oshtri Deka wrote:

                    2200 lines of cases? Impressive, at least to me, humble employee of small companies

                    I'm not happy with 2200 lines of cases. I'm seeking to clean this up and reduce complexity. Things tend to just keeping growing. I'm curious, do you see the problem with my posted code?

                    X Offline
                    X Offline
                    xenonysf
                    wrote on last edited by
                    #11

                    You should construct a data structure and keep the possible values in case statements in an ordered way in your data structure; then your incoming value should be compared with the values inside your data structure in binary search fashion... this will be faster and save you from time consuming sequential searching

                    T 1 Reply Last reply
                    0
                    • X xenonysf

                      You should construct a data structure and keep the possible values in case statements in an ordered way in your data structure; then your incoming value should be compared with the values inside your data structure in binary search fashion... this will be faster and save you from time consuming sequential searching

                      T Offline
                      T Offline
                      Tony Wesley
                      wrote on last edited by
                      #12

                      xenonysf wrote:

                      You should construct a data structure and keep the possible values in case statements in an ordered way in your data structure; then your incoming value should be compared with the values inside your data structure in binary search fashion... this will be faster and save you from time consuming sequential searchin

                      I agree with what you say but not for the reasons you give. A switch/case statement has better performance than a sequence of if/else statements. The compiler may generate a jump table. For large tables, some use binary searches. See http://www.eventhelix.com/RealtimeMantra/Basics/CToAssemblyTranslation3.htm[^]. So performance isn't the driving factor here. But I do agree that the cases -- where possible -- should be encapsulated in a data structure. We would like to have actions for the various cases in a data structure to simply the logic. Complexity leads to increased probability of error. We have unneccessary complexity. However, it takes a while to untangle code that has grown for decades.

                      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