Decomposition of complex method with switch case

replace switch statement with polymorphism java
refactor switch statement design pattern c#
refactor switch statement design pattern java
replace conditional with polymorphism
refactoring if-else
prefer polymorphism over switch case to encapsulate and delegate complex operations
refactoring method
replace if/else with pattern c#

I have a logic in my application that depends on type of the parameter and now it works like:

   switch (parameter.getType()) {
                case DOUBLE:
                    countOfParameters = parameter.getDoubleValueCount();
                    if (countOfParameters == 1) {
                        propertiesBuilder.addProperty(parameter.getName(), parameter.getDoubleValue(0));
                    } else if (countOfParameters > 1) {
                        Double[] doubleValues = new Double[countOfParameters];
                        for (int kj = 0; kj < countOfParameters; kj++) {
                            doubleValues[kj] = parameter.getDoubleValue(kj);
                        }
                        propertiesBuilder.addProperty(parameter.getName(), doubleValues);
                    }
                    break;
                case BOOLEAN:
                    countOfParameters = parameter.getBoolValueCount();
                    if (countOfParameters == 1) {
                        propertiesBuilder.addProperty(parameter.getName(), parameter.getBoolValue(0));
                    } else if (countOfParameters > 1) {
                        Boolean[] booleanValues = new Boolean[countOfParameters];
                        for (int kj = 0; kj < countOfParameters; kj++) {
                            booleanValues[kj] = parameter.getBoolValue(kj);
                        }
                        propertiesBuilder.addProperty(parameter.getName(), booleanValues);
                    }
                    break;
                case STRING:
                    countOfParameters = parameter.getStringValueCount();
                    if (countOfParameters == 1) {
                        propertiesBuilder.addProperty(parameter.getName(), parameter.getStringValue(0));
                    } else if (countOfParameters > 1) {
                        String[] stringValues = new String[countOfParameters];
                        for (int kj = 0; kj < countOfParameters; kj++) {
                            stringValues[kj] = parameter.getStringValue(kj);
                        }
                        propertiesBuilder.addProperty(parameter.getName(), stringValues);
                        break;
                    }
                case INTEGER:
                    countOfParameters = parameter.getIntValueCount();
                    if (countOfParameters == 1) {
                        propertiesBuilder.addProperty(parameter.getName(), parameter.getIntValue(0));
                    } else if (countOfParameters > 1) {
                        Integer[] integerValues = new Integer[countOfParameters];
                        for (int kj = 0; kj < countOfParameters; kj++) {
                            integerValues[kj] = parameter.getIntValue(kj);
                        }
                        propertiesBuilder.addProperty(parameter.getName(), integerValues);
                    }
                    break;
                case LOCALDATE:
                    countOfParameters = parameter.getStringValueCount();
                    if (countOfParameters == 1) {
                        propertiesBuilder.addProperty(parameter.getName(), DateHelper.parseDate(parameter.getStringValue(0)));
                    } else if (countOfParameters > 1) {
                        LocalDate[] dateValues = new LocalDate[countOfParameters];
                        for (int kj = 0; kj < countOfParameters; kj++) {
                            dateValues[kj] = DateHelper.parseDate(parameter.getStringValue(kj));
                        }
                        propertiesBuilder.addProperty(parameter.getName(), dateValues);
                    }
                    break;
            }

As you can see I have the same logic and the only thing that changes - is a method for parsing value. Is it possible to get rid of duplicate code? I guess it is possible to make some Map of Functional interface Function, but not sure how it should be done. Any suggestions?

If I understood you correctly, than something like this may be:

Map<Type, Consumer<Builder>> map = Map.of(
     BOOLEAN, x -> x.add(BOOLEAN.parseBool()),
     STRING, x -> x.add(STRING.parseString())
);


map.get(type).accept(builder);

Decompose Conditional, Problem: You have a complex conditional (if-then/else or switch). Solution: Decompose the complicated parts of the conditional into separate methods: the� In such case either we can use lengthy if..else-if statement or switch case. The problem with lengthy if..else-if is that it becomes complex when we have several conditions. The switch case is a clean and efficient method of handling such scenarios. The syntax of Switch case statement:

