org.sonar.l10n.javascript.rules.javascript.S1516.html Maven / Gradle / Ivy
Why is this an issue?
Creating multiline strings by using a backslash (\
) before a newline is known as "line continuation" or "line breaking." While it may
seem like a convenient way to format multiline strings, it is generally considered bad practice.
- Line continuation can make the code harder to read and understand, especially when dealing with long strings. It introduces an extra character
at the end of each line, which can clutter the code and reduce its readability.
- If the string content changes, it might require reformatting the entire multiline string, involving adjusting the line breaks and ensuring the
backslashes are correctly placed. This can be error-prone and cumbersome, leading to maintenance issues.
- Line continuation can sometimes behave unexpectedly, particularly when there are trailing spaces or tabs after the backslash. This can lead to
subtle bugs that are difficult to spot and debug.
let myString = 'A rather long string of English text, an error message \
actually that just keeps going and going -- an error \
message to make the Energizer bunny blush (right through \
those Schwarzenegger shades)! Where was I? Oh yes, \
you\'ve got an error and all the extraneous whitespace is \
just gravy. Have a nice day.'; // Noncompliant
Instead, you should use string concatenation for multiline strings, which involves combining multiple strings to create a single string that spans
multiple lines.
let myString = 'A rather long string of English text, an error message ' +
'actually that just keeps going and going -- an error ' +
'message to make the Energizer bunny blush (right through ' +
'those Schwarzenegger shades)! Where was I? Oh yes, ' +
'you\'ve got an error and all the extraneous whitespace is ' +
'just gravy. Have a nice day.';
Resources
Documentation
- MDN web docs - String
operators