Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coding standards and line length [closed]

Every coding standard I've ever seen has a recommended or absolute limit on number of characters in a line. There are various ways of working within this limitation, but I've not seen any specific guidance in this regard.

Obviously, if possible, don't write excessively long lines.

But what if that's not practical? How should long lines be handled?

Here are a couple of examples

if ($Stmt = $Mysqli->prepare("SELECT color, pattern, size,
                              manufacturer, mfgSku, storeLocation,
                              aisle, status
                              FROM tblItems WHERE ourSku = ?")) {

or

$flavors = array ('chocolate', 'strawberry', 'vanilla', 'cookie dough', 
                  'chocolate chip', 'mint chocolate chip', 'rocky road',
                  'peach', 'fudge brownie', 'coffee', 'mocha chip');

or

$Stmt->bind_result( $this->_firstName,
                    $this->_lastName,
                    $this->_BillToAddress->address1,
                    $this->_BillToAddress->address2,
                    $this->_BillToAddress->city,
                    $this->_BillToAddress->state,
                    $this->_BillToAddress->zip,
                    $this->_BillToAddress->country,
                    $this->_email,
                    $this->_status,
                    $this->_primaryPhone,
                    $this->_mobilePhone );

In each of these examples, the indenting of lengthy code is different. Is there a better or more "standard" way of doing this? Should extra lines always be indented the same way. Or is this OK?

like image 677
PartialOrder Avatar asked Feb 19 '09 16:02

PartialOrder


4 Answers

My personal preference is the following;

$Stmt->bind_result(
    $this->_firstName,
    $this->_lastName,
    $this->_BillToAddress->address1,
    $this->_BillToAddress->address2,
    $this->_BillToAddress->city,
    $this->_BillToAddress->state,
    $this->_BillToAddress->zip,
    $this->_BillToAddress->country,
    $this->_email,
    $this->_status,
    $this->_primaryPhone,
    $this->_mobilePhone 
);

That way the closing bracket and semi-colon are on the same indent as the opening call. Not all languages support having parameters on another line to the method call though...

like image 168
Daniel Morris Avatar answered Nov 20 '22 00:11

Daniel Morris


There is a pattern you can see in each example - they are indented to the first parameter of the function. This is a good standard to follow as it transposes the data from horizontal to vertical and the columns allow easy reading.

For other line length issues, such as lengthy computations, the preferred method is to break it down. Calculating the julian date, or easter is done in several steps instead of one long calculation.

like image 24
Adam Davis Avatar answered Nov 20 '22 02:11

Adam Davis


Context dictates which one you pick. Ultimately you're writing code to be read by a human being. If indenting a block of code differently would make it easier to read then do it.

like image 37
Allain Lalonde Avatar answered Nov 20 '22 01:11

Allain Lalonde


An unusual indentation style that I found myself easing into when doing a lot of SQL work was;

INSERT INTO someTable
(
    id,
    name,
    age,
    address1,
    address2,
)
VALUES
(
    2,
    'Bob'
    25,
    '12 Fake Street',
    'The Moon'
)

I actually find it much easier to read than any other layout for long parameter lists.

like image 5
Chris McAtackney Avatar answered Nov 20 '22 02:11

Chris McAtackney