473,588 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Oct 17 '06 #1
5 4508

You need to look at the

State
Decorator

Design Patterns
http://www.dofactory.com/Patterns/Patterns.aspx
The "Head First" design pattern book .. has a Decorator example based on
Coffee.

See
http://www.oreilly.com/catalog/hfdes...apter/ch03.pdf
"Phuff" <pc*****@gmail. comwrote in message
news:11******** *************@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?

Oct 17 '06 #2

"Phuff" <pc*****@gmail. comwrote in message
news:11******** *************@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.
Oct 17 '06 #3
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*****@gmail. comwrote in message
news:11******** *************@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.
Oct 17 '06 #4
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?
Oct 17 '06 #5
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*****@gmail. comwrote in message
news:11******** *************@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.
Oct 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
14127
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the appropriate document, or post an example? I don't relish the idea especially long if-else statements. Joe
10
9567
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the 'case' code snippet does not. (the code's function is illustrative.) ////////////////////////////////////////// //////// working 'if/else' switch //////// //////////////////////////////////////////
18
3438
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
2
4399
by: MJ | last post by:
Hi We can use the switch statement, or if else statement instead of switch One more method is there which can replace the switch using the function pointer or the array of function pointer. If any one has any idea how to do it , could guide me MJ
13
7539
by: Michael Griebe | last post by:
Simple question. I am optimizing some C++ code and I'd like to know which is faster (or if there is any difference at all) between using a switch statement or nested else-ifs. I'm partial to else-if. I know to put the if statement that is most likely to be true at the top of the else-if chain -so as to minimize checks. I've searched around online and mainly found answers to this question for Java programmers. Thanks in advance,...
1
510
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} Inside this method I determine what object I should call the Persist method on using a switch statement (not very OO). switch (otype)
21
7715
by: aaron80v | last post by:
Hi, A simple question: In ANSI-C, how to specify a string as the expression for switch statement. eg switch (mystr) { case "ABC" : bleh... break;
10
2424
by: anon.asdf | last post by:
Here's a reminder of duff's device: /*************************************/ #include <stdio.h> #define STEP 8 #define MAX_LEN STEP*4+1 #define SOURCE_LEN 28
13
11797
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
0
7862
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6634
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5729
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3847
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1196
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.