私が求めているのは、これを行うことに違いがあるかどうかです:
public Something importantBlMethod(SomethingElse arg) {
if (convenienceCheckMethod(arg)) {
// do important BL stuff
}
}
private boolean convenienceCheckMethod(SomethingElse arg) {
// validate something
}
この:
public Something importantBlMethod(SomethingElse arg) {
if (convenienceCheckMethod(arg)) {
// do important BL stuff
}
}
private static boolean convenienceCheckMethod(SomethingElse arg) {
// validate something
}
より自然に思えるので、実際にはオプション 1 を使用します。
では、最初の方法と 2 番目の方法の間にスタイル/規則/パフォーマンスの違いはありますか?
ありがとう、
私がテストしたコメントで示唆されているように、私のベンチマークでは動的な方法の方が高速です。
これはテストコードです:
public class Tests {
private final static int ITERATIONS = 100000;
public static void main(String[] args) {
final long start = new Date().getTime();
final Service service = new Service();
for (int i = 0; i < ITERATIONS; i++) {
service.doImportantBlStuff(new SomeDto());
}
final long end = new Date().getTime();
System.out.println("diff: " + (end - start) + " millis");
}
}
これはサービス コードです。
public class Service {
public void doImportantBlStuff(SomeDto dto) {
if (checkStuffStatic(dto)) {
}
// if (checkStuff(dto)) {
// }
}
private boolean checkStuff(SomeDto dto) {
System.out.println("dynamic");
return true;
}
private static boolean checkStuffStatic(SomeDto dto) {
System.out.println("static");
return true;
}
}
100000 回の反復では、動的メソッドは 577 ミリ秒、静的メソッドは 615 ミリ秒で合格します。
ただし、コンパイラが何をいつ最適化するかがわからないため、これは私にとって決定的ではありません。
これは私が見つけようとしているものです。