Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #24043 > unrolled thread
| Started by | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| First post | 2014-05-05 12:07 +1000 |
| Last post | 2014-05-09 12:40 +0100 |
| Articles | 9 — 6 participants |
Back to article view | Back to comp.lang.javascript
Strings with trailing plus Andrew Poulos <ap_prog@hotmail.com> - 2014-05-05 12:07 +1000
Re: Strings with trailing plus "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2014-05-05 08:08 +0300
Re: Strings with trailing plus Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-05 13:02 +0200
Re: Strings with trailing plus Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-05 19:45 +0200
Re: Strings with trailing plus Andrew Poulos <ap_prog@hotmail.com> - 2014-05-09 11:39 +1000
Re: Strings with trailing plus Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-09 10:13 +0200
Re: Strings with trailing plus Norman Peelman <npeelman@cfl.rr.com> - 2014-05-09 22:43 -0400
Re: Strings with trailing plus Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-09 10:11 +0200
Re: Strings with trailing plus Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-09 12:40 +0100
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2014-05-05 12:07 +1000 |
| Subject | Strings with trailing plus |
| Message-ID | <T72dnUGM8ZJAb_vOnZ2dnUVZ_g2dnZ2d@westnet.com.au> |
I typically concatenate long stings with trailing spaces and plus signs eg.
var str = "blah blah... blah " +
"blah blah... blah " +
"blah blah... blah " +
"blah blah... blah " +
"blah blah... blah";
but I've seen code with leading plus signs and spaces eg.
var str = "blah blah blah"
+ " blah blah... blah"
+ " blah blah... blah"
+ " blah blah... blah"
+ " blah blah... blah";
Is their a reason to prefer one over the other?
Andrew Poulos
[toc] | [next] | [standalone]
| From | "Jukka K. Korpela" <jkorpela@cs.tut.fi> |
|---|---|
| Date | 2014-05-05 08:08 +0300 |
| Message-ID | <lk76d7$7pf$1@dont-email.me> |
| In reply to | #24043 |
2014-05-05 5:07, Andrew Poulos wrote: > I typically concatenate long stings with trailing spaces and plus signs eg. > > var str = "blah blah... blah " + > "blah blah... blah " + > "blah blah... blah " + > "blah blah... blah " + > "blah blah... blah"; > > but I've seen code with leading plus signs and spaces eg. > > var str = "blah blah blah" > + " blah blah... blah" > + " blah blah... blah" > + " blah blah... blah" > + " blah blah... blah"; > > Is their a reason to prefer one over the other? This is just a matter of coding style. It may reflect variation in writing mathematical expressions: A fairly common practice is to divide before an operator in display formulas, but after an operator in text formulas. A “+” sign at the end of a line normally indicates that the expression continues on the next line, whereas a “+” sign at the start of a line normally indicates that the expression is continuation of something that started on the preceding line. In mathematics, people may use “+” both at the end of a line and at the start of the next line, for assumed clarity, but this is nonstandard and cannot of course be used in programming, as it would cause a syntax error. So you have to make your choice. -- Yucca, http://www.cs.tut.fi/~jkorpela/
[toc] | [prev] | [next] | [standalone]
| From | Christoph Michael Becker <cmbecker69@arcor.de> |
|---|---|
| Date | 2014-05-05 13:02 +0200 |
| Message-ID | <53676fba$0$6705$9b4e6d93@newsspool3.arcor-online.net> |
| In reply to | #24044 |
Jukka K. Korpela wrote: > 2014-05-05 5:07, Andrew Poulos wrote: > >> I typically concatenate long stings with trailing spaces and plus >> signs eg. >> >> var str = "blah blah... blah " + >> "blah blah... blah " + >> "blah blah... blah " + >> "blah blah... blah " + >> "blah blah... blah"; >> >> but I've seen code with leading plus signs and spaces eg. >> >> var str = "blah blah blah" >> + " blah blah... blah" >> + " blah blah... blah" >> + " blah blah... blah" >> + " blah blah... blah"; >> >> Is their a reason to prefer one over the other? > > This is just a matter of coding style. ACK. However, wrt. to ECMAScript implementations one must be aware of the automatic semicolon insertion (which doesn't apply for the given examples, though). Some authors recommend to always break a line *after* an operator (in the broadest sense) to avoid problems with the ASI. -- Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-05-05 19:45 +0200 |
| Message-ID | <14301371.mnbpq9uMup@PointedEars.de> |
| In reply to | #24043 |
Andrew Poulos wrote:
> I typically concatenate long stings with trailing spaces and plus signs
> eg.
>
> var str = "blah blah... blah " +
> "blah blah... blah " +
> "blah blah... blah " +
> "blah blah... blah " +
> "blah blah... blah";
>
> but I've seen code with leading plus signs and spaces eg.
>
> var str = "blah blah blah"
> + " blah blah... blah"
> + " blah blah... blah"
> + " blah blah... blah"
> + " blah blah... blah";
>
> Is their a reason to prefer one over the other?
The reason I prefer the latter over the former is better readability of
code. In fact, I prefer
var str = "blah blah blah"
+ " blah blah... blah"
+ " blah blah... blah"
+ " blah blah... blah"
+ " blah blah... blah";
then (equals and operators aligned), or
var str = "blah blah blah"
+ " blah blah... blah"
+ " blah blah... blah"
+ " blah blah... blah"
+ " blah blah... blah";
(operators indented one level; depends on the length of the prefix as too
many leading spaces are wasteful). Lines can be long even if they do not
exceed the recommended 80-columns mark, and as I am reading from left to
right, it is more obvious to me that concatenation (or any other operation)
occurs if the operator is at the front where it is easily visible.
Another reason is that I can remove lines without having to bother much
about the operator. Remove the last line from
var str = "blah blah blah" +
" blah blah... blah" +
" blah blah... blah";
and you get a syntax error or worse:
var str = "blah blah blah" +
" blah blah... blah" +
Remove it from
var str = "blah blah blah"
+ " blah blah... blah"
+ " blah blah... blah";
and you get only a warning about a “missing semicolon”:
var str = "blah blah blah"
+ " blah blah... blah"
Avoiding to produce syntax errors accidentally is important, and can be very
important when writing longer complex programs (like libraries) if the code
is checked as you type (which I prefer).
A reason other people prefer the former over the latter might be that such
code is easier to write for them. I have observed, for example, that
Eclipse JSDT (which I also use) can automatically insert “" + "” when you
press the Return key while in a string literal. They also might put greater
emphasis on the fact that removing the first line from
var str = "blah blah blah" +
" blah blah... blah" +
" blah blah... blah";
does not result in a syntax error, while removing it from
var str = "blah blah blah"
+ " blah blah... blah"
+ " blah blah... blah";
results in
+ " blah blah... blah"
+ " blah blah... blah";
which in most cases evaluates to NaN (“unary +” operator).
That said, on occasion I prefer a third (or fourth, depending on how you
count) variant instead:
var str = [
"blah blah blah",
"blah blah... blah",
"blah blah... blah",
"blah blah... blah",
"blah blah... blah"
].join(" ");
Particularly so when the glue is not " ", but "\n" or "<br>\n", or there are
many parts to concatenate. There are also code efficiency considerations
here. It is in general more efficient to build a string consecutively from
an Array of strings than to built it from consecutive concatenation, because
no intermediate strings are created.
var out = [];
for (var i = 0; i < 10; ++i)
{
/* creates one string value */
out.push("foo " + i + " bar");
}
out.join("")
instead of
var out = "";
for (var i = 0; i < 10; ++i)
{
/* creates two string values */
out += "foo " + i + " bar";
}
out
Knowing other programming languages, I think it is a deficiency of
ECMAScript that it allows neither to let string literals span lines without
escape sequence nor supports concatenation of strings without explicit
operator. Life could be easier for programmers if code like
/* as in C++ with string.hpp, or Python */
var str = "blah blah blah"
" blah blah... blah"
" blah blah... blah";
and/or
/* as in shell scripting, PHP, or Python with triple quotes/apostrophes */
var str = "blah blah blah
blah blah... blah
blah blah... blah";
was syntactically valid (although the latter does not work well with
indentation; likewise for here-doc.)
Edition 5.1, §7.8.4, finally standardizes
var str = "blah blah blah\
blah blah... blah\
blah blah... blah";
but that is not the same. Particularly, it is equivalent to the former
syntax except that it does not work as well with indentation.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2014-05-09 11:39 +1000 |
| Message-ID | <-dCdnfqRbY2Er_HOnZ2dnUVZ_oudnZ2d@westnet.com.au> |
| In reply to | #24043 |
On 9/05/2014 7:58 AM, Stefan Ram wrote: > Andrew Poulos <ap_prog@hotmail.com> writes: >> I typically concatenate long stings with trailing spaces and plus signs eg. >> var str = "blah blah... blah " + >> "blah blah... blah " + >> "blah blah... blah " + >> "blah blah... blah " + >> "blah blah... blah"; >> but I've seen code with leading plus signs and spaces eg. >> var str = "blah blah blah" >> + " blah blah... blah" >> + " blah blah... blah" >> + " blah blah... blah" >> + " blah blah... blah"; >> Is their a reason to prefer one over the other? > > Those are /intermediate/ plus signs in both cases. > > Trailing plus signs would be (not possible in JavaScript): > > str = "a"+ "b"+; > > . Preceding plus signs would be: > > str = +"a" +"b" /* gives "NaNb" */; > > . But what you have are intermediate plus signs: > > str = "a" + "b"; Thanks but I think you misunderstood my question. I wasn't asking about how leading or trailing plus signs might affect interpretation but why, in a "long" text string that is written over a number of lines, do some people prefer plus signs to be leading (or trailing) when concatenating the string. Andrew Poulos
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-05-09 10:13 +0200 |
| Message-ID | <4274232.bnBKQZDH9F@PointedEars.de> |
| In reply to | #24049 |
Stefan Ram wrote: > Andrew Poulos <ap_prog@hotmail.com> writes: >> Thanks but I think you misunderstood my question. I wasn't asking about >> how leading or trailing plus signs might affect interpretation but why, >> in a "long" text string that is written over a number of lines, do some >> people prefer plus signs to be leading (or trailing) when concatenating >> the string. > > When JavaScript is being interpreted, it might be most > run-time efficient to just write a single long line: > […] > Otherwise, I do not indent the following lines, […] You are still missing the point. -- PointedEars FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/> Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix> Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Norman Peelman <npeelman@cfl.rr.com> |
|---|---|
| Date | 2014-05-09 22:43 -0400 |
| Message-ID | <lkk3o3$hd9$1@dont-email.me> |
| In reply to | #24049 |
On 05/08/2014 09:39 PM, Andrew Poulos wrote:
> On 9/05/2014 7:58 AM, Stefan Ram wrote:
>> Andrew Poulos <ap_prog@hotmail.com> writes:
>>> I typically concatenate long stings with trailing spaces and plus
>>> signs eg.
>>> var str = "blah blah... blah " +
>>> "blah blah... blah " +
>>> "blah blah... blah " +
>>> "blah blah... blah " +
>>> "blah blah... blah";
>>> but I've seen code with leading plus signs and spaces eg.
>>> var str = "blah blah blah"
>>> + " blah blah... blah"
>>> + " blah blah... blah"
>>> + " blah blah... blah"
>>> + " blah blah... blah";
>>> Is their a reason to prefer one over the other?
>>
>> Those are /intermediate/ plus signs in both cases.
>>
>> Trailing plus signs would be (not possible in JavaScript):
>>
>> str = "a"+ "b"+;
>>
>> . Preceding plus signs would be:
>>
>> str = +"a" +"b" /* gives "NaNb" */;
>>
>> . But what you have are intermediate plus signs:
>>
>> str = "a" + "b";
>
> Thanks but I think you misunderstood my question. I wasn't asking about
> how leading or trailing plus signs might affect interpretation but why,
> in a "long" text string that is written over a number of lines, do some
> people prefer plus signs to be leading (or trailing) when concatenating
> the string.
>
> Andrew Poulos
>
My preference is for preceding such that:
var str = "blah blah... blah"
+ "blah blah... blah"
+ "blah blah... blah"
+ "blah blah... blah"
+ "blah blah... blah";
...the beginnings (+) of each continued line lines up (=) when possible
(in language of choice). Sometimes even breaking the lines up on visual
cues for sake of readability. Even in languages where a continuation
marker is required such that:
var str = "blah blah... blah" _
+ "blah blah... blah" _
+ "blah blah... blah" _
+ "blah blah... blah" _
+ "blah blah... blah"
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2014-05-09 10:11 +0200 |
| Message-ID | <1430916.RbAtrO6aNY@PointedEars.de> |
| In reply to | #24043 |
Stefan Ram wrote: > Andrew Poulos <ap_prog@hotmail.com> writes: >>I typically concatenate long stings with trailing spaces and plus signs >>eg. var str = "blah blah... blah " + >> "blah blah... blah " + >> "blah blah... blah " + >> "blah blah... blah " + >> "blah blah... blah"; >>but I've seen code with leading plus signs and spaces eg. >>var str = "blah blah blah" >> + " blah blah... blah" >> + " blah blah... blah" >> + " blah blah... blah" >> + " blah blah... blah"; >>Is their a reason to prefer one over the other? > > Those are /intermediate/ plus signs in both cases. > > Trailing plus signs would be […] [de] Nuhr. ,<http://www.oxforddictionaries.com/definition/english/trail?q=trailing#trail__16> | | [NO OBJECT] (Typically of a plant) grow or hang over the edge of something | or along the ground: (whereas the edge would be the perceived edge of the source code) ,-<http://www.merriam-webster.com/dictionary/trail> | | 3 […] | c : to follow along behind -- PointedEars FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/> Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix> Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2014-05-09 12:40 +0100 |
| Message-ID | <7fLY8UVy6LbTFwYV@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #24043 |
In comp.lang.javascript message <T72dnUGM8ZJAb_vOnZ2dnUVZ_g2dnZ2d@westne t.com.au>, Mon, 5 May 2014 12:07:56, Andrew Poulos <ap_prog@hotmail.com> posted: >I typically concatenate long stings with trailing spaces and plus signs eg. > >var str = "blah blah... blah " + > "blah blah... blah " + > "blah blah... blah " + > "blah blah... blah " + > "blah blah... blah"; > >but I've seen code with leading plus signs and spaces eg. > >var str = "blah blah blah" > + " blah blah... blah" > + " blah blah... blah" > + " blah blah... blah" > + " blah blah... blah"; > >Is their a reason to prefer one over the other? Yes. With a trailing + it is immediately obvious to a human reader of the line, without looking ahead, that there is a continuation. That applies particularly in the case of a single continuing line; with many continuing lines, the overall structure will be noticed peripherally while the first line is being considered - if they are on-screen. JavaScript treats them the same, although there may be a very small browser-dependent speed difference at the parsing stage. Obviously you can measure that well enough to see whether it is significant. -- (c) John Stockton, Surrey, UK. ¬@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036) Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web