String literals should not be duplicated

Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

Noncompliant Code Example

With the default threshold of 3:

public void run() {
  prepare("action1");                              // Noncompliant - "action1" is duplicated 3 times
  execute("action1");
  release("action1");
}

@SuppressWarning("all")                            // Compliant - annotations are excluded
private void method1() { /* ... */ }
@SuppressWarning("all")
private void method2() { /* ... */ }

public String method3(String a) {
  System.out.println("‘" + a + "‘");               // Compliant - literal "‘" has less than 5 characters and is excluded
  return "";                                       // Compliant - literal "" has less than 5 characters and is excluded
}

Compliant Solution

private static final String ACTION_1 = "action1";  // Compliant

public void run() {
  prepare(ACTION_1);                               // Compliant
  execute(ACTION_1);
  release(ACTION_1);
}

Exceptions

To prevent generating some false-positives, literals having less than 5 characters are excluded.

时间: 2024-08-25 00:47:58

String literals should not be duplicated的相关文章

C++11中的raw string literals

作为一名C++书看得少得可怜的新手,我一直没有勇气去系统地学习一下C++ 11添加的新特性.不过,平日里逛论坛,阅读大犇们的博客,倒是了解了一些.比如,这个帖子: 如何绕过g++ 4.8.1那个不能在宏里面使用R"(...)"的bug? 看到形如:R"" 这样的写法,相信学过Python的童鞋会感到似曾相识.Python支持所谓的“raw string”.Python文档这样介绍raw string: Both string and bytes literals m

react 使用 ref 报错 ,[eslint] Using string literals in ref attributes is deprecated. (react/no-string-refs)

react 项目中给指定元素加事件,使用到 react 的 ref 属性,Eslink 报错 [eslint] Using string literals in ref attributes is deprecated. (react/no-string-refs) 常用方法:(会报错) var Hello = createReactClass({ componentDidMount: function() { var component = this.refs.hello; // ...do

Npoi 解决下拉列表 String literals in formulas can't be bigger than 255 Chars ASCII

代码: 1 public static void dropDownList(string[] datas, string filePath) 2 { 3 HSSFWorkbook workbook = new HSSFWorkbook(); 4 ISheet sheet = workbook.CreateSheet("下拉列表测试"); 5 ISheet hidden = workbook.CreateSheet("hidden"); 6 //数据源sheet页不显

利用宏方便地书写raw string literals

以前一直没用过标准库的regex,今天写一个hlsl的解析工具的时候用了一下,发现用字符串字面值写regular expression的时候非常不方便,特别是每个"\"字符都要被识别为转义,只能写成"\\".比如,一系列空白字符加一个数字要写成这样: std::regex reg("\\s*\\d"); 这样一来,稍微一复杂的regular expression就完全没法看了.理所当然地,可以用raw string literal来解决这个问题.

Sonar 规则

bug类型: 1.".equals()" should not be used to test the values of "Atomic" classes. bug 主要 不要使用equals方法对AtomicXXX进行是否相等的判断 Atomic变量永远只会和自身相等,Atomic变量没有覆写equals()方法.2."=+" should not be used instead of "+=" bug 主要 "

[C++] String Basic

Namespace Declarations A using declaration let us use a name from a namespace without qualify the name with a namespace_name:: prefix. // using namespace::name using std::vector using std::string  A sperate using declaration is required for each name

理解String的intern()方法

API文档中的介绍: intern public String intern() Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a stri

《Just For Java——基础扎实》——第二节:String

一.String简介 包:java.lang.String,java.lang提供利用 Java 编程语言进行程序设计的基础类. 实现:public final class String  implements java.io.Serializable, Comparable<String>, CharSequence{ } 其中:java.io.Serializable  是序列化有关的接口:   java.lang.CharSequence  是cahr值的一个可读序列: java.lan

Java文法(7)—Literals

----------------------------------------------------------------------------------------------------------------------------------- 说明: A literal is the source code representation of a value of a primitive type (§4.2), the String type (§4.3.3), or th