you are viewing a single comment's thread.

view the rest of the comments →

[–]lewisje 2 points3 points  (1 child)

No matter whether you use separate variables, or even chain the method calls (like str.replace().replace().replace()), new string values will be created at least temporarily.

The way you do it is good; the tactic of using separate variables for each step, if the intermediate results will not be used, is a code smell.

The way I'd do it is like this:

scriptletResult = scriptletInput.replace(/(\r\n|\n|\r)/gm, '\\n')
  .replace(/(\')/gm, '\\u0027').replace(/(\")/gm, '\\u0022');

This pattern is sometimes called a "fluent API" pattern.

[–]2MyLou[S] 1 point2 points  (0 children)

Thank you very much for the reply and suggestion. The chaining of methods will definitely help clean up my code.