Linked Questions

12
votes
8answers
617 views

Proper design for a class with one method that can vary between customers

I have a class used to process customer payments. All but one of the methods of this class are the same for every customer, except for one that calculates (for example) how much the customer's user ...
12
votes
7answers
7k views

Can the Strategy pattern be implemented without significant branching?

The Strategy pattern works well to avoid huge if...else constructs and make it easier to add or replace functionality. However, it still leaves one flaw in my opinion. It seems like in every ...
9
votes
6answers
5k views

How to reduce a switch in a switch statement?

So I'm making a method to create a salutation line based on two people from a database. There are four parameters: the two names (name1 and name2) and the two genders (gender and gender2). For every ...
4
votes
5answers
4k views

Switch vs Polymorphism

I know this is a classic problem but it's so hard to chose the right way or the best. Let me present you a simplified version of the code. Design 1 using switch : public class Disease { private ...
9
votes
3answers
8k views

How to refactor an application with multiple switch cases?

I have an application which takes an integer as input and based on the input calls static methods of different classes. Every time a new number is added, we need to add another case and call a ...
3
votes
3answers
2k views

How to handle large switch statement running several different commands?

I've been tasked with refactoring a console application, which is constantly running on a server and receiving messages from a service bus. Right now, it just parses the incoming message, and based ...
2
votes
3answers
4k views

Most efficient method for large switch statements

Let's say you have many paths that an application can take at a certain point based on the value of a specific input (for example a simple int). Is there a certain method that is most efficient for ...
1
vote
3answers
715 views

How to shorten the case statement from hades? [duplicate]

I'm refactoring code and have reached a horribly gigantic switch statement. Every single API method available to end users is represented as an enum and we have a switch statement iterating over the ...
4
votes
2answers
4k views

Is it possible to avoid enormously big switch in that case? [duplicate]

I'm writing a simple chess-related code with intention to write it clearly (performance doesn't matter at all). And this method I have doesn't look clean to me at all: public static Piece ...
1
vote
2answers
239 views

How is a dictionary better than a big switch, when mapping data to action? [duplicate]

Often times we see stuff like that in code: void myFunction(string someValue) { if (someValue == "a") { // ... } else if (someValue == "b") { // ... } else if (someValue == ...
1
vote
1answer
377 views

Should I replace switch case of functions determined by options in previous activity?

For example, assume my app has 3 steps with 3 pages: Step 1 : MainActivity: choose color Step 2 : EditTextActivity: input text Step 3 : GreenActivity: display color and input text In ...
0
votes
1answer
79 views

Refactoring – Removing switch statement [duplicate]

There is a base class Product having all the generic properties and methods which a product should have. abstract class Product { public abstract function process(); } Now, These are the few ...
0
votes
1answer
230 views

Simplifying code of a calculator (help) [duplicate]

This is part of the code of a calculator that works on command line. It works fine and the math is correct but it's a little redundant: switch(Operator) { case "+": result = num1 + num2; ...
0
votes
1answer
144 views

Call methods of subclass when iterating over base type collection [duplicate]

Imagine this code: using System; public enum BaseClassType { DerivedA, DerivedB } public abstract class BaseClass { public BaseClassType Type; public BaseClass(BaseClassType type) ...
-4
votes
1answer
77 views

How to fix a method with a myriad of switch statements? [duplicate]

I need to test whether data returned from a search is correct. Several operators can be used for search (term, prefix, wildcard etc.), several fields can be searched (firstName, lastName, email, ...

15 30 50 per page