Skip to content

Commit 1cf63f7

Browse files
github-actions[bot]denis-trolleryassin-kammoun-sonarsource
authored
Create rule S8132: String conversions should use explicit methods instead of empty string concatenation (#5665)
* Create rule S8132 * Update rules/S8132/apex/rule.adoc in PR #5665 * Update rules/S8132/apex/metadata.json in PR #5665 * Update metadata and description --------- Co-authored-by: denis-troller <[email protected]> Co-authored-by: denis-troller <[email protected]> Co-authored-by: yassin-kammoun-sonarsouce <[email protected]>
1 parent d92caf3 commit 1cf63f7

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

rules/S8132/apex/metadata.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"title": "String conversions should use explicit methods instead of empty string concatenation",
3+
"type": "CODE_SMELL",
4+
"status": "ready",
5+
"remediation": {
6+
"func": "Constant/Issue",
7+
"constantCost": "5 min"
8+
},
9+
"tags": [
10+
"performance",
11+
"readability"
12+
],
13+
"defaultSeverity": "Minor",
14+
"ruleSpecification": "RSPEC-8132",
15+
"sqKey": "S8132",
16+
"scope": "Main",
17+
"defaultQualityProfiles": [
18+
"Sonar way"
19+
],
20+
"quickfix": "unknown",
21+
"code": {
22+
"impacts": {
23+
"MAINTAINABILITY": "LOW"
24+
},
25+
"attribute": "CLEAR"
26+
}
27+
}

rules/S8132/apex/rule.adoc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
This rule raises an issue when values are converted to strings by concatenating them with empty strings (``++''++``).
2+
3+
== Why is this an issue?
4+
5+
Converting values to strings by concatenating them with empty strings is an inefficient and unclear practice.
6+
7+
When you write ``++'' + someValue++``, the Apex runtime must:
8+
9+
* Create a temporary string object for the empty string
10+
* Convert ``++someValue++`` to its string representation
11+
* Concatenate these two strings
12+
* Return the result
13+
14+
This process involves unnecessary object creation and string manipulation overhead.
15+
16+
Using explicit ``++toString()++`` methods is more efficient because:
17+
18+
* It directly converts the value to a string without intermediate steps
19+
* The intent is clearer to other developers reading your code
20+
* It follows Apex best practices for type conversion
21+
22+
The performance difference becomes more noticeable when this pattern is used frequently, such as in loops or bulk operations common in Salesforce development.
23+
24+
=== What is the potential impact?
25+
26+
This issue impacts code performance and maintainability:
27+
28+
* *Performance degradation*: Unnecessary object creation and string operations can slow down your code, especially in bulk operations
29+
* *Reduced readability*: The intent to convert a value to string is less clear when using concatenation
30+
* *Maintenance overhead*: Code reviewers and future developers may not immediately understand the purpose of empty string concatenation
31+
32+
== How to fix it
33+
34+
Replace empty string concatenation with the appropriate ``++toString()++`` method for the data type or use ``++String.valueOf()++``.
35+
36+
=== Code examples
37+
38+
==== Noncompliant code example
39+
40+
[source,apex,diff-id=1,diff-type=noncompliant]
41+
----
42+
String numberAsString = '' + 123; // Noncompliant
43+
String decimalAsString = '' + 45.67; // Noncompliant
44+
String booleanAsString = '' + true; // Noncompliant
45+
----
46+
47+
==== Compliant solution
48+
49+
[source,apex,diff-id=1,diff-type=compliant]
50+
----
51+
String numberAsString = String.valueOf(123);
52+
String decimalAsString = String.valueOf(45.67);
53+
String booleanAsString = String.valueOf(true);
54+
----
55+
56+
== Resources
57+
58+
=== Documentation
59+
60+
* Apex String Class - https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm[Official Salesforce documentation for String class methods including valueOf()]
61+
62+
* Apex Data Types - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_primitives.htm[Documentation on Apex primitive data types and their conversion methods]
63+
64+
=== Standards
65+
66+
* Salesforce Apex Best Practices - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_best_practices.htm[Official Salesforce guidelines for writing efficient Apex code]

rules/S8132/metadata.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

0 commit comments

Comments
 (0)