HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Hanging semicolons in multiline statements

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
statementsmultilinesemicolonshanging

Problem

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.

Solution

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:

http://www.php-fig.org/psr/psr-2

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); 
extract($statementInfoArray,'stm');
$rbt .= $startOfLine.
        padBothAndTruncate($stm_bill-to-addr1,$rbtSt_billToBox_addr_maxWidth).
        $innerMargin.
        substr($stm_bill-to-code,0,$rbtSt_billToBox_acctNum_maxWidth).
        $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.

Code Snippets

$startOfLine = PHP_EOL.
               str_repeat(' ',$rbtSt_billToBox_leadingWidth);
$innerMargin = str_repeat(' ',$rbtSt_billToBox_innerMargin); 
extract($statementInfoArray,'stm');
$rbt .= $startOfLine.
        padBothAndTruncate($stm_bill-to-addr1,$rbtSt_billToBox_addr_maxWidth).
        $innerMargin.
        substr($stm_bill-to-code,0,$rbtSt_billToBox_acctNum_maxWidth).
        $startOfLine.
        padBothAndTruncate($stm_bill-to-city.', '.$stm_bill-to-state.' '.$stm_bill-to-zip);

Context

StackExchange Code Review Q#70981, answer score: 3

Revisions (0)

No revisions yet.