编程不是单行的。这是关于可读性。首先我会写一个测试:
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.runner.RunWith;
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;
@RunWith(ZohhakRunner.class)
public class SampleTest {
Sample sample = new Sample();
@TestWith({
"true, 3, true",
"true, 2, false",
"false, 3, true",
"false, 2, true",
"false, 1, false"
})
public void should_xxx(boolean isLive, int num, boolean expectedResult) {
boolean actualResult = sample.isALive(isLive, num);
assertThat(actualResult).isEqualTo(expectedResult);
}
}
然后我会写:
import static org.apache.commons.lang3.ArrayUtils.contains;
public class Sample {
public boolean isALive(boolean isLive, int num) {
int[] liveValues = {3};
int[] deadValues = {2, 3};
int[] validValues = isLive ? liveValues : deadValues;
return contains(validValues, num);
}
}