Like Tree1Likes

Opinions on line length etiquette?

This is a discussion on Opinions on line length etiquette? within the General Discussions forums, part of the Community Boards category; Hey guys, what's your maximum line length? I've started getting some pretty long lines because of variables names and the ...

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,275

    Opinions on line length etiquette?

    Hey guys, what's your maximum line length? I've started getting some pretty long lines because of variables names and the STL. I don't think I'm breaking 120 characters but I'm just curious, what's the proper etiquette here?

    Instead of this :
    Code:
     
    bool three_to_two_flip(std::vector<tetra*> &pile, tetra *t, const std::array<point*, 3> &f, const point *a, const point *b, mesh &m)
    should I be typing this?
    Code:
    bool three_to_two_flip(std::vector<tetra*> &pile, 
                                   tetra *t, 
                                   const std::array<point*, 3> &f, 
                                   const point *a, 
                                   const point *b,
                                   mesh &m) {
    }
    I will say one thing in favor of super long lines, it makes commenting them out with // much, much easier.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,675
    Here? Like C Board? I don't know that we have one, but I would say 80-120 max is about right. Even though my browser window is much wider, I don't like long lines of code.

    In general?
    I would say 80 is pretty universally accepted -- even if your display can handle wider, 80 looks fine. 120 is more common nowadays for people who develop in GUI environments, and for people who simply aren't stuck in the old terminal days. Much more than 120, and it tends to get annoying. Either because I have to scroll a bunch, or because 99% of my lines are 60ish chars, and one is 348 chars long. It just looks bad. Also, I think there is some biology/physiology behind it, in how much the human eye/brain can easily absorb when reading top-to-bottom (i.e. excessive left-to-right scanning is inefficient). Think about the aspect ratio of most books, paper and such.

    Personally I stick to 80 chars pretty strictly. Work requires it (even for C++ code with lots of template usage, though we have some super-sexy Clang formatting scripts/rules setup) and I spend enough time in Linux-land that I'm used to it and use it for personal stuff, unless I'm working on a project where the convention is otherwise. 80 chars in C++ code is okay, as is 120; wrapping a function definition is fine and typically expected for long function definitions. I would say the etiquette is: pick format that is sufficiently easy to read, and be consistent in using it.

    That etiquette probably applies to almost all aspects of formatting/indentation/readability.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Quote Originally Posted by MutantJohn
    Hey guys, what's your maximum line length? I've started getting some pretty long lines because of variables names and the STL. I don't think I'm breaking 120 characters but I'm just curious, what's the proper etiquette here?
    Yeah, like anduril462, I am not aware of any such convention in this forum community. Personally, I have a soft limit of 100 and a hard limit of 120 ("soft limit" as in "avoid breaking it unless you have to go to extremes to avoid breaking it"; "hard limit" as in "thou shall not break it no matter what") for C and C++. 80 is my soft limit for Python because of the PEP-8 recommendation.

    Quote Originally Posted by MutantJohn
    I will say one thing in favor of super long lines, it makes commenting them out with // much, much easier.
    Some programming text editors have a shortcut whereby you can select the code that you want to comment out and it will be done at once. Also, in C or C++, you can always write:
    Code:
    #if 0
    // ...
    #endif
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Is there such a thing as etiquette in the computer world? I know not of one. But as far as code styles go, anything goes. It's a preference, like much else, unless restricted by coding guidelines in projects or companies. Some prefer long lines. Some prefer short. So I wouldn't say there "is" any etiquette. But if you change it to what is your opinion on code length and what do you find an acceptable maximum length, the question becomes different. I certainly don't know an answer to it myself since I follow no maximum length other than that it shall not extend beyond the length of my screen which is 1920x1080.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by laserlight View Post
    Some programming text editors have a shortcut whereby you can select the code that you want to comment out and it will be done at once.
    In some IDEs, when uncommenting, it would destroy my already destroyed indentation. ;p

    Quote Originally Posted by laserlight View Post
    Also, in C or C++, you can always write:
    Code:
    #if 0
    // ...
    #endif
    Today, I wanted to do that and remembered that our Prof. in 1st semester has told us about it and that it can be really useful. At that time, I didn't really understand #endif, etc. and I thought that I wouldn't need it.. It's funny I saw that post of yours today, which was the day that I was thinking about it. Telepathy!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,395
    O_o

    For the original example posted, I'd break the parameters--declarations--the same as I'd break any other variable declaration.

    As far as it goes, I consider 80 characters a weak limit, 96 a stronger limit, and 112 a hard limit... except when a break would make things worse.

    *shrug*

    The point is, the context of code matters.

    You shouldn't go breaking lines just because the line hits 121 characters instead of 120.

    On the other hand, you should break a line with 119 characters which has a logical break that actually contributes to maintenance.

    Code similar to as posted turns up a lot in template programming, even more so with C++98. You can't do much for parameters, return type, and other items at namespace scope, but you could, for example, break long `typedef' of a template referencing nested types into multiple lines logically by grouping instead of some arbitrary character.

    Code:
    /* ... */ DoSomething
    (
        /* ... */
    )
    {
        /* ... */
        typedef Template1<Parameter1, Parameter2> Dictionary; 
        typedef Template2<Dictionary> Implementation;
        /* ... */
    }
    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,275
    Alright, thanks guys! I'm really excited about this software I've been writing.

    I'm sure you've all heard my talk about it forever but I finally did it. I coded a full Delaunay triangulation which is a self-repairing tetrahedral mesh. The quality of my mesh is probably weak but nevertheless, I've got something that works and works well so I'm getting ready to put it up on github and I'm doing the commenting now. I'm just trying to make sure that I'm not committing any social faux pas but I'll probably still ask you guys to look over it for syntax and appearance and such. The math can be kind of tedious if you're not into it.

    I'm happy to know that line length isn't much of an issue

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,395
    I'm just trying to make sure that I'm not committing any social faux pas
    O_o

    Who cares if you are?

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,618
    Line length, in itself, is not an issue.

    Readability of code, and ability of mere mortals to understand it, is. If you're going to distribute code that the recipients can't reasonably understand, there is no point in distributing it. [An exception might be distributing code so the program can be built on a new target but, even then, it is often necessary for a human to understand key parts of the code in order to retarget it for a new host platform].

    That can be affected by line length since people have trouble scanning a line of more than a hundred characters to so, so going much over 100-120 line length is inadvisable. But it is still possible to stick to such limits and produce incomprehensible code by writing statements with multiple side effects, poorly named variables or functions, functions that don't have a clearly defined purpose, unnecessary using statics, writing comments that give a misleading impression of what the code does.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Quote Originally Posted by Elysia View Post
    Is there such a thing as etiquette in the computer world? I know not of one.
    Yes, but communicating on the Internet is a lot like communicating in other situations in terms of being able to show respect, maintain dignity for everyone, and offer forgiveness of other people's mistakes. As you may know, it can also require a degree of cultural awareness. You are a part of the programming community, but there are many other communities you could need to acclimate to before you start communicating. That's the "lurk before you leap" principle of netiquette.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by whiteflags View Post
    Yes, but communicating on the Internet is a lot like communicating in other situations in terms of being able to show respect, maintain dignity for everyone, and offer forgiveness of other people's mistakes. As you may know, it can also require a degree of cultural awareness. You are a part of the programming community, but there are many other communities you could need to acclimate to before you start communicating. That's the "lurk before you leap" principle of netiquette.
    Well, that's net etiquette, not programming etiquette in my view, but I agree. Absolutely vital.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,275
    Okay, in case anyone is interested, here's my project : https://github.com/LeonineKing1199/Regulus

    You can give me feedback on readability/commenting/overall appearance, I'm very welcoming of criticism at this stage before I hit up LinkedIn with it as well.

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    When publishing code, it's usually a good idea to add a copyright notice to all files (even though this is not legally required), as well as licensing terms (in your case it must be GPL or more permissive since you are using a GPL library).

  14. #14
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    Uses spaces instead of tabs.

    gg

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by Codeplug View Post
    Uses spaces instead of tabs.
    Nonsense You've clearly never heard that spaces are Evil™ and that tabs are Heaven™.
    MutantJohn: You've also seemed to forgot that references and pointers bind to the type, so clearly it should be int* p and int& r and not int *p and int &r! And when using auto, it should not be auto &a, it should be auto & a!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets - how to get a line length
    By baxy in forum C Programming
    Replies: 3
    Last Post: 04-21-2013, 05:30 PM
  2. how to get the length of the string in one line
    By transgalactic2 in forum C Programming
    Replies: 15
    Last Post: 03-24-2009, 05:00 AM
  3. Length of line..in polygon
    By zap_hax in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2006, 08:49 AM
  4. Opinion on line length
    By tretton in forum C Programming
    Replies: 6
    Last Post: 01-09-2006, 03:54 PM
  5. Replies: 1
    Last Post: 02-05-2003, 09:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21