Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Considering multiple lines of say SQL query strings:

$sql = "select * from table ".
"where a=5";

What do you think are the benefits and downsides of putting the concatenation character at the end or beginning of the line?

This is to revise a coding guidelines document, we have 2 people here that "feel" it should be either way

share|improve this question
    
One of them thinks it should go at the beginning and the other thinks it should go at the end? Right? –  Rook Mar 15 '11 at 22:49
    
One of them thinks it should go at the beginning and the other thinks it should go at the end, when in fact there must be no concatenation character at all, since PHP enables to write strings on multiple lines. –  MainMa Mar 15 '11 at 23:14
    
@MainMa but then you get a new line character in the string, which may not be what you want. So sometimes you need a concatenation character. –  James Mar 16 '11 at 14:36
    
Umm... Readability? Maintainability? –  Jim G. Dec 2 '11 at 17:16
add comment

5 Answers

up vote 3 down vote accepted

Just from scanning I would say that a dot at the beginning of a line give a more clear sight while scanning the left ruler of a block of text since the left side is "fixed" and the right side not. On the right side your eyes have more trouble finding the dots.

Let a friend take a stopwatch....

Shout "Start!":

add_settings_section(
  Config::GetPluginSlug() .
    Config::GetModuleMenuSlug($this->_module_id) . '_menu_section_main',
  Config::GetModuleSectionHeaderTitle($this->_module_id),
  array($this,'SectionHeader'),
  Config::GetPluginSlug() .
    Config::GetModuleMenuSlug($this->_module_id) . '_page'
);

Shout "Stop!" (...write down how many dots you saw...)

Shout "Start!"

add_settings_section(
  Config::GetPluginSlug()
  . Config::GetModuleMenuSlug($this->_module_id) . '_menu_section_main',
  Config::GetModuleSectionHeaderTitle($this->_module_id),
  array($this,'SectionHeader'),
  Config::GetPluginSlug()
  . Config::GetModuleMenuSlug($this->_module_id) . '_page'
);

Shout "Stop!" (...write down how many dots you saw...)

Now you have the scientifically tested answer :) (well not really... let a test group do the same test on the pieces of text reversed since you were already trained by reading the first part before the second).

share|improve this answer
    
interesting idea, fair enough –  jdog Mar 17 '11 at 20:19
add comment

Placing the concatenation character (or any operator) at the beginning of the line improves readability. We scan code by focusing on the beginning of each line. When a line starts with an operator, the reader can tell that the line is a continuation of the previous statement by scanning that one character.

Long mathematical expressions are always typeset so that each new line begins with an operator. There is no reason that code should not follow this convention.

This is an argument against implicit statement termination.

share|improve this answer
1  
Thinking back to a bunch of page long maths equations I've seen recently that makes sense, good point. –  James Mar 16 '11 at 14:58
    
+1. And additional argument: putting dots at the beginning of line => vertically aligned => more readable and less risk of forgetting one dot (even if the IDE should detect it) –  Frosty Z Dec 2 '11 at 15:41
add comment

Personally, I make it readable by indenting the following lines:

$sql = "select * from table ".
     "where a=5";

$sql = "select * from table "
     ."where a=5";

Which means that whether the . is at the start or the end doesn't bother me much.

share|improve this answer
    
Indentation can be misleading. –  kevin cline Mar 17 '11 at 4:07
    
@kevincline how so? –  James Mar 17 '11 at 7:52
    
Thanks again everyone for the feedback. We'll switch to beginning of line - and we already use that type of indentation above –  jdog Mar 17 '11 at 20:19
add comment

NEITHER! Just stick to the official way of doing it. From http://www.elated.com/articles/creating-php-strings/ Creating multi-line strings. If you want to insert a multi-line string in your PHP code, just use newlines to end each line:

$myString = "Line 1\nLine 2\nLine 3\n";

$myString = " 
  In himself he is;
  But in this kind, wanting your father's voice,
  The other must be held the worthier.
";

EDIT: An SQL query would look like:

$sqlQueryText = " 
  delete from users
  where first_name='Bobby'
  and last_name='Tables'
  or middle_name is not null;
";

Sorry, I have not done PHP, so I do not know if single quotes need to be escaped, but it is still doable as a multi-string.

share|improve this answer
1  
+1 for the best answer. If it's an SQL query, there are multiline strings for this. If it's a source code like the one posted by edelwater, it's unreadable in both cases because of the lack of refactoring. So I don't really see where can we need such concatenation. –  MainMa Mar 16 '11 at 0:18
    
But this is only for multi line strings. This way you get a new line character in the string, as well as any indention. Depending on the circumstances, that may be a bad thing. So sometimes you do need a concatenation character. –  James Mar 16 '11 at 14:39
    
Disagree, this (and php,net) simplify things too much, since everyone ;) uses ORM frameworks and libraries, this becomes very tedious and unreadable. –  jdog Mar 17 '11 at 20:18
    
@jdog, I am not sure if you are being sarcastic or not. –  Job Mar 17 '11 at 20:58
1  
This is the best answer, but only because the example in the question is an SQL query. SQL does not care about newlines in the string. –  Izkata Dec 2 '11 at 22:34
show 1 more comment

Whether the concatenation character is at the beginning or the end of the line is justifiable either way. Trumping the issue is: consistency.

I recommend:

  • Conform to the existing placement (within the code)
  • Pick one and ensure its consistency throughout the code
share|improve this answer
1  
Good answer. Just toss a coin and move on :-) –  James Mar 16 '11 at 14:39
    
Mmh yes, partially agree, but if you work in a small number of frameworks you have to show some leadership yourself. Hence we have our own coding guidelines –  jdog Mar 17 '11 at 20:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.