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?
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.
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.
Purple Mash uses the acronym SMART: ● Safe. ● Never Meet. ● Accepting (as in avoid accepting emails from people you don't know) ● Reliable (information)
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.
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";
}
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";
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With