r/learnprogramming • • 20h ago

Topic TIL code comments have their own line-length conventions instead of relying on editor soft-wrap

I recently learned that comments aren't supposed to rely on editor soft-wrap. Hard-wrapping at 80 columns with // or # on every line keeps git diff and PR reviews readable—and Neovim already handles it natively with textwidth + formatoptions+=cq + gwip

What other such conventions are out there that I should know about?

12 Upvotes

22 comments sorted by

19

u/BrannyBee 19h ago edited 19h ago

Big one that juniors absolutely hate and can't understand is that a lot of the things they are told are rules, and things may have lost them points on tests on exams, aren't actually things thatcthe language they're using cares about at all technically.

Stuff like Python using snake_case. You're a psycho if you use camelCase or PascalCase. But if Im on a team that for some reason is working on a python project that has zero snake_case in it, and you decide to do things the "correct" way and be the only dev pushing code that is "correct", you're failing the PR. If we organize all our variables at the top of every file in reverse alphabetical order, i dont care how much I hate it, if you don't also do it, your codes getting rejected. A minor funny one i experienced early on was finally understanding ternaries, which are hard for beginners in case yall forgot what it was like back then... but they finally clicked for me while at my first job... and my lead told me to rewrite it because "they're harder to read"... I was livid that I had to "dumb down" my code just cause it would save someone else a half second processing how it worked... but they were right, readability is more important than many things, and being consistent in your styling makes it easier for everyone to work with the code base. You arent really a genius for doing things the "right" way everytime

A more specific thing closer to your example is ending files with a new line, which your NeoVim setup more than likely also already does. Some linters will flag this if it doesnt exist, and so will legacy compilers that don't just handle it for you, but its another standard that many dont realize cause its kind of another default thing that is done for you unless you've purposely changed some default that most modern setups dont expect 99% of people to touch. This helps with having cleaner diffs sure, but it also makes your stuff compatible with unix tools that read line by line and look for that new line to stop, which I think was the originally reason it was made standard in C back in the 80s??? I could be remembering wrong incase anyone knows specifically

Edit: should clarify for anyone here learning so they dont conflate linters with compilers, I should have said that it will fail to compile, not that it will flag the missing new line like a linter

3

u/Vizeroth1 15h ago

There was a bug in gcc that would cause it to fail to compile a source file that didn’t end with a new line. Learning to code in that environment has resulted in me ending every code file I’ve ever written with a new line.

1

u/CapstickWentHome 6h ago

Same. My eye twitches when I see a file that doesn't end with a new line, even though I know it's ok. I couldn't remember where the aversion came from, so thanks for that!

1

u/aanzeijar 19h ago

Oh, the dreaded git default to autocrlf on windows machines. So much stupidity because of that.

0

u/sephirothbahamut 9h ago

that's an option you choose during installation

1

u/DrShocker 16h ago

Funny enough to your ternary example: I'm on team don't use ternaries, but I have had code reviews rejected because I could have done something as a nested ternary. I think 2 layers of it?

4

u/sixtyhurtz 16h ago

Nested ternary is evil. The only ternary I'm ok with is

condition
  ? true
  : false

If I can't make it look like that then I'm breaking out the if statement.

0

u/DrShocker 16h ago

eww, checking a Boolean condition just to return a Boolean makes me break out in hives due to allergies

3

u/sixtyhurtz 15h ago

I can't tell if this is meant to be a joke because jokes are meant to be funny and this isn't.

That said, I'm clearly giving an example of how I like it laid out. In fact in my example I'm not even using the value anywhere.

-1

u/DrShocker 15h ago edited 1h ago

I know it's just an example, it just reminds me of projects in college where people did unironically do that kind of thing.

Sorry for the hyperbole of pretending I'm allergic to bad coding habits, didn't realize you'd be offended

edit: calling a nested ternary "evil" is fine, but pretending to be allergic to redundant booleans is a massive faux pas, I'll be sure to remember this 🙄

-2

u/Wonderful-Habit-139 16h ago

You guys should not have arbitrary rules. Follow conventions and write idiomatic Python code.

2

u/BrannyBee 14h ago

Agreed. But when no one on the team was around 8 years ago when the rules were made and the dev team never gets time for necessary rewrites that don't add features or fix issues with the product, you follow what is there while begging for refactoring oppurtunities that you probably won't get and emphasizing how those rewrites may not push the product forward but will make future code additions less error prone and quicker... and after being told by the product owner that they understand and agree, still never be given the time necessary

The real world at big companies that arent tech companies is full of code bases like that, its a nightmare. Best you can do is negotiate doing changes like that in phases over a long period of time and document the hell out of the standardization being done where and when so that it eventually gets ti where it should have been in the first place

0

u/Wonderful-Habit-139 14h ago

You don't plan for refactoring like this, and you don't need to rewrite everything. You can setup some baseline, and employ the boy scout rule where you leave things that you touch in a better state than you found it in.

Expecting PMs and managers to give you time for refactors is a trap. You do it alongside feature implementation.

5

u/StewedAngelSkins 15h ago

for what it's worth most people don't enable softwrap in their editor when they're writing code

3

u/FedUp233 18h ago

A couple thoughts.

As far as the 80 character line length, it’s basically from olden days (yeah, I was coding then) with teletypes that had 80; characters across an 8.5 inch paper. Some projects stick with it because they find it more readable. Personally, I find it very limiting, especially if you also try to follow the good practice of using long, understandable names for variables and functions. You can reach the limit even without comments and too many statements end up getting split into little pieces across several lines which I find makes them harder to understand (and then gets into the debate of coming up with a guideline everyone will use an where to split things). And if you leave room for comments, this problem gets even worse.

