r/learnprogramming • u/adper07 • 16h 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
18
u/BrannyBee 16h ago edited 16h 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
4
u/StewedAngelSkins 11h ago
for what it's worth most people don't enable softwrap in their editor when they're writing code
2
u/FedUp233 15h 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!
2
u/michellev_626 7h 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 16h 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.