Studies on optimal code width?

前端 未结 11 650
滥情空心
滥情空心 2021-01-29 18:42

If you enable the \"View Right Margin\" in your IDE of choice, it is likely that it will default to 80 characters. I tend to change it to 120 for no reason other than it was th

相关标签:
11条回答
  • 2021-01-29 19:08

    As some people have pointed out in other answers the reason for the 80 character limit is partly historical (punch cards, small screens, printers etc) and partly biological (to track what line you are in it's generally good to be able to see the entire line without needing to turn the head).

    That said, please remember that we are still humans and we build tools to solve our own limitations. I propose you ignore the entire debate about character limitation and just write stuff that makes sense regardless of their length, and use an IDE or text editor that can help you keep track of the lines properly. Using the same argument for indentation in the tabs vs spaces debate, as well as the how wide should the indentations be I propose you use an indentation marker (most commonly the tab) and just have people configure their own IDE or text editors to display them as they find most comfortable to them.

    Sticking with a fixed number of characters per line will always make things worse for everyone but the targeted audience. That said, if you will never share the code, ever; then there's really no reason to even have this discussion to begin with. Should you want to share the code, you should probably let people decide what they want on their own instead of forcing yours (or someone elses) ideals on them.

    0 讨论(0)
  • 2021-01-29 19:09

    I normally use 120-150 unless the company describes otherwise. However it depends also on the kind of code:

    • I (almost) never use multiple statements on one line
    • I only use long lines (>12) only if lines that look similar can be aligned and not broken.
    • I always use enough spaces/parenthesis etc
    • I prefer longer variables names above shorter names

    Until a few years ago I limited to 100 but now widescreens are normally used and high resolution monitors 120 can be even seen on laptops (which I barely use).

    Comparing a screen to a book is not really good because a book has more vertical space and a screen has more horizontal space. I always try to keep a function max. one visible screen long.

    0 讨论(0)
  • 2021-01-29 19:16

    To the best of my knowledge the 80 character is used as a coding standard to maintain compatibility with command line editors (default terminal width is typically 80 characters). With modern IDEs and large screen resolutions 80 characters is probably not "optimal", but for many developers maintaining readability in the terminal is essential. For that reason it is not likely that 80 character width will be replaced as the de facto standard for code width anytime soon. And to answer your final question, yes, code width as well as any other characteristic which will affect your code's readability should be addressed in your coding standards.

    0 讨论(0)
  • 2021-01-29 19:20

    From https://en.wikipedia.org/wiki/Line_length

    I'd say the same principles for optimal line length in books are applicable to reading code.

    0 讨论(0)
  • 2021-01-29 19:24

    Actually, the 80-column thing long precedes DOS. It comes from card punches, which were 80-column devices.

    And to kind of answer the OP's question, one "study" has been going on for about 600 years now - the printed book. These have evolved over the centuries, with readbility foremost in mind, to the position we are at now where the average line length for text is around 60 characters. So for readability, go for narrower margins.

    0 讨论(0)
提交回复
热议问题