This question is to share a very simple trick I learned on StackOverflow that has helped me clean thousands of cases in my code.
NOTE: it is not a translation, it is simply a transmission of knowledge that I think is necessary and interesting and originally created by me for SO in Spanish.
We have all assembled a text string by hand by inserting the separators, for example:
Characteristics of an element (car) separated by commas
,
ABS, ESP, EE, CC
Printable list with line breaks
\n
producto1 2,23\n producto2 3,23\n producto33 5,31\n
And we have encountered one of the following problems:
insert a comparison to each iteration:
JAVA
// bucle que inserta valor v en variable x if ("".equals(x)) x = v; else x = "," + v;
JAVA SCRIPT
// bucle que inserta valor v en variable x if (x == "") x = v; else x = "," + v;
if we do not insert that comparison to optimize, we still have to do it later to avoid
last empty element:
1,1,1,1,1, // ↑ aquí!
first empty element
,1,1,1,1,1 //↑ aquí!
QUESTION
Is there a pattern to avoid this usual and annoying case and that meets the following characteristics?
- Transversal (usable in any language).
- Optimal (avoid expensive functions/methods, comparisons or extra iterations)
- Readable
There really is a very simple and effective trick that makes the chains created in this way assemble perfectly, optimizing the code to the maximum:
SEPARADOR
Create an empty variable at the beginning and assign it at the end of each iteration:JAVA WITHOUT USING THE TECHNIQUE
JAVA
JAVASCRIPT/JQUERY WITHOUT USING THE TECHNIQUE
JAVASCRIPT/JQUERY
There it is for whoever wants to use it.
This is another way of looking at it.
In cases where you work with items, and add them, the ideal is to use an array .
And from there:
array.push()
add an element to the arrayarray.join()
concatenate all elements with a separatorCode
There is no optimal method that works for all languages. Even within the same language, the optimal method may vary depending on the data structure where the data to be concatenated is.
For 99% of programs, making the code readable is more important than optimizing it. Make it readable first, and optimize later only if you find it necessary.
If the language has a function that does this, use it. It is the most readable and usually the most optimal. For example, in javascript use
Array.join
if the data is in an array.If not, just make it readable and don't worry about optimizing it for now. A
if
in the loop is readable, an initially empty variable that is updated in the loop as Jordi Castilla proposes in his answer as well. Preferring one method or another depends on each programmer.If after implementing it you find that you need to improve performance, a possible option, if the way of traversing the data allows it, is to take the first iteration out of the loop. For example:
The problem is that this is clearly less readable, and not all ways of looping through the data allow it to be done.
The other answers compare against an empty string. I don't understand if that is part of the definition of the problem. If it isn't, the following is my solution.
I usually use a variable
boolean
. Something like:Too many laps. The least expensive is to trim the comma at the end.
That's it, the larger the amount of data, any 1-to-1 validation becomes too expensive, and it is precisely in this case that it needs to be optimal.
Delete the trailing comma. A single operation once per row. Finish
Now that's the least of the problems some of the java and js demos have shown you inefficiency is the rule... You can do much more optimal things with C# or C, C++... But hey you asked for something agnostic. There you have it.
In JAVA 8 we can do it like this:
The result will be: Hello, David
Note that the result does not include any ', ' after David.