Thread: Opinion on line length

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    32

    Opinion on line length

    Hey all! This is just a question of style and preference, I guess. But still, I'd like to know how you veterans code. ;)

    Maximum line length in your source code, if any? I read a paper on C guidelines somewhere and was hinted to use a max. of 72 characters, due to historical reasons. I'm beginning to find this a bit too restrictive and perhaps not that necessary. My DOS console displays 80 characters, and I'm thinking about settling at that.

    So what do you prefer? :)
    -tretton

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Prefer 80. Not set in stone.

    [edit]For one system I forced myself to reduce it by about 10, which annoyed me, because it made looking at the generated assembly much more tolerable.
    Last edited by Dave_Sinkula; 01-07-2006 at 10:57 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    I just let it go on for as long as it needs. Shorter lines get their comment at the end of the line. Longer lines get their comments placed above the line.

    Of course, if it's really really ridiculous, then I'll hit enter somewhere in the middle of the line...sometimes. But then again, if you're bordering on 150 charactesr per line, you can probably break it up into smaller chunks, and not just rely on "Enter" to make everything look good.

    (In my current project, my longest line is 182 characters. One of the exceptions to the "smaller chunks" suggestion).
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    I'm interested to see which line of code is 182 characters... any reason why you don't break it up?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I don't like lines to extend beyond the right side of the window in which I'm working. Of course I don't change the size of the line if I resize the window smaller I almost never put more than one semicolon on the same line, so normally the lines are pretty short except for spaces/tabs. If there are very long string literals, I'll break them up too so that they are easly read without scrolling the window.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Heh, it's just a really long function definition:
    Code:
    void cWindow::Init(char BackgroundPath[50], HINSTANCE *hInstance, unsigned int ClassStyles,
    unsigned long WindowStyles, unsigned int Width, unsigned int Height, HBRUSH DefaultBrush)
    The longest "in-function" code I have is 167:
    Code:
    if(FAILED(D3DXCreateFont(D3DDevice, 22, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET,
    OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Arial", &Font)))
    (I split them both up here so as not to have ginormous scrolling action going on.

    It's really just my personal preference. Once I write the code, I know it works, and if there's little chance of me going back to change it, then I really don't need to be looking at it. And if I do have to, well, I'll just scroll a little. The long lines usually only happen when I'm filling in parameters for a function, or writing the definition for a function.

    Code like this (120 characters):
    Code:
    float Weight = (TERRAIN_HEIGHT / NUM_TEXTURES - abs(Height - ((I + 1.0f) * dT - 1.0f)) / TERRAIN_HEIGHT / NUM_TEXTURES;
    Got split up into:
    Code:
    float dT = TERRAIN_HEIGHT / NUM_TEXTURES;
    
    float dY = Height - ((I + 1.0f) * dT - 1.0f);
    					
    if(dY < 0.0f)
    	dY = dY * -1;
    
    float Weight = (dT - dY) / dT;
    Like I said, for function definitions, they'll stay. For longer pieces of code, I'll usually break it down like that. It helps a lot in being able to debug, especially with long math equations or similarly complex code.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  7. #7
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    C coding style standards differ between projects, groups, and individuals. Most of them are based on the notion that the code will be viewed in a standard 80 column terminal or printed on 80 column paper. I've tended to use 76 or 78 as my maximum line length. Some 80 column displays and printers automatically perform a newline when a character is displayed in column 80, and then the newline at the end of the 80 character line displays as an empty (blank) line. Other terminals wrap (perform the newline operation) when an attempt is made to display a character in the 81st position. Real DEC VT terminals have the latter behavior, many others have the former.

    Why 80 columns? That's historical. IBM 029 punch cards had 80 columns, thus could contain 80 characters per card. Many video terminals provided 80 columns in order to match the line length of a punch card.

    I've only worked in one place where the C coding standard allowed a maximum of 72 columns per source line in order to allow for the last 8 columns to be used for a punched card sequence number, but that was common in Fortran coding style standards.

    Thus endeth the history lesson.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to get the length of the string in one line
    By transgalactic2 in forum C Programming
    Replies: 15
    Last Post: 03-24-2009, 04:00 AM
  2. Imposing Line Numbers automatically in the C code
    By cavestine in forum C Programming
    Replies: 14
    Last Post: 10-15-2007, 12:41 AM
  3. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 11:10 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM