Page 1 of 1

The length of a line of PHP code?

Posted: Mon May 25, 2009 7:49 am
by kaisellgren
Hi,

I often find people and websites suggesting to use a max length of 80-100 for a line of code. Traditionally, the max length has been 80 characters, which was invented by COBOL around 1960 as far as I know. However, nowadays people have widescreen monitors and high resolution monitors, so, it makes me think about what is the "best" choice for a maximum length limit.

This is kind of blurry, because if you use a larger font or a larger font size - obviously you will run out of horizontal space sooner. So, I'm not sure what should be the limit of line height...

What are you thoughts on this?

Here's what I have been thinking:

- Coders have at least 1024 px in horizontal direction
- Coders use fixed-width fonts
- Coders use at most 14pt font size?

I've been trying to determine a good max line length and I usually come up with something like 80-100 characters/line.

However, this is not quite simple... netbooks are 20% of the laptop market - and they have smaller screens. What about people who have bad vision? They might use larger font sizes. To make matters worse, almost every single editor has sidebars, which take up some horizontal space.. so, it's pretty much impossible to find a good maximum limit, or is it?

I know that most editors have that "wrap overlong lines" feature, but if I have plenty of comments in my code, it may look bad if the editor wraps the lines based on the length..

Re: The length of a line of PHP code?

Posted: Mon May 25, 2009 7:58 am
by alex.barylski
- Coders use at most 14pt font size?
Nagative I still use 10pt...courier new...
To make matters worse, almost every single editor has sidebars, which take up some horizontal space.. so, it's pretty much impossible to find a good maximum limit, or is it?
I would say so...I personally focus more on SLOC (per method, module, file, class, etc).

To further complicate this issue...what about editors (or coders) enable wordwrap?

I personally *hate* long lines. Long lines typically mean confusing code (unless were talking language strings or something trivial). When I see long lines it's usually of the form:

Code: Select all

$a = ($b > $c ? ($d < $e ? $f : $g) : $h > $i ? $j : $k);
I hate the ternary operator for that reason...I much prefer IF statements in these cases but some developers just love putting complex lines of code on a single line... :banghead: :lol:
I know that most editors have that "wrap overlong lines" feature, but if I have plenty of comments in my code, it may look bad if the editor wraps the lines based on the length..
Ahhh...thats why you want to know...I dunno what to say...difficult question to answer :P

Re: The length of a line of PHP code?

Posted: Mon May 25, 2009 8:20 am
by kaisellgren
PCSpectra wrote:Nagative I still use 10pt...courier new...
I see. Courier New 10pt on my 24" looks like waste of an insect... :D
PCSpectra wrote:I personally *hate* long lines. Long lines typically mean confusing code (unless were talking language strings or something trivial). When I see long lines it's usually of the form:

Code: Select all

$a = ($b > $c ? ($d < $e ? $f : $g) : $h > $i ? $j : $k);
I hate the ternary operator for that reason...I much prefer IF statements in these cases but some developers just love putting complex lines of code on a single line... :banghead: :lol:
Hmm, yeah. Sometimes you just can't split, think about regular expressions, for instance, but the question is this: where do you draw the line between long lines and short lines? :roll:

Re: The length of a line of PHP code?

Posted: Mon May 25, 2009 11:40 am
by alex.barylski
Hmm, yeah. Sometimes you just can't split, think about regular expressions, for instance, but the question is this: where do you draw the line between long lines and short lines?
Regular expressions can usually be greatly simplified by taking advantage of string interpolation:

http://www.iamcal.com/publish/articles/ ... ing_email/

Now the regex resembles something akin to EBNF as opposed to the crazy cryptic RE's

Anyways, to answer your question, I draw the line pretty short. The only time line length becomes an issue for me is when leaving inline comments or language strings, in which case I usually move the comment to a new line and/or replace the language text with a DEFINE placeholder or something similar.

I hate scrolling horizontally...drives me nutts...so I avoid long lines as much as I can.

Cheers,
Alex

Re: The length of a line of PHP code?

Posted: Mon May 25, 2009 12:05 pm
by kaisellgren
PCSpectra wrote:Regular expressions can usually be greatly simplified by taking advantage of string interpolation:
Well yeah you could just do:

Code: Select all

preg_replace('...'.
'...'.
'...','','');
PCSpectra wrote:Anyways, to answer your question, I draw the line pretty short.

I hate scrolling horizontally...drives me nutts...so I avoid long lines as much as I can.
So, maybe I'll stick with the sum of Euler's totient function over the first sixteen integers :)