Switch statement alternative?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phuff

    Switch statement alternative?

    Hey all, I need some direction help. I have a switch case statement
    that is seemingly my only option right now, but its too large and not
    easy to maintain the code. Here goes...

    I have part descriptions (ie. 3/8" X ____" NYLON ALL-THREAD RODS...or
    ____" x ____" X ____" ____ WRAPPED MULLION) that I need to replace the
    blank lines on. I do a switch on the part id.

    I have 53 in all out of the parts list. What fills the blank is
    dependent on information specific to the customized product being
    built. Getting the information is not a problem...the problem is that
    I cannot do any generalization. Almost every part is different and
    what information the blank is based on is different for each part. I
    have no generalized function aside from the one that fills the blank.
    I just do a switch statement and have specialized business logic code
    for each part.

    I'm sure you can see the problem...what happens if we change a part
    description in the system, or add a new part, or remove one? I have to
    change the code. This is not the best practice, but I see no
    alternative at the moment. Everything is so freaking customizable
    here! Please help, any ideas?

  • sloan

    #2
    Re: Switch statement alternative?


    You need to look at the

    State
    Decorator

    Design Patterns


    C# Design Patterns are proven solutions to common software design problems. Design Patterns help you write code that is more flexible, adaptable, and easier to maintain. Examples come with 100% source code.



    The "Head First" design pattern book .. has a Decorator example based on
    Coffee.

    See



    "Phuff" <pc.huff@gmail. comwrote in message
    news:1161098675 .292520.69190@m 7g2000cwm.googl egroups.com...
    Hey all, I need some direction help. I have a switch case statement
    that is seemingly my only option right now, but its too large and not
    easy to maintain the code. Here goes...
    >
    I have part descriptions (ie. 3/8" X ____" NYLON ALL-THREAD RODS...or
    ____" x ____" X ____" ____ WRAPPED MULLION) that I need to replace the
    blank lines on. I do a switch on the part id.
    >
    I have 53 in all out of the parts list. What fills the blank is
    dependent on information specific to the customized product being
    built. Getting the information is not a problem...the problem is that
    I cannot do any generalization. Almost every part is different and
    what information the blank is based on is different for each part. I
    have no generalized function aside from the one that fills the blank.
    I just do a switch statement and have specialized business logic code
    for each part.
    >
    I'm sure you can see the problem...what happens if we change a part
    description in the system, or add a new part, or remove one? I have to
    change the code. This is not the best practice, but I see no
    alternative at the moment. Everything is so freaking customizable
    here! Please help, any ideas?
    >

    Comment

    • pvdg42

      #3
      Re: Switch statement alternative?


      "Phuff" <pc.huff@gmail. comwrote in message
      news:1161098675 .292520.69190@m 7g2000cwm.googl egroups.com...
      Hey all, I need some direction help. I have a switch case statement
      that is seemingly my only option right now, but its too large and not
      easy to maintain the code. Here goes...
      >
      I have part descriptions (ie. 3/8" X ____" NYLON ALL-THREAD RODS...or
      ____" x ____" X ____" ____ WRAPPED MULLION) that I need to replace the
      blank lines on. I do a switch on the part id.
      >
      I have 53 in all out of the parts list. What fills the blank is
      dependent on information specific to the customized product being
      built. Getting the information is not a problem...the problem is that
      I cannot do any generalization. Almost every part is different and
      what information the blank is based on is different for each part. I
      have no generalized function aside from the one that fills the blank.
      I just do a switch statement and have specialized business logic code
      for each part.
      >
      I'm sure you can see the problem...what happens if we change a part
      description in the system, or add a new part, or remove one? I have to
      change the code. This is not the best practice, but I see no
      alternative at the moment. Everything is so freaking customizable
      here! Please help, any ideas?
      >
      I may not understand your issue correctly, but it sounds as if you are
      embedding data in the code in your switch?
      Given that you have 53 + or - part numbers with multiple data fields
      associated with each part, can you not use a struct or some other ADT to set
      up a description of each part, then use a collection that allows key-value
      pairs (part number being the unique key) to manage the structs in memory?
      If you need to store the part info between program runs, a file or database
      table that can be read into your collection at program start should work.
      That way changes to the data are made to the file/table and your code does
      not change.


      Comment

      • Phuff

        #4
        Re: Switch statement alternative?

        Thanks for the responses. I'm reading the deswign practices right
        now...this is the type of answer I was looking for, thank you.

        As to pvdg42: No, I am not storing data in my runtime. The data is
        stored in SQL Server. I am trying to automate the generation of a
        materials list for a manufacturing company that has one standard: there
        are few standards and everything is customizable. I get the parts for
        each job based on xml files etc etc....anyway now that I have all the
        parts I need to calculate quantites. Some of the parts need their
        descriptions altered based on what's going on with the job...for
        instance a dimension based on the thickness of the wall, a color based
        on surrounding panel finishes. We want this list to be 100% automatic
        so I need to replace the blank received from the db with the proper
        descrtiption. Almost every part is very different and based on a
        combination of different factors. My problem was, How do I code for
        this without the need to change the code for each part that might
        change in the future. The book chapter and information from the first
        response's links is, seeming, what I need. I'll post if I have any
        other questions

        Thanks!
        pvdg42 wrote:
        "Phuff" <pc.huff@gmail. comwrote in message
        news:1161098675 .292520.69190@m 7g2000cwm.googl egroups.com...
        Hey all, I need some direction help. I have a switch case statement
        that is seemingly my only option right now, but its too large and not
        easy to maintain the code. Here goes...

        I have part descriptions (ie. 3/8" X ____" NYLON ALL-THREAD RODS...or
        ____" x ____" X ____" ____ WRAPPED MULLION) that I need to replace the
        blank lines on. I do a switch on the part id.

        I have 53 in all out of the parts list. What fills the blank is
        dependent on information specific to the customized product being
        built. Getting the information is not a problem...the problem is that
        I cannot do any generalization. Almost every part is different and
        what information the blank is based on is different for each part. I
        have no generalized function aside from the one that fills the blank.
        I just do a switch statement and have specialized business logic code
        for each part.

        I'm sure you can see the problem...what happens if we change a part
        description in the system, or add a new part, or remove one? I have to
        change the code. This is not the best practice, but I see no
        alternative at the moment. Everything is so freaking customizable
        here! Please help, any ideas?
        I may not understand your issue correctly, but it sounds as if you are
        embedding data in the code in your switch?
        Given that you have 53 + or - part numbers with multiple data fields
        associated with each part, can you not use a struct or some other ADT to set
        up a description of each part, then use a collection that allows key-value
        pairs (part number being the unique key) to manage the structs in memory?
        If you need to store the part info between program runs, a file or database
        table that can be read into your collection at program start should work.
        That way changes to the data are made to the file/table and your code does
        not change.

        Comment

        • Martin Z

          #5
          Re: Switch statement alternative?

          Well, the agile, OOP way to do it would be to make each part type it's
          own class, and then just have each class implement a "GetDescription "
          method. Classes with common features get refactored into having common
          parents, and such classes would generally have common descriptions, or
          at least similar-enough descriptions that you could factor-out the
          commonalities.

          But that wouldn't acheive the goals you outlined - config-oriented
          instead of recompile-oriented, and no searching for the description
          strings if you want to change them all up. So here's my
          non-polymorphic, config-oriented approach

          So here's how I'd do it:
          1) each object exposes a
          Dictionary<stri ng, stringGetBusine ssFields()
          method, thus implementing the ICanGetBusiness Fields() interface.

          2) Then, you have an XML-serializable class called
          DescriptionFact ory(), which is just a serliazed wrapper around a
          Dictionary-based version of String.Format.. .. something like this (I
          got carried away and did most of the implementation) :

          interface IDescribable
          {
          string DescriberName() ;
          Dictionary <string, stringGetBusine ssFields();
          }

          class Describer
          {
          [XmlAttribute()]
          public string Name;

          [XmlArrayItem("k ey", typeof(string))]
          public string[] Keys;

          public string Describe(string formatString, IDescribable obj)
          {
          Dictionary <string, stringfields =
          obj.GetBusiness Fields();
          string[] formatValues = new string[fields.Count];
          int i=0;
          foreach (string key in Keys)
          {
          formatValues[i] = fields[key];
          i++;
          }
          return string.Format(f ormatString, formatValues);
          }
          }

          class DescriberManage r
          {
          public Describer[] Describers;
          public string FormatString;

          private Dictionary<stri ng, Describerdict = null;

          //The meat of the function. Call this to get description from
          object.
          public string GetDescription( IDescribable obj)
          {
          //lazily construct Description dictionary
          if (dict == null)
          {
          foreach (Describer desc in Describers)
          {
          dict[desc.Name] = desc;
          }
          }

          return dict[obj.DescriberNa me()].Describe(Forma tString,
          obj);
          }
          }


          The descriptionmana ger and it's child descriptions are serialized out
          of your configuration system. IDescribable is implemented by your part
          type(s). Of course, this is offered with no warranty - I haven't even
          included an exception handler (or TryGetValue) for the very real
          possibility of a KeyNotFoundExce ption.

          Phuff wrote:
          Hey all, I need some direction help. I have a switch case statement
          that is seemingly my only option right now, but its too large and not
          easy to maintain the code. Here goes...
          >
          I have part descriptions (ie. 3/8" X ____" NYLON ALL-THREAD RODS...or
          ____" x ____" X ____" ____ WRAPPED MULLION) that I need to replace the
          blank lines on. I do a switch on the part id.
          >
          I have 53 in all out of the parts list. What fills the blank is
          dependent on information specific to the customized product being
          built. Getting the information is not a problem...the problem is that
          I cannot do any generalization. Almost every part is different and
          what information the blank is based on is different for each part. I
          have no generalized function aside from the one that fills the blank.
          I just do a switch statement and have specialized business logic code
          for each part.
          >
          I'm sure you can see the problem...what happens if we change a part
          description in the system, or add a new part, or remove one? I have to
          change the code. This is not the best practice, but I see no
          alternative at the moment. Everything is so freaking customizable
          here! Please help, any ideas?

          Comment

          • Phuff

            #6
            Re: Switch statement alternative?

            I just solved my problem. I will replace the blanks in the database
            with tokens...%exter iorFin% or %interiorFin% or %wallThick% or
            %panelHeight% etc etc. Then I just need to write a simple parser to
            replace those tokens instead of generic blanks. thanks!!



            Phuff wrote:
            Thanks for the responses. I'm reading the deswign practices right
            now...this is the type of answer I was looking for, thank you.
            >
            As to pvdg42: No, I am not storing data in my runtime. The data is
            stored in SQL Server. I am trying to automate the generation of a
            materials list for a manufacturing company that has one standard: there
            are few standards and everything is customizable. I get the parts for
            each job based on xml files etc etc....anyway now that I have all the
            parts I need to calculate quantites. Some of the parts need their
            descriptions altered based on what's going on with the job...for
            instance a dimension based on the thickness of the wall, a color based
            on surrounding panel finishes. We want this list to be 100% automatic
            so I need to replace the blank received from the db with the proper
            descrtiption. Almost every part is very different and based on a
            combination of different factors. My problem was, How do I code for
            this without the need to change the code for each part that might
            change in the future. The book chapter and information from the first
            response's links is, seeming, what I need. I'll post if I have any
            other questions
            >
            Thanks!
            pvdg42 wrote:
            "Phuff" <pc.huff@gmail. comwrote in message
            news:1161098675 .292520.69190@m 7g2000cwm.googl egroups.com...
            Hey all, I need some direction help. I have a switch case statement
            that is seemingly my only option right now, but its too large and not
            easy to maintain the code. Here goes...
            >
            I have part descriptions (ie. 3/8" X ____" NYLON ALL-THREAD RODS...or
            ____" x ____" X ____" ____ WRAPPED MULLION) that I need to replace the
            blank lines on. I do a switch on the part id.
            >
            I have 53 in all out of the parts list. What fills the blank is
            dependent on information specific to the customized product being
            built. Getting the information is not a problem...the problem is that
            I cannot do any generalization. Almost every part is different and
            what information the blank is based on is different for each part. I
            have no generalized function aside from the one that fills the blank.
            I just do a switch statement and have specialized business logic code
            for each part.
            >
            I'm sure you can see the problem...what happens if we change a part
            description in the system, or add a new part, or remove one? I have to
            change the code. This is not the best practice, but I see no
            alternative at the moment. Everything is so freaking customizable
            here! Please help, any ideas?
            >
            I may not understand your issue correctly, but it sounds as if you are
            embedding data in the code in your switch?
            Given that you have 53 + or - part numbers with multiple data fields
            associated with each part, can you not use a struct or some other ADT to set
            up a description of each part, then use a collection that allows key-value
            pairs (part number being the unique key) to manage the structs in memory?
            If you need to store the part info between program runs, a file or database
            table that can be read into your collection at program start should work.
            That way changes to the data are made to the file/table and your code does
            not change.

            Comment

            Working...