3

私の質問は-

2 つの文字列変数site_inclusionsite_exclusion. 値site_inclusionがある場合、どの値site_exclusionが含まれているかは気にしません。つまり、 をsite_inclusionオーバーライドしますsite_exclusion。ただし、 site_inclusionが値nullsite_exclusion持っている場合は、 を調べたいと思いsite_exclusionます。

より正確には:

  1. site_inclusionsite_exclusionが両方の場合、 としてnull設定さuseTheSynthesizertrueます。
  2. site_inclusionでなくnull、 then と一致する場合は としてregexPattern設定useTheSynthesizertrueます。そして、 にどんな値があるかは気にしませんsite_exclusion
  3. であり、等しくなく、かつ一致しない場合site_inclusionnull、 trueに設定されます。site_exclusionnullsite_exclusionregexPatternuseTheSynthesizer

以下のコードを書きましたが、どういうわけか、if/else ループでいくつかのことを繰り返していると思います。私の条件を満たすコードの改善は大歓迎です。

String site_inclusion = metadata.getSiteInclusion();
String site_exclusion = metadata.getSiteExclusion();

// fix for redundant data per site issue
if(site_inclusion != null && site_inclusion.matches(regexPattern)) {
    useTheSynthesizer = true;
} else if(site_exclusion != null && !(site_exclusion.matches(regexPattern))) {
    useTheSynthesizer = true;
} else if(site_inclusion == null && site_exclusion == null ) {
    useTheSynthesizer = true;
}
4

5 に答える 5

7
  1. null最後のテストは本当に必要ありません。
  2. 私は(個人的に)if(test == true) flag = true声明を出すのはスタイルが悪いと思います。簡単に言うことができますflag = test

私の推奨事項は次のとおりです。

if(site_inclusion != null)
{
    useTheSynthesizer = site_inclusion.matches(regexPattern);
}
else if(site_exclusion != null)
{
    useTheSynthesizer = ! site_exclusion.matches(regexPattern);
}
else
{
    useTheSynthesizer = true;
}

ワンライナーでそれを行うこともできます:

useTheSynthesizer = site_inclusion != null ? site_inclusion.matches(regexPattern) : (site_exclusion != null ? ! site_exclusion.matches(regexPattern) : true);

しかし、私はそのようなものを読むのは不快だと思います。

useTheSynthesizer(注、そうでないという仮定をしましたfalse。これはコードや説明では明示的ではありませんが、この仮定は安全だと思います。)

于 2012-06-07T21:57:22.610 に答える
2

私は次のようにします:

    boolean useTheSynthesizer;

    if (siteInclusion == null && siteExclusion == null) {
        useTheSynthesizer = true;
    }
    else if (siteInclusion == null) {
        useTheSynthesizer = ( ! siteExclusion.matches(regexPattern) );
    }
    else {
        useTheSynthesizer = siteInclusion.matches(regexPattern);
    }

また、変数名からアンダースコアを削除しました。これは、Java の命名規則に適合しないためです (そして、それらは恐ろしい IMO です)。

于 2012-06-07T22:08:15.353 に答える
0

こんにちは私はあなたがORのようなものを使ってそれを改善することができると思います、そしてANDだけでなく、またはスイッチケースのような何かを試してみてください。

とにかく、変数をテストする関数を作成することができ、メインモジュールからこの紛らわしいコードを書き出すことができます。

たとえば、このコードを次の関数で記述できます。boolean TestingVariable ( String X, String Y);

例:boolean TesteingVariable(String X、String Y){

  if(X != null && X.matches(regexPattern)) {
      return true;
  } else if(Y != null && !(Y.matches(regexPattern))) {
      return = true;
  } else if(X == null && Y == null ) {
      return = true;
  }
};

このようにして、最終的なメインモジュールコードは次のようになり、メインコード内の混乱したコードを回避できます。

String site_inclusion = metadata.getSiteInclusion();
String site_exclusion = metadata.getSiteExclusion();

// fix for redundant data per site issue
useTheSynthesizer = TesteingVariable (site_inclusion ,site_exclusion);

関数に変数を入力する必要があると思いますregexPattern

私の英語で申し訳ありませんが、あなたがすべてを理解することができ、それがあなたに役立つことを願っています。

于 2012-06-07T22:23:55.000 に答える
0

このようにすることができます。基本的にはすべての条件を小さなメソッドとして抽出し、OR条件にしました。

    String site_inclusion = metadata.getSiteInclusion();
    String site_exclusion = metadata.getSiteExclusion();
        if(isInclusionAndExclusionNull(site_inclusion, site_exclusion) || isSiteExclusionMatches(site_exclusion, regexPattern) || isSiteInclusionMatches(site_inclusion, regexPattern)) {
            useTheSynthesizer = true;
        }

private static boolean isInclusionAndExclusionNull(String site_inclusion,
            String site_exclusion) {
        return site_inclusion == null && site_exclusion == null;
    }    
    private boolean isSiteExclusionMatches(String site_exclusion,
                String regexPattern) {
            return site_exclusion != null && !(site_exclusion.matches(regexPattern));
        }

        private  boolean isSiteInclusionMatches(String site_inclusion,
                String regexPattern) {
            return site_inclusion != null && site_inclusion.matches(regexPattern);
        }
于 2012-06-07T22:05:33.153 に答える
0

以下のように、包含と除外を柔軟に処理するための 2 つの方法があります。

callingMethod() {
  boolean useTheSynthesizer = processSiteInclusions(site_inclusion, regexPattern);

  if (useTheSynthesizer == false) {
     useTheSynthesizer = processSiteExclusions(site_inclusion, regexPattern);
  }

  if (useTheSynthesizer == false) {
    useTheSynthesizer = true;
  }
}

private boolean processSiteInclusions(site_inclusion, regexPattern) {
   boolean useSynthesizer = false;

   if (site_inclusion != null && !site_inclusion.matches(regexPattern))
      useSynthesizer = true;

   return useSynthesizer;
}

private boolean processSiteExclusions(site_exclusion, regexPattern) {
   boolean useSynthesizer = false;

   if (site_exclusion != null && !site_inclusion.matches(regexPattern))
      useSynthesizer = true;

   return useSynthesizer;
}
于 2012-06-07T22:09:42.013 に答える