Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Condition handling the smart way

Tags:

java

if (lineStyle == 5 || lineStyle == 21 || lineStyle == 82 || lineStyle == 83 || lineStyle == 3) {
    lineStyleString = "DOUBLE";
} else if (lineStyle == 6 || lineStyle == 35 || lineStyle == 39 || lineStyle == 30) {
    lineStyleString = "DOTTED" ;
} else if (lineStyle == 26 || lineStyle == 27  || lineStyle == 28  || lineStyle == 29 || lineStyle == 1) {
    lineStyleString = "SOLID";
} else if(lineStyle == -1) {
    lineStyleString = "NONE";
}

How do we handle this code the smart way in Java? Switch case, enum or key pair value mode?

like image 235
user1023675 Avatar asked Jul 18 '17 10:07

user1023675


People also ask

What are the 5 smart objectives in the SMART acronym?

Completion of objectives result in specific, measurable outcomes that directly contribute to the achievement of the project goals. Setting specific, measurable, achievable, relevant, and time-bound (SMART) objectives is a good way to plan the steps to meet the long-term goals in your grant.

What are smart objectives in care?

A SMART objective is one that is specific, measurable, achievable, relevant, and time-bound. SMART objectives provide the details for how a group or organization will achieve a goal.

What does SMART mean in safety?

Purple Mash uses the acronym SMART: ● Safe. ● Never Meet. ● Accepting (as in avoid accepting emails from people you don't know) ● Reliable (information)

What does SMART mean in mental health?

When setting and achieving goals, individuals are exercising autonomy, which is a vital feature of mental health. There are certain features which make a quality goal. These features are referred to as SMART goals, which stands for specific, measurable, attainable, relevant, and time-bound goals.


3 Answers

Your conditions looks more random.

Switch looks good here

switch(lineStyle) {
    case 5:
    case 21:
    case 82:
    case 83:
    case 3: 
     lineStyleString = "DOUBLE";   
     break;
    .. // add more cases
}

Or I prefer to create utility method

public static boolean contains(int expecxted, int... vals) {
        for (int i = 0; i < vals.length; i++) {
            if (expecxted == vals[i]) {
                return true;
            }
        }
        return false;
    }

And you can use it like

if (contains(lineStyle, 5,21,82,83,3)) {
    lineStyleString = "DOUBLE";
} else if(contains(lineStyle,6,35,39,30)){
   lineStyleString = "DOTTED";
}
like image 134
Suresh Atta Avatar answered Oct 28 '22 06:10

Suresh Atta


A switch case well-indented would take 30 lines (Netbeans proposed to transform it by itself so I could count)

So I would consider this way is better (9 lines) :

if (Arrays.asList(5, 21, 82, 83, 3).contains(lineStyle)) {
     lineStyleString = "DOUBLE";
} else if (Arrays.asList(6, 35, 39, 30).contains(lineStyle)) {
     lineStyleString = "DOTTED";
} else if (Arrays.asList(26, 27, 28, 29, 1).contains(lineStyle)) {
     lineStyleString = "SOLID";
}else if (lineStyle == -1) {
     lineStyleString = "NONE";
}
like image 26
azro Avatar answered Oct 28 '22 06:10

azro


You could extract the conditions into methods to make them more readable.

private boolean isDoubleStyle(int lineStyle) {
    return lineStyle == 5 || lineStyle == 21 || lineStyle == 82 || lineStyle == 83 || lineStyle == 3;
}

private boolean isDottedStyle(int lineStyle) {
    return lineStyle == 6 || lineStyle == 35 || lineStyle == 39 || lineStyle == 30;
}

private boolean isSolidStyle(int lineStyle) {
    return lineStyle == 26 || lineStyle == 27  || lineStyle == 28  || lineStyle == 29 || lineStyle == 1;
}

and then call the methods

if (isDoubleStyle(lineStyle)) {
    lineStyleString = "DOUBLE";
} else if (isDottedStyle(lineStyle)) {
    lineStyleString = "DOTTED" ;
} else if (isSolidStyle(lineStyle)) {
    lineStyleString = "SOLID";
} else {
    lineStyleString = "NONE";
}

I removed the final check for linestyle == -1 to ensure that lineStyleString always has a value, no matter what.

like image 2
QBrute Avatar answered Oct 28 '22 07:10

QBrute