logo
down
shadow

Reducing complexity of large switch statements


Reducing complexity of large switch statements

By : Jaz Ramage
Date : October 18 2020, 03:08 PM
I think the issue was by ths following , If, by your example, this is just the case of choosing a mapped value from a key, a table or properties file would be a more appropriate way to handle this.
If you're talking about logic within the different switch statements, you might find that a rules engine would suit better.
code :


Share : facebook icon twitter icon
Reducing the cyclomatic complexity, multiple if statements

Reducing the cyclomatic complexity, multiple if statements


By : ajapyy
Date : March 29 2020, 07:55 AM
help you fix your problem At some point in your program, you will have to implement the logic:
If the new facility has a property defined, update the old facility accordingly If not, do not override the previous value from the old facility.
code :
public class Facility {

    public void setSomething(String something) {
        if (something != null) {
            this.something = something;
        }
    }

}
private Facility updateFacility(Facility newFacility, Facility oldFacility) {
    oldFacility.setSomething(newFacility.getSomething());
    // etc for the rest
}
Are macros for large switch statements faster than functions with large switch statements?

Are macros for large switch statements faster than functions with large switch statements?


By : pm86
Date : March 29 2020, 07:55 AM
it fixes the issue First, note that when you have code in a macro, the compiler must insert it inline in the calling code. When you make it a function, the compiler may insert it inline.
You should also understand what happens when you declare a function like:
code :
void Lex_Char(char* chPtr, Tag* tag) { ... }
static void Lex_Char(char* chPtr, Tag* tag) { ... }
Struck with Switch, reducing cyclomatic complexity java

Struck with Switch, reducing cyclomatic complexity java


By : sergeff
Date : March 29 2020, 07:55 AM
hop of those help? From Martin Fowler's Refactoring book
I can suggest following refactoring method:
code :
import java.util.HashMap;
import java.util.Map;

public class Refactoring {

    private static class IonStruct {};

    private static interface MessageBuilder {
        String  build(final String xId,final String yId, final IonStruct metadataStruct);
    }


    private static class NotActiveMessageBuilder implements MessageBuilder {

        private static final String YX_NON_ACTIVE_FMT = "Y %s X %s not active: status is inactive%n;";
        private static final String Y_NON_ACTIVE_FMT = "Y %s not active: status is inactive%n";

        @Override
        public String build(String xId, String yId, IonStruct metadataStruct) {
            return (xId != null) ? String.format(YX_NON_ACTIVE_FMT, yId, xId) : String.format(Y_NON_ACTIVE_FMT, yId) ;
        }

    }

    private static enum MessageTypeChine
    {
        instance();
        private final Map<String,MessageBuilder> builders;
        private MessageTypeChine() {
            this.builders = new HashMap<>();
            this.builders.put("NOT_ACTIVE", new NotActiveMessageBuilder());
        }

        public String constructMessage(final String reason, final String xId,
                final String yId, final IonStruct metadataStruct) {
            MessageBuilder builder = this.builders.get(reason);
            if(null != builder)
                return builder.build(xId, yId, metadataStruct);

            throw new UnsupportedOperationException("Message type is not supported");
        }
    }


    public static String constructMessage(final String reason, final String xId,
            final String yId, final IonStruct metadataStruct) {
        return MessageTypeChine.instance.constructMessage(reason, xId, yId, metadataStruct);
    }

}
Reducing complexity of code for if-else statements

Reducing complexity of code for if-else statements


By : user3023352
Date : March 29 2020, 07:55 AM
To fix this issue I would do early continue to avoid the repetition of command.execute(). I don't think it's worth to combine the conditions in one or creating another function for its sake.
code :
for(Command command : commands) {
  if (command instanceof AddCommand || command instanceof UpdateCommand) {
    if (isMaturityDateInPast() || paymentDueDate().isAfter(LocalDate.now())) {
        continue;
    }
  }
  command.execute();
}
Reducing the cyclomatic complexity, multiple if statements java

