[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

What's your maximum line size preference?

Gianni Mariani

10/9/2008 9:31:00 AM


Do you have a preference on maximum line width for C++ code?

I've seen the craziest debates on this most silly of topic.

I have witnessed engineers spent oodles of time fiddling with line
breaks just to get it right. I find in general a prescriptive rule
makes for markedly less readable code, which unfortunately is a
subjective argument, however, the waste of time modifying code when it
does not need to is not.

I'm interested in knowing what you'all do.

G
54 Answers

Ian Collins

10/9/2008 9:37:00 AM

0

Gianni Mariani wrote:
>
> Do you have a preference on maximum line width for C++ code?
>
About 80. I find I seldom write lines longer than this and I prefer to
have several files open side by side.

--
Ian Collins.

Juha Nieminen

10/9/2008 11:34:00 AM

0

Gianni Mariani wrote:
> Do you have a preference on maximum line width for C++ code?

Historically 80 characters has been the recommended maximum because
text terminals were that many character wide, and trying to edit code
lines longer than that would require either scrolling horizontally
(assuming your text editor supported that) or ugly line wraps.

Nowadays 80-character-wide terminals are seldom used for programming
and instead freely-resizeable graphical editors on high-resolution
monitors are used instead, so there's no practical or technical limit.

However, personally I still keep my emacs at 80 characters and avoid
lines longer than that when programming. IMO code lines which are too
long are a bit like text lines which are too long: Hard to read. The
very worst case scenario is when code lines are so long that they won't
fit the width of your monitor and you either need to scroll horizontally
to see the entire line (very bad usability) or the lines are wrapped,
which makes the code even harder to read. I have seen code out there
which badly break this concept and use humongously long lines, which is
a braindead idea.

I think the 80 characters is a good width, although I wouldn't oppose
something slightly larger, eg. 100 characters.

Victor Bazarov

10/9/2008 12:45:00 PM

0

Gianni Mariani wrote:
> Do you have a preference on maximum line width for C++ code?
>
> I've seen the craziest debates on this most silly of topic.
>
> I have witnessed engineers spent oodles of time fiddling with line
> breaks just to get it right. I find in general a prescriptive rule
> makes for markedly less readable code, which unfortunately is a
> subjective argument, however, the waste of time modifying code when it
> does not need to is not.
>
> I'm interested in knowing what you'all do.

Readability is a concern in our team because it's a few hundred strong.
Any time you write code, you have to consider that (a) it's not going
to be read by you and (b) if it's going to be read by you, it can be a
year or more later. So, based on that, I would think that if the line
fully fits in my editor's code window, so I don't have to scroll the
window horizontally to read the entire line, the width is acceptable.
Now, it probably means that on the other man's monitor it could be too
long (if the code window isn't as wide as mine), and unfortunately there
is no cure for that. Hopefully, the monitors do get upgraded and if the
line lengths don't change, eventually all lines will fit. <weakG>

Just like any other style element, the line length participates in a
compromise. For example, you can shorten your lines by using shorter
names for variables and functions, but it detracts from readability in
its own, possibly more unpleasant, way.

Whitespace (line breaks included) has an important role in the code
readability, but I would probably say, "don't get hung up on it". The
ability to read and understand any code without wasting too much time on
reformatting is a skill *every programmer* needs to develop.

Just my $0.04...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Tim Slattery

10/9/2008 1:08:00 PM

0

Juha Nieminen <nospam@thanks.invalid> wrote:

>Gianni Mariani wrote:
>> Do you have a preference on maximum line width for C++ code?
>
> Historically 80 characters has been the recommended maximum because
>text terminals were that many character wide,

Just a digression: the 80-character-per-line standard goes all the way
back to Herman Hollerith and his 80-column punch cards, long before
anybody ever thought of text terminals.

--
Tim Slattery
Slattery_T@bls.gov
http://members.cox.net...

Juha Nieminen

10/9/2008 4:17:00 PM

0

Tim Slattery wrote:
> Juha Nieminen <nospam@thanks.invalid> wrote:
>
>> Gianni Mariani wrote:
>>> Do you have a preference on maximum line width for C++ code?
>> Historically 80 characters has been the recommended maximum because
>> text terminals were that many character wide,
>
> Just a digression: the 80-character-per-line standard goes all the way
> back to Herman Hollerith and his 80-column punch cards, long before
> anybody ever thought of text terminals.

But interactive programming didn't become viable until text terminals,
which is one of the biggest reasons why 80 characters has always been
the limit.

Erik Wikström

10/9/2008 5:03:00 PM

0

On 2008-10-09 11:30, Gianni Mariani wrote:
> Do you have a preference on maximum line width for C++ code?
>
> I've seen the craziest debates on this most silly of topic.
>
> I have witnessed engineers spent oodles of time fiddling with line
> breaks just to get it right. I find in general a prescriptive rule
> makes for markedly less readable code, which unfortunately is a
> subjective argument, however, the waste of time modifying code when it
> does not need to is not.

I'd say as short as possible without sacrificing the readability of the
code, but keep in mind that a modern monitor is wide. I also think that
different standards can be applied to different kinds of lines, I really
hate things like this:

SomeReturnType SomeClass::SomeMethod(SomeType1 artument1,
SomeType3 argument3,
SomeType4 argument4,
SomeType5 argument5,
SomeType6 argument6,
SomeType7 argument7)
{
// ....
}

Since you (in my experience) seldom have to read or change the arguments
of functions (at least of working on a large code-base with a few years
worth of code) you can just keep them all on one line, which gives a
more uniform formatting of functions and makes it easier to quickly scan
the code.

Also, when you get a few indentation levels deep you generally do not
have many columns left (at least not if you have a tab-size of 4 spaces
or more). I therefore think that it might be useful to count the line-
width from the first non-whitespace character (though I've been unable
to convince my employers this is a good idea).

Having said that, take up a few novel (or some other book) and count how
many characters they put on a line and you get a number somewhere around
80, there might be a good reason for that.

--
Erik Wikström

Paavo Helde

10/9/2008 9:40:00 PM

0

Gianni Mariani <gi4nospam@mariani.ws> kirjutas:

>
> Do you have a preference on maximum line width for C++ code?
>
> I've seen the craziest debates on this most silly of topic.
>
> I have witnessed engineers spent oodles of time fiddling with line
> breaks just to get it right. I find in general a prescriptive rule
> makes for markedly less readable code, which unfortunately is a
> subjective argument, however, the waste of time modifying code when it
> does not need to is not.
>
> I'm interested in knowing what you'all do.

I am trying to keep a single statement on a single line, unless it's
really starting to get off the screen. It seems the longest lines are
about 130-140 characters. Example of a line ending at pos 133:

HtmlCmdHandlerFile* main_file_handler = dynamic_cast
<HtmlCmdHandlerFile*>(HttpServer::InstanceNoStart().FindHandler("file"));

And yes, when working on the laptop the ends of some lines are not
visible. Given that vertically the cpp file is not fully visible anyway
on the screen, I have not turned much attention to this, and my
colleagues have not complained either.

Regards
Paavo

Chris Ahlstrom

10/10/2008 1:26:00 AM

0

After takin' a swig o' grog, Erik Wikström belched out
this bit o' wisdom:

> I'd say as short as possible without sacrificing the readability of the
> code, but keep in mind that a modern monitor is wide. I also think that
> different standards can be applied to different kinds of lines, I really
> hate things like this:
>
> SomeReturnType SomeClass::SomeMethod(SomeType1 artument1,
> SomeType3 argument3,
> SomeType4 argument4,
> SomeType5 argument5,
> SomeType6 argument6,
> SomeType7 argument7)
> {
> // ....
> }
>
> Since you (in my experience) seldom have to read or change the arguments
> of functions (at least of working on a large code-base with a few years
> worth of code) you can just keep them all on one line, which gives a
> more uniform formatting of functions and makes it easier to quickly scan
> the code.

I disagree violently <grin>.

SomeReturnType
SomeClass::SomeMethod
(
SomeType1 artument1, /**< Destination for the stuff. More comment.*/
SomeType3 argument3, /**< blah blah blah */
SomeType4 argument4, /**< blah blah blah */
SomeType5 argument5, /**< blah blah blah */
SomeType6 argument6, /**< blah blah blah */
SomeType7 argument7 /**< blah blah blah */
)
{
// ....
}

I really prefer dragging my eyes /down/ the page, not left to right,
especially wayyyyyy to the right, then back leffffffffffft again,
then down.

Why do you think newspaper columns are so narrow?

I like code I can page down through quickly and grok at a glance.

--
* LG loves czech girls.
<vincent> LG: do they have additional interesting "features" other girls
don't have? ;)
-- #Debian

Ian Collins

10/10/2008 1:48:00 AM

0

Chris Ahlstrom wrote:
>
> I disagree violently <grin>.
>
> SomeReturnType
> SomeClass::SomeMethod
> (
> SomeType1 artument1, /**< Destination for the stuff. More comment.*/
> SomeType3 argument3, /**< blah blah blah */
> SomeType4 argument4, /**< blah blah blah */
> SomeType5 argument5, /**< blah blah blah */
> SomeType6 argument6, /**< blah blah blah */
> SomeType7 argument7 /**< blah blah blah */
> )
> {
> // ....
> }
>
> I really prefer dragging my eyes /down/ the page, not left to right,
> especially wayyyyyy to the right, then back leffffffffffft again,
> then down.
>
> Why do you think newspaper columns are so narrow?
>
> I like code I can page down through quickly and grok at a glance.
>
That gets my vote. Having a child with an eye tracking problem, I've
been though all the exercises and it's surprising how long it takes for
a normal person to locate the start of the next line when scanning long
lines.

I've tried an edit window with 360 character lines and it isn't fun...
My guess is there's only so much text we can keep in short term memory,
so when a line is too long, we forget the beginning of the current line
which makes tracking to the next one hard.

I'm sure there's some papers on this somewhere!

--
Ian Collins

Hendrik Schober

10/10/2008 8:14:00 AM

0

Chris Ahlstrom wrote:
> After takin' a swig o' grog, Erik Wikström belched out
> this bit o' wisdom:
>
>> I'd say as short as possible without sacrificing the readability of the
>> code, but keep in mind that a modern monitor is wide. I also think that
>> different standards can be applied to different kinds of lines, I really
>> hate things like this:
>>
>> SomeReturnType SomeClass::SomeMethod(SomeType1 artument1,
>> SomeType3 argument3,
>> SomeType4 argument4,
>> SomeType5 argument5,
>> SomeType6 argument6,
>> SomeType7 argument7)
>> {
>> // ....
>> }
>>
>> Since you (in my experience) seldom have to read or change the arguments
>> of functions (at least of working on a large code-base with a few years
>> worth of code) you can just keep them all on one line, which gives a
>> more uniform formatting of functions and makes it easier to quickly scan
>> the code.
>
> I disagree violently <grin>.
>
> SomeReturnType
> SomeClass::SomeMethod
> (
> SomeType1 artument1, /**< Destination for the stuff. More comment.*/
> SomeType3 argument3, /**< blah blah blah */
> SomeType4 argument4, /**< blah blah blah */
> SomeType5 argument5, /**< blah blah blah */
> SomeType6 argument6, /**< blah blah blah */
> SomeType7 argument7 /**< blah blah blah */
> )
> {
> // ....
> }

Just for diversity, I like this:

SomeReturnType SomeClass::SomeMethod( SomeType1 argument1 /**< Destination for the stuff. More comment.*/
, SomeType3 argument3 /**< blah blah blah */
, SomeType4 argument4 /**< blah blah blah */
, SomeType5 argument5 /**< blah blah blah */
, SomeType6 argument6 /**< blah blah blah */
, SomeType7 argument7 /**< blah blah blah */ )
: data1(argument1)
, data3(argument3)
, data4(argument4)
, data5(argument5)
, data6(argument6)
, data7(argument7)
{
// ....
}

(But then I had never had to look at code at a machine which
only has vi. And hopefully I'll never will. :^> )

> [...]

Schobi