Sonar建议汇总

Sonar建议汇总

标签(空格分隔): 工具 编码规范


The Cyclomatic Complexity of this method “deleteMission” is 14 which is greater than 10 authorized

嵌套复杂度为14,高于限定值10

Refactor this code to not nest more than 3 if/for/while/switch/try statements.

重构此代码,不得超过3行

Move this variable to comply with Java Code Conventions

构造函数应遵守Java代码约定,请移动变量的代码。参考:Java Code Conventions
下表描述了类和接口声明的各个部分以及它们出现的先后次序。

次序类/接口声明的各部分注解
1类/接口文档注释(/*……/)该注释中所需包含的信息,参见”文档注释”
2类或接口的声明
3类/接口实现的注释(/……/)如果有必要的话该注释应包含任何有关整个类或接口的信息,而这些信息又不适合作为类/接口文档注释。
4类的(静态)变量首先是类的公共变量,随后是保护变量,再后是包一级别的变量(没有访问修饰符,access modifier),最后是私有变量。
5实例变量首先是公共级别的,随后是保护级别的,再后是包一级别的(没有访问修饰符),最后是私有级别的。
6构造器
7方法这些方法应该按功能,而非作用域或访问权限,分组。例如,一个私有的类方法可以置于两个公有的实例方法之间。其目的是为了更便于阅读和理解代码。

Reorder the modifiers to comply with the Java Language Specification.

调整修饰符次序,反例:public final static PropertyMap<Event, EventVO> voMap = new PropertyMap<Event, EventVO>()

Rename this constant name to match the regular expression ‘^[A-Z][A-Z0-9](_[A-Z0-9]+)$’

常量要求大写

Add a private constructor to hide the implicit public one.

工具类不应该有默认或者公共的构造函数,也就是说这个类里可能方法都是static,那就不需要构造它的实例,因此应该给加一个private的构造函数,就不会报这个错了。
a class which only has private constructors should be final
例如上一个,加了private构造函数,又会出这个,把class设置成final即可。例:

  public class Shape {
    private Shape() { 
       /* set something here */
    }

    public static Shape makeShape(/* arglist */) {
       System.out.println("here is the shape you ordered");
       return (new Shape());
    }
 }

Invoke method(s) only conditionally

以下代码会报错:

logger.log(Level.DEBUG, "Something went wrong: " + message);  // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages

LOG.error("Unable to open file " + csvPath, e);  // Noncompliant

Preconditions.checkState(a > 0, "Arg must be positive, but got " + a);  // Noncompliant. String concatenation performed even when a > 0

Preconditions.checkState(condition, formatMessage());  // Noncompliant. formatMessage() invoked regardless of condition

Preconditions.checkState(condition, "message: %s", formatMessage());  // Noncompliant

推荐如下:

logger.log(Level.SEVERE, "Something went wrong: %s ", message);  // String formatting only applied if needed

logger.log(Level.SEVERE, () -> "Something went wrong: " + message); // since Java 8, we can use Supplier , which will be evaluated lazily

LOG.error("Unable to open file {}", csvPath, e);

if (LOG.isDebugEnabled() {
  LOG.debug("Unable to open file " + csvPath, e);  // this is compliant, because it will not evaluate if log level is above debug.
}

Preconditions.checkState(arg > 0, "Arg must be positive, but got %d", a);  // String formatting only applied if needed

官方解释:

“Preconditions” and logging arguments should not require evaluation

Passing message arguments that require further evaluation into a Guava com.google.common.base.Preconditions check can result in a performance penalty. That’s because whether or not they’re needed, each argument must be resolved before the method is actually called.

Only the sign of the result should be examined

While most compareTo methods return -1, 0, or 1, some do not, and testing the result of a compareTo against a specific value other than 0 could result in false negatives.

大部分compareTo()比较方法,返回结果都是-1, 0, or 1,但并非全部,建议如下:

Noncompliant Code Example

if (myClass.compareTo(arg) == -1) {  // Noncompliant
  // ...
}
Compliant Solution

if (myClass.compareTo(arg) < 0) {
  // ...
}
<think>好的,我需要帮助用户寻找并下载声呐相关的数据集,特别是Sonar数据集。首先,用户提到了“sonar dataset download”,并引用了几个相关的来源。我需要整理这些信息,确保回答准确且符合用户的需求。 首先,查看用户提供的引用内容。引用[1]提到可以从UCI机器学习仓库下载数据集,保存为sonar.all-data.csv,并给出了URL。引用[3]提到了使用Python读取txt文件的方法,可能涉及不同的数据格式或来源。引用[2]是知乎上的公开数据集汇总,可能作为补充资源。 接下来,我需要确认Sonar数据集的主要来源。通常,UCI机器学习仓库是常用的数据集来源,所以应该优先考虑引用[1]中的链接。用户提供的示例代码中使用了read_csv函数加载本地文件,这也说明数据集可能是CSV格式的。 然后,我需要确保提供的下载链接正确有效。用户给出的UCI链接是https://goo.gl/NXoJfR,这是一个短链接,需要验证是否仍然有效。不过,由于用户直接引用了该链接,可能假设它有效。同时,引用[3]中的Windows和Linux链接可能涉及SciPy的安装,但用户的问题是关于数据集下载,所以这部分可能作为补充信息,比如数据处理时需要的库。 用户可能还需要知道如何加载和处理数据集,因此需要提供Python代码示例,如使用pandas的read_csv或numpy的genfromtxt。引用[1]和[3]中的代码片段可以作为参考,但要注意数据文件的路径和格式是否正确。 另外,引用[2]提到的知乎资源可以作为额外的数据集来源,但需要确认是否有Sonar数据集的具体信息。如果没有,可以建议用户在该平台搜索相关关键词。 需要检查是否有其他可能的来源或注意事项,比如数据集的分隔符、列数等。例如,在引用[3]中,使用逗号分隔符和60列,这可能与Sonar数据集的结构相关,需确保代码示例准确无误。 最后,总结下载步骤,分点说明,确保用户能一步步操作。同时,生成相关问题,帮助用户进一步探索数据的使用和应用,比如分类任务或数据预处理方法。 需要避免的信息:过时的链接、无法访问的资源。如果UCI的链接已更新,可能需要调整,但用户提供的引用应优先考虑。此外,确保所有代码语法正确,如正确使用库函数和参数。 总结回答结构:先提供UCI的下载方式,再补充其他可能的来源,如知乎和直接文件读取方法,最后给出处理数据的代码示例和相关问题。</think>### Sonar数据集下载与使用指南 Sonar数据集常用于二元分类任务,例如区分岩石与金属圆柱体的声呐信号。以下是获取和使用该数据集的详细方法: #### 1. 官方数据源下载(UCI仓库) 通过UCI机器学习仓库直接下载: ```python # 下载地址 url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data' ``` 数据集包含208个样本,60个特征值,1个标签列(最后一列为"R"或"M")[^1]。 #### 2. 本地加载方法 (1)**Pandas读取CSV**: ```python from pandas import read_csv dataset = read_csv('sonar.all-data.csv', header=None) ``` (2)**NumPy读取TXT**: ```python import numpy as np data = np.genfromtxt('./sonar.txt', delimiter=',', usecols=np.arange(0,60)) # 读取前60列特征[^3] ``` #### 3. 其他资源渠道 - 知乎公开数据集汇总包含多个领域数据资源[^2] - 科研镜像站点(如SourceForge)提供预处理后的数据集[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值