Home > Software engineering >  How to simplify too many if condition and capture the error out field in java
How to simplify too many if condition and capture the error out field in java

Time:11-05

I have these fields productID, productName, productDesc, productPrice - all these are optional fields, so I'm checking if this key exists in the map if exist I'm assigning it to the variable and performing null check validation. Please find the code below - is there any better way of writing the below logic, how to avoid too many if in this code?

And I would like to capture the field which is failed to satisfy the null check.

Any help would be really appreciated. Thanks!

private void validate(Map<String, String> map) {
    String productID = null;
    String productName = null;
    String productDesc = null;
    String productPrice = null;

    if ((map.containsKey("PRODUCT_ID")))
        productID = map.get("PRODUCT_ID");

    if ((map.containsKey("PRODUCT_NAME")))
        productName = map.get("PRODUCT_NAME");

    if ((map.containsKey("PRODUCT_DESC")))
        productDesc = map.get("PRODUCT_DESC");

    if ((map.containsKey("PRODUCT_PRICE")))
        productPrice = map.get("PRODUCT_PRICE");

    if ((productID != null || productName != null) || (productDesc != null && productPrice != null)) {
        System.out.println("validation success");
    } else {
        System.out.println("validation failed"); // how to get the field name here which is failed to satisfy the above condition?
    }
}

CodePudding user response:

This is one different approach that might be easy to extend:

private void validate2(Map<String, String> map) {
    String[] list = new String[]{"PRODUCT_ID", "PRODUCT_ID", "PRODUCT_DESC", "PRODUCT_PRICE"};
    String [] result = new String[list.length];
    for (int i = 0; i < list.length; i  ) {
        String v = map.getOrDefault(list[i], null);
        if (v == null) {
            System.out.println("validation failed for "   list[i]);
        } else {
            result[i] = v;
        }
    }
    
    String productID = result[0];
    String productName = result[1];
    String productDesc = result[2];
    String productPrice = result[3];
}

CodePudding user response:

In your case the containsKey check is unnecessary. Because you initialize the variables with null and Map.get(Key) returns null if no value was found for the given key.

So just writing this does the same, but it's shorter.

String productID = map.get("PRODUCT_ID");
String productName = map.get("PRODUCT_NAME");
String productDesc = map.get("PRODUCT_DESC");
String productPrice = map.get("PRODUCT_PRICE");

CodePudding user response:

Maybe you are looking for something like this:

public void validateNonNullValues(Map<String, String> map, String... keys) {
    for (String key : keys) {
        if (map.get(key) == null) {
            System.out.println("validation failed for "   key);
        }
    }
}

validateNonNullValues(map, "PRODUCT_ID", "PRODUCT_ID", "PRODUCT_DESC", "PRODUCT_PRICE");
  • Related