Farid Arshad
Farid Arshad

Reputation: 334

Shorten with multiple If else condition

I am trying to shorten my if else statement, i have a few approaches that i tried however, i still its too long. The orignal script that i wrote is show below

public static SqlType getSqlType(String metadata) {
        System.out.println("metadata info " + metadata);
        if (metadata.startsWith("string") || metadata.startsWith("char") || metadata.startsWith("varchar")) {
            log.logger.info(metadata + " to " + "varchar");
            return SqlType.text();
        }
        if (metadata.startsWith("bigint")) {
            log.logger.info(metadata + " : " + "bigint");
            return SqlType.bigInt();
        }
        if (metadata.startsWith("int")) {
            log.logger.info(metadata + " : " + "int");
            return SqlType.integer();
        }
        if (metadata.startsWith("smallint") || metadata.startsWith("tinyint")) {
            log.logger.info(metadata + " : " + "tinyint");
            return SqlType.smallInt();
        }
        if (metadata.startsWith("boolean")) {
            log.logger.info(metadata + " : " + "boolean");
            return SqlType.bool();
        }
        if (metadata.startsWith("float") || metadata.startsWith("double") || metadata.startsWith("decimal")) {
            log.logger.info(metadata + " : " + "decimal");
            return SqlType.doublePrecision();
        }
        if (metadata.startsWith("timestamp")) {
            log.logger.info(metadata + " : " + "varchar");
            return SqlType.timestamp();
        } else {
            log.logger.log(Level.WARNING,"Datatype is not declared in java class and is convert as text : " + metadata );
            return SqlType.text();
        }
    }

The **first** approach i tried was using is as follow
if(metadata.startsWith("string") || metadata.startsWith("char") || metadata.startsWith("varchar")){return SqlType.text();}
if(metadata.startsWith("int")){return SqlType.bigInt();}

using this approach will not enable me to include logger as it will too long winded too.


The second approach i tried is using ternary as follow
 return metadata.startsWith("string") || metadata.startsWith("char") || metadata.startsWith("varchar") ? return SqlType.text();
         : metadata.startsWith("string") || metadata.startsWith("char") || metadata.startsWith("varchar") ? SqlType.text();

However in the second approach , how add in the logger file as shown in example script above. What is the best practices or approach should be taken into coding multiple if else conditions

Upvotes: 1

Views: 99

Answers (1)

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

Simplify if conditions is depend on the metadata and how you want to process in. But you can improve the code readability and make it simple using StringUtils

Your code:

   if (metadata.startsWith("float") || metadata.startsWith("double") || metadata.startsWith("decimal")) {
        log.logger.info(metadata + " : " + "decimal");
        return SqlType.doublePrecision();
    }

StringUtils:

if (StringUtils.startsWithAny(metadata, "float", "double", "decimal")) {
     return SqlType.doublePrecision();
}

Upvotes: 1

Related Questions