20

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?

EBGreen
  • 36,735
  • 12
  • 65
  • 85
PartialOrder
  • 2,870
  • 3
  • 36
  • 44
  • I have mixed feelings on this. I initially thought dupe since line length and standards around it has been discussed ad nauseum. On second thought, I think this may be distinct enough to stand on it's own. – EBGreen Feb 19 '09 at 16:30
  • Having said that, I would suggest reading the other questions where line length and how to handle it has been beat to death. http://stackoverflow.com/questions/131468/what-is-a-sensible-maximum-number-of-characters-per-line-of-code-closed – EBGreen Feb 19 '09 at 16:30
  • Based on coobirds links, I think this is a dupe – annakata Feb 19 '09 at 16:49

14 Answers14

12

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...

Daniel Morris
  • 6,852
  • 8
  • 25
  • 30
11

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.

reformed
  • 4,505
  • 11
  • 62
  • 88
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
6

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
  • 91,574
  • 70
  • 187
  • 238
5

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.

Chris McAtackney
  • 5,192
  • 8
  • 45
  • 69
4

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
  • 74,572
  • 17
  • 113
  • 180
  • 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. – PartialOrder Feb 19 '09 at 16:55
3

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.
  • 2,496
  • 19
  • 20
  • 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 Feb 19 '09 at 16:37
1

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.

Alex McBride
  • 6,881
  • 3
  • 29
  • 30
1

In prose, lines longer than 80 or so have been shown to be harder to read. (See page 13 of LaTeX Memoir class documentation.) Code Complete (section 18.5, page 425 of my edition) of also references the 80 character limit with this proviso:

With larger screens, narrow typefaces, laser printers, and landscape mode, the arguments for the 80-character limit aren't as compelling as they used to me. A single 90-character-long line is usually more readable than one that has been broken in two just to avoid spilling over the 80the column. With modern technology, it's probably all right to exceed 80 columns occasionally.

I would indent the SQL in your first example separately from the rest of the 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 
);
Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
  • 1
    `In prose, lines longer than 80 or so have been shown to be harder to read` - citation? Shown by who? – Benubird Jan 14 '16 at 13:20
  • @Benubird: I updated the answer with my primary source. I've see this research referenced other places (and read papers describing the research), but I don't recall where they may be found off the top of my head. – Jon 'links in bio' Ericson Jan 14 '16 at 17:00
1

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.

Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
1

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

Pat
  • 5,761
  • 5
  • 34
  • 50
0

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.

Sampson
  • 265,109
  • 74
  • 539
  • 565
0

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
  • 55,990
  • 32
  • 132
  • 223
0

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

Apex
  • 1,018
  • 1
  • 11
  • 13
0

Some useful info here http://framework.zend.com/manual/en/coding-standard.coding-style.html

Mark Unwin
  • 1,613
  • 1
  • 15
  • 19