Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #24046

Re: Strings with trailing plus

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Strings with trailing plus
Date 2014-05-05 19:45 +0200
Organization PointedEars Software (PES)
Message-ID <14301371.mnbpq9uMup@PointedEars.de> (permalink)
References <T72dnUGM8ZJAb_vOnZ2dnUVZ_g2dnZ2d@westnet.com.au>

Show all headers | View raw


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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web