views:

945

answers:

15

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?

+6  A: 

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.

Adam Davis
A: 

I'm not aware of any standard, as it would be tough to say. For those of us on larger monitors, we can view more horizontal code than others on smaller monitors. I generally try to build long strings sequentially via .= (PHP) when it's necessary, and as your code demonstrated I split lengthy arrays arbitrarily onto new lines depending on how many characters exist in that particular line.

Jonathan Sampson
+3  A: 

I don't think one should intentionally have long lines but, at the risk of offending many, I suggest that line length really isn't that important anymore.

Vim and emacs handle long lines pretty well, and they're installed on almost every Unix box. On Windows, you'll almost always be within a GUI text editor. I do think your $Stmt->bind_result style is the easiest to read, but if you just need to load a bunch of mostly static information in one statement, I have no problem with a 1000 character line.

Oliver N.
and if the long line REALLY irks you, you can always turn on line wrap... with line numbers displayed per-line, it's really not too bad. +1
rmeador
+5  A: 

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.

Allain Lalonde
+4  A: 

Door number 3. If you can't do it on one line, do it on one line per item, anything else obfuscates the items after the first on the line and is horrible to read. Consistent indentation matters too.

Fwiw, I think this is old hat in a day and age where most programmers should have dual monitors at a high resolution. The first example looks like it could be one line quite happily.

annakata
Apart from the question of standards, the high resolution doesn't change the nature of reading. When lines become too long, they are hard to read. I can fit more than twice what I'm comfortable reading on the width of my screen. Using the full width would only slow me down.
Clayton
+1  A: 

This is pretty subjective really, much like the position of brackets, and other coding-styles. The main thing is not so much which style you choose, but that you do choose a style, and that you stick to it throughout the project.

For me personally, coming from a Python background, I use a line-length of 79 and a

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

style.

But as I say, in my opinion, it's more important to have a style, rather than to worry about which one.

Fara
A: 

None of these is that bad... ;-) The first looks like it came from some of my own code.:

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

The second example might be better if it were loaded into a configuration file or table.

The third is fine, but you could tighten it up a bit with:

$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 prose, lines longer than 80 or so have been shown to be harder to read. 60 or less would be even better. I think code is more like poetry and so line ought to be even shorter if possible. But it's probably not worth spending a huge amount of time worrying about.

Jon Ericson
A: 

I don't mind option 3 too much (1 item per line). Also, personally, I always use wordwrapping, so having code on one line doesn't bother me at all. However, in your second example, with wordwrapping on, that could look like a mess to programmers who do use wordwrapping. Perhaps I am of a smaller group who don't mind long lines.

Typeoneerror
A: 

The best practice typically stems from the purposes behind the line-length restriction itself:

  • Increase interoperability (between programmers, editing software, etc.)
  • To increase readability and comprehension
  • To increase enjoyment and development speed
  • To increase revenues and profits

Thus, your choices, such as aligning all the stmt parameters, are good if they contribute both to your own future comprehension and that of others on your team.

Hope this helps ;) -M

Matt Gardner
+5  A: 

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 languges support having parameters visibly on another line to the method call though...

danieljohnmorris
+1 because I prefer this approach as well
pd
+1  A: 

If you're wrapping more than two items, I prefer a new line for each item like in your third example. It's easier for automated source control tools to merge edits from other people if there's only one item per line.

Kristo
+4  A: 

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.

C.McAtackney
I have done this although it doesn't really fix the awful SQL insert syntax. Why cant we INSERT INTO someTable (id=1, foo=3, bar='gold') ??
Nick
I do this too. *But* I also find this style is tricky when using copy and paste.
Jon Ericson
Nick: MySQL at least supports syntax like INSERT INTO table SET foo = "foo", bar="bar" etc.
pd
+1  A: 

Follow the standard used by the surrounding code. Don't create your own "standard' no matter how much "better".

Pat