I'm writing code to do line printing to a template -- counting characters to box A, then to box B, etc. -- and as such, it's not very modular. There are a lot of (long) variable names and each statement, which adds to the string to be printed, is pretty long.

So I've been breaking up the statements across multiple lines. But I don't know how I should be indenting it or where I should put the semicolon.

Here's a sample line:

$rbt .= "\n"
    . str_repeat(" ", $rbtSt_billToBox_leadingWidth)
    . padBothAndTruncate($statementInfoArray['bill-to-addr1'], $rbtSt_billToBox_addr_maxWidth)
    . str_repeat(" ", $rbtSt_billToBox_innerMargin)
    . substr($statementInfoArray['bill-to-code'], 0, $rbtSt_billToBox_acctNum_maxWidth)

I like how the concat operators are lined up and how it looks bracketed, but the naked semicolon on the following line makes me a little nervous. Putting it at the end of the line makes me even more nervous, since then it's hard to see.

It gets a little trickier when there's more nesting.

$rbt .= "\n"
    . str_repeat(" ", $rbtSt_billToBox_leadingWidth)
    . padBothAndTruncate(
        $statementInfoArray['bill-to-city'] . ", " . $statementInfoArray['bill-to-state']
            . " " . $statementInfoArray['bill-to-zip']

Do I always do multiple lines? Just when it is more than a certain number of characters (say, 120)? Once a statement is multiline, should I only have one operator per line?

I'm feeling a bit paralyzed by choices here and I fear that none of them are very good.

  • \$\begingroup\$ One thing to keep in mind is that this all comes down to convention and personal preference. Essentially, no way is right or wrong, and it's not like your code won't work if you write it in a certain way. Relax and try to find what looks best to you. \$\endgroup\$ – Ivo Coumans Nov 27 '14 at 9:05
  • \$\begingroup\$ This looks like just sample codes. This not appropriate here \$\endgroup\$ – morbidCode Nov 27 '14 at 10:13
  • \$\begingroup\$ They're not sample codes - they're from a real program I'm working on. I cannot post the entire thing as it's a closed-source project, sorry. \$\endgroup\$ – AmadeusDrZaius Nov 27 '14 at 10:13

This is indeed purely personal, however, when you work in a group, or want to share your code, it's best to stick to certain conventions. A good source for this is:


Eventhough these guidelines are quite good, I don't always follow them myself. For instance, I often use two spaces to indent, not four. Etc.

As for my personal opinion about any code: It should be very easy to read and understand. Your code wouldn't be accepted here, mainly due to the lack of comments and sometimes vague names. But that's not what your question is about. As for the concatenation dots, I like them at the end of lines:

$startOfLine = PHP_EOL.
               str_repeat(' ',$rbtSt_billToBox_leadingWidth);
$innerMargin = str_repeat(' ',$rbtSt_billToBox_innerMargin); 
$rbt .= $startOfLine.
        padBothAndTruncate($stm_bill-to-city.', '.$stm_bill-to-state.' '.$stm_bill-to-zip);

and then I align all the parts on the left side. This makes it very clear what belongs together. I am however sure there are many other ways to do this.

  • \$\begingroup\$ "mainly due to the lack of comments and sometimes vague names." I apologize for that - it's more clear what's going on with all of the code, but I wanted to post a MWE just focusing on the semicolons / indentation. Thanks for the suggestions. \$\endgroup\$ – AmadeusDrZaius Nov 27 '14 at 10:16
  • \$\begingroup\$ Yes, I assumed that might have been the case. \$\endgroup\$ – KIKO Software Nov 27 '14 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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