|
| 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] |
0 commit comments