Dataflow anomaly analysis warnings from PMD

前端 未结 2 704
攒了一身酷
攒了一身酷 2020-12-17 09:21

I am using Eclipse with the PMD Plug-in (4.0.0.v20130510-1000) and get a lot of those violations:

Found \'DD\'-anomaly for variable \'freq\' (line

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-17 10:10

    You could get around this problem (and separate concerns a bit more clearly) by extracting the parsing into a separate method:

    double freq = parseDouble(getFrequencyTextField().getText());
    
    // later in the class (or in a utility class):
    
    public static double parseDouble(final String input) {
      try {
        return Double.parseDouble(input);
      } catch (final NumberFormatException e) {
        Log.e(e.getMessage());
        return 0;
      }
    }
    

    And if you have different default values, you could also add a two-argument version: public static double parseDouble(final String input, final double defaultValue).

提交回复
热议问题