rspec/rules/S2005/java/rule.adoc

38 lines
967 B
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
There is no reason to concatenate literal strings. Doing so is an exercise is reducing code readability. Instead, the strings should be combined. Similarly, literal strings should not be ``++append++``ed to a ``++StringBuffer++`` or ``++StringBuilder++`` sequentially, but combined into one call.
2020-06-30 12:48:07 +02:00
=== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:48:07 +02:00
----
String message = "Hello " + "world" + "!"; // Noncompliant
StringBuilder sb = new StringBuilder();
sb.append("I'm pleased").append(" to meet you."); //Noncompliant
----
=== Compliant solution
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:48:07 +02:00
----
String message = "Hello world!";
StringBuilder sb = new StringBuilder();
sb.append("I'm pleased to meet you.");
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]