Not sure what you need but you can use generic types.

public class Element<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}

You can use in your function:

public Object parse(Element t){
    if( t.get() instanceof Boolean )
        return parseBoolean(t);
    else if ( t.get() instanceof String )
        return parseString(t);
}


Switch Statements, You have a complex switch operator or sequence of if statements. If this case, you can break that method into multiple smaller methods with Replace� A decomposition method for solving complex distributed energy system optimization problems was developed and tested. The test cases described in this article show how the strength of the method is heavily dependent on the case being analysed.

First the mapping DOUBLE-to-Double and so on, can be done in the enum:

enum ValueType {
    DOUBLE(Double.class), // Or Double[].class
    BOOLEAN(Boolean.class),
    ...;

    public final Class<?> type;

    ValueType(Class<?> type) {
        this.type = type;
    }
}

Evidently the code is already too specialized, whereas actually the exact type is irrelevant. At this level a value could just be an Object and one could have Object[].

Consequently some deep refactoring would be ideal.

ValueType vt = parameter.getType();
Class<?> t = vt.type;

Object array = Array.newInstance(t, countOfParameters);
// Actuallly Double[] or such.

int countOfParameters = parameter.getValueCount();
if (countOfParameters == 1) {
    propertiesBuilder.addProperty(parameter.getName(), parameter.getValue(0));
} else if (countOfParameters > 1) {
    Object array = Array.newInstance(t, countOfParameters);
    Class<?> componentT = array.getClass().getComponentType(); // == t

    Object[] values = new Object[countOfParameters];
    for (int kj = 0; kj < countOfParameters; kj++) {
        Array.set(array, kj, parameter.getValue(kj));
        values[kj] = parameter.getValue(kj);
    }
    propertiesBuilder.addProperty(parameter.getName(), values); // or array
}

I have added a bit of reflecting code (Array), which hopefully is not needed.

Refactoring Reducers Example, #Refactoring Reducer Logic Using Functional Decomposition and Reducer Composition switch (action.type) { That function is fairly short, but already becoming overly complex. Next, we can split each specific case into its own function:. If the Business Analysts conducted a thorough use case analysis, all functions of a software are linked to actors. Functional Decomposition. Functional decomposition means to take a problem and divide it into subproblems, until reaching a convenient level or depth at which the problem can be solved or can be seen atomic.

Control Flow — The Swift Programming Language (Swift 5.3), Swift's switch statement is considerably more powerful than its counterpart in many to temporary constants or variables for use within the case's body, and complex value) tuple when the dictionary is iterated, and you can decompose the (key, value) The example above uses a very simple approach to dice rolling. Such decomposition has some implications in statistical applications for the case that n = m. In fact, when Σ or Σ ̂ is known, and in consequence when Q or Q ̂ can be obtained, we can replace the structure matrix A in ( 1 ) with Q or Q ̂ to have X = Q Z or X = Q ̂ Ẑ , depending on the availability Q or Q ̂ , so that, because n = m

The Complete Guide to the Java SE 12 Extended Switch Statement , The current design of Java's switch statement closely follows languages such new Java APIs and its features, and even for prototyping complex code. in the previous method for the default case of the switch statement to identifies planning all the way through application and database decomposition. The decomposition method works well when there are few complicating variables, and In this case, any strip with a width exceeding the interaction distance

[PDF] Decomposition & Style, In C the unit of decomposition is the function and the ADT. In C++ the When is a subtask sufficiently complex or independent to merit The break in the body can detect the exception case which comes up during the iteration. If you ever exploit the fall-through property of the switch, your documentation should definitely. A switch-block becomes complex very easily. All code is placed in one method and parts of it are used multiple times. In a lot of cases, a switch-block is based on an enum. Next to that, it's not possible to use a fall-through in C#. The only way to go from case to case is the use of the goto statement, which isn't a good idea when reducing