Reducing the cyclomatic complexity, multiple if statements java


By : Neal Wong
Date : March 29 2020, 07:55 AM
I hope this helps you . Your code would be easier to read, while it's just as "complex" (while it's not really that complex), if you:
Use a switch statement Extract the value of getNodeName() beforehand
code :
String value = formatter.getElementValueAfterNullCheckWithTrim((Element) eachTag);
String nodeName = eachTag.getNodeName();

switch (nodeName) {
    case ONE:
        myclassDto.setOne(value);
        break;
    case TWO:
        myclassDto.setTwo(value);
        break;
    ...
}
  myclassDto.setValue(nodeName, value)
Related Posts Related Posts :
  • How to create a jar with tests and run it from another version of project?
  • SelectOutput malfunctioning
  • How to display time in HHhMM a format in java ?
  • Is this the right way to change the charset of a String?
  • Apache-poi Restrict the user to select only the values of drop down list
  • Junit4 with Parameterized Test
  • How to print server responses using LoggingFeature in Dropwizard 1.0.2?
  • rabbitmq setting requeue in spring xd
  • Evaluator Expression: How to know if the input is wrong
  • Drawing a JLabel with X,Y coordinate of the center
  • How to check when a bookmark placed inside a cell?
  • Access fields in class
  • What are all the events captured by Websphere JAVA_DUMP_OPTS conditions?
  • About compile servlets from command line: the provided solution didn't work
  • RegEx to extract text between tags in Java
  • JSON ObjectMapper with javac "-parameters" behaves when run via maven, not via InteliJ IDEA
  • datepickerdialog hide calendar and change my date picker view to standard mode
  • How to create config properties for tinylog at runtime with User preferences
  • MessageArrived MQTT paho library
  • Why java doesn't allow to create instance of inner class?
  • Getting NullPointerException while using Lambda expression
  • Reading values from a text file and separating into String and double
  • Gson: skip different fields with different gsons
  • Java this in parent and child not working as expected
  • Java Android - close application (notifications) after swiping in recents
  • I am using Authorize.net payment gateway and doing transaction through Direct Post Method,is it possible to just validat
  • Java Agent wont load into JVM
  • android error: The RealmClass annotation does not support nested classes
  • Hadoop: There are 0 datanodes running and no nodes & cannot connect to namenode
  • Return Response arraylist and 1 attribute
  • Java, why do my lines always white out in BlueJ?
  • starting apache drill process with java API?
  • Configuring a Maven plugin twice in a pom.xml
  • How to work with Lombok?
  • How to parse list of object from Json Object
  • Adding certificate in .cer format into to .bks keystore
  • String array initialization using for loop
  • Dynamic element inside several 'li'
  • Unable to upload a file to Spring REST service via RestTemplate
  • Scala: Why java.lang.IllegalArgumentException: Cannot format given Object as a Date?
  • How does CoreOS allocates memory to a Docker container?
  • Repaint never reaches paintComponent();
  • Instantiating a generic class in a generic class
  • Case insensitive path startsWIth endsWith
  • Deleting directory in finally block?
  • How to include resources from src/main/java
  • Cant assign found item to variable as array
  • add(T) in List cannot be applied to Java.lang.Number
  • Spring Data JPA Many to Many with extra column
  • How to index a boolean @Field in Hibernate Search if the field is not in the db table
  • Modify and access value outside a thread class
  • How to skip @Param in @Query if is null or empty in Spring Data JPA
  • Using AssertJ, how do I perform complex assertions on the contents of a list?
  • Get the corner value of dienantion array in java
  • How can I delete the available state in a local cache?
  • Can't get rid of the ugly border around ImageButton
  • Togglez feature activation based on input parameters from call
  • Passing confirmation to JSP from servlet, getting errors
  • How to check the date and timestamp is before 20mins
  • Java: How does a call to the super class constructor inside of a child class constructor work?
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com