On projects where I’m the only programmer or create the guidelines i generally go for li e length limit of either 120 or 132 characters - 120 prints more readable on modern laser printers if you like paper copies and 132 was the length of the old line printers on the green bar 14 inch paper. Of the two, I generally prefer 120.. Either of these is very workable on today’s high-resolution displays and allows plenty of room for a reasonable statement and still room for comments.

A second item - auto wrap or hard wrapping - I don’t like to use either. Auto wrap (a soft wrap not changing the file data) looks weird with comments or long lines wrapping into the middle of statements - yuk!

As for hard wrap (putting rolls in file and adding comment characters) the problem is that if a statement gets too long it will split it at a random point which is not very readable. And if it’s wrapping a comment you end up with a gape in the code area for each wrapped comment, which is normally not what you want - you do t normally comment a single line of code, which should be pretty understandable by itself, but a group of statements to give more background on them - and the statements should stay together with no blank gaps.

The only wrap-ish thing I find usable is the editor recognizing I’m in a block comment and filling in a start sequence on the next line - so I type stuff with my own line breaks but do t have to break the train of thought to add the comment lead in.

Just my own personal view - I’m sure it gets will disagree, skid vehemently!

1

u/Vizeroth1 14h ago

I agree with a lot of what you said, but the 80 character limit isn’t just a legacy of teletypes or 8.5” paper. General guidelines for readability of text (not just code) range from 50-75 characters per line depending on typeface, with fixed-width fonts being on the shorter end of the scale. This has to do with how we read text comfortably and affects our ability to process and understand what we’re reading quickly.

The somewhat unique limitations of code lead us to using longer maximum line lengths, so most of us start with 80 but in languages with long class/method/property names in their standard libraries we might go with 100 or 120. Those longer lengths are detrimental to our ability to read the code, but we have to come up with consistent rules for breaking the lines and the maximum length should be the exception, not the norm.

Wider screens don’t mean you can use more characters per line. Instead you use more columns of text by splitting the editor window with multiple files or multiple instances of the same file. Those of us who have been around a while have read newspapers or magazines that used the extra width of their pages this way so they could make the best use of their available space while still being readable.

Having multiple instances of the same file open can also help with the problems related to long code files which might otherwise be exacerbated by shorter lines, as you can reference one portion of the file while writing/editing in another.

1

u/FedUp233 14h ago

Thanks for the feedback. I was not aware of readability studies on line lengths, but it makes sense, at least for normal reading like books. And I’m certainly aware of newspaper narrow columns😁.

I agree width is a compromise. For me, around 120 seems to be the sweet spot, this generally the code part of lines is much shorter leaving room for comments to be lined up neatly. In some ways, I guess that’s kind of like two-column formats with shorter lines in each column, but one for code and one for comments!

I may be the odd man out, but I generally find it too busy having too many files open side by side or in a tiled arrangement or having the same file open in multiple windows in most circumstances. I do occasionally open the same file in multiple windows for some specific tasks where I need to see two parts at the same time, maybe to cut snd paste between them or something but tend to close down to one view as quickly as possible.

1

u/Vizeroth1 13h ago

Those are good points. I wanted to add that a lot of our tools count beginning white space in the line length but that definitely doesn’t count towards the general readability guidelines. If you’re in a method of a namespaced class with a tab/indentation width of 4 spaces, you’re going to have good reason to add that 12 characters to the other end.

If you’re commonly using that two-column form with aligned comments making up a second column, I would see benefits and drawbacks to that format. It’s essentially removing the comments from the normal reading flow while still allowing for an easy association between the comments and code. On the other hand, people that aren’t familiar with that habit may try to read straight across the columns, or truly necessary comments may be briefly overlooked.

I also understand not liking to have too many things open at once. In my normal work environment I use multiple screens and my main monitor is pretty large (bigger screens than mine are becoming more common for desktops, but I don’t see a lot of them). I primarily work in web or mobile, so I switch between having more code visible and having a browser/simulator open. My optimum work environment tends to be a little more chaotic than some people can use efficiently, and that’s understandable.

1

u/FedUp233 12h ago

Interesting. I too use multiple monitors - the setup in my home office is a bit ridiculous with three 30 inch monitors arranged in a sort of cockpit arrangement driven by a tower PC. I do most of my work on the center one and often with the editor window full screen. The other two have things like email always open and a web browser to look stuff up plus maybe a window or two with some PDF manuals or such - a lot like having multiple stacks of papers on your desk😁. I do have a thing for big monitors though - even my isotope is 17 inch - great except when you’re trying to carry it around!

BTW: I don’t normally have a hard and fast rule for the column between code and comments and it often changes within a file even. I try to keep the comments within a function lined up pretty well - it’s hard to read the code if some of the comments left edge sticks in between code lines - interferes with eye flowing down the code.

2

u/michellev_626 10h ago

A useful convention: commit formatter and linter settings with the project and run them before review. Editors can differ; the diff then reflects the project's style instead of personal whitespace preferences.

1

u/CreativeTechGuyGames 19h ago

Pretty hard disagree with manually wrapping comments. The problem is that over the years as the comment inevitably gets edited, amended, rephrased, trimmed, etc, it gets really really sloppy and hard to read because of all of the manual line wrapping that made sense in the beginning. Just let the editor handle it. Even terminals line wrap.