Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid a lot of if else conditions [duplicate]

I have read a lot of topics about code refactoring and avoiding of if else statements. Actually, I have a class where I am using a lot of if - else conditions.

private static String getXSIType(String fieldType) {
    if ("_freeFormText".equals(fieldType) || "_eMailAddress".equals(fieldType) || "_help".equals(fieldType)
            || "_hyperlink".equals(fieldType) || "_inlineText".equals(fieldType) || "_longText".equals(fieldType)
            || "_password".equals(fieldType) || "_phoneNumber".equals(fieldType) || "_richText".equals(fieldType)
            || "_textArea".equals(fieldType)) {

        return "platformCore:StringCustomFieldRef";

    } else if ("_integerNumber".equals(fieldType)) {
        return "platformCore:LongCustomFieldRef";

    } else if ("_multipleSelect".equals(fieldType)) {
        return "platformCore:MultiSelectCustomFieldRef";

    } else if ("_document".equals(fieldType) || "_listRecord".equals(fieldType) || "_image".equals(fieldType)) {
        return "platformCore:SelectCustomFieldRef";

    } else if ("_currency".equals(fieldType) || "_decimalNumber".equals(fieldType)
            || "_percent".equals(fieldType)) {
        return "platformCore:DoubleCustomFieldRef";

    } else if ("_checkBox".equals(fieldType)) {
        return "platformCore:BooleanCustomFieldRef";

    } else if ("_date".equals(fieldType) || "_datetime".equals(fieldType) || "_timeOfDay".equals(fieldType)) {
        return "platformCore:DateCustomFieldRef";

    }
    return "platformCore:StringCustomFieldRef";
}

Now, I would like to use something else, instead of those if else conditions, but I don't know what.

Can you please give me an example or a good tutorial page?

Thank you

like image 277
Amrendra Kumar Avatar asked Dec 24 '22 03:12

Amrendra Kumar


1 Answers

Here is what you need:

private static String getXSIType(String fieldType) {
    String result=new String();
    switch (fieldType)
    {
        case "_checkBox":
            result="platformCore:BooleanCustomFieldRef";
            break;
        case "_integerNumber":
            result="platformCore:LongCustomFieldRef";
            break;
        case "_multipleSelect":  
            result="platformCore:MultiSelectCustomFieldRef";
            break;

        case "_currency":
        case "_decimalNumber":
        case "_percent":
            result="platformCore:DoubleCustomFieldRef";
            break;

        case "_document":
        case "_listRecord":
        case "_image": 
            result="platformCore:SelectCustomFieldRef";
            break;
        case "_date":
        case "_datetime":
        case "_timeOfDay":
            result="platformCore:DateCustomFieldRef";
            break;

        case "_freeFormText":
        case "_eMailAddress":
        case "_help":
        case "_hyperlink":
        case "_inlineText":
        case "_longText":
        case "_password":
        case "_phoneNumber":
        case "_richText":
        case "_textArea":
            result="platformCore:StringCustomFieldRef";
            break;      
        default:
            result="platformCore:StringCustomFieldRef";
            break;
    }
    return result;
}
like image 137
The KNVB Avatar answered Jan 10 '23 20:01

The KNVB