r/learnprogramming • • Mar 22 '22

Don't be that guy.

I just took over a project for a guy who left. Don't be this guy:

  • ZERO comments in your codebase.

  • One and two character variable names with no discernable pattern.

  • Comment characters used to comment out big chunks of code for no discernable reason.

  • No error checking.

  • Reliance on data files that you forgot to include or explain, going in to meaningless data frame names.

  • Making outrageous claims about how much your code improves things with zero baseline performance.

People like this think you're an idiot because you can't make their code work, and get to be all smug. We will track you down and find you. You will need references someday....oh my.

2.6k Upvotes

427 comments sorted by

View all comments

26

u/OddBet475 Mar 22 '22

Comments are an absolute last resort. Don't be the opposite and comment the death out of your code. It should be understood without.

There's a place for comments (maybe) but that should be extremely rare. Use meaningful naming, the KISS and do not repeat yourself principles and we can all live without comments. I work in a 25 odd year old code base and comments hurt more then they help, they can't be trusted and may be misleading. People don't fix comments when changing code.

0

u/[deleted] Mar 22 '22

[deleted]

0

u/psioniclizard Mar 23 '22

It really depends on what comments. Also are you writing a system? A library? A single function? Self documenting code often fails when the fundermental structure of the codebase is just plain wrong. The reason people say comments are not the be all and end all is because developers are notoriously bad at writing bad comments/not updating them and using bad comments as a crutch not to write self documenting code.

Self documenting code os definitely not a myth, but certain languages are better at it than others. Also you probably need good domain modelling (which is a vital part of Self documenting code).

4

u/__Mauroo__ Mar 22 '22

I'm new to programming so i have a question. Isn't writing a comment before ever function/part of code to explain what it does a good idea if you know someone else will have to look at your code? I think it might take some time, especially if you have already tested that function/part of code and know it works

5

u/[deleted] Mar 22 '22

Write comments whether you want to justify certain “weird” choices or if you want to explain a particular complex business logic procedure.

4

u/g4rthv4d3r Mar 22 '22

The idea is that the name of a function should be descriptive enough so readers know what it does without having to read its comment.

Block comments to explain what a paragraph of code does is an indication that this paragraph should be moved into a private function.

In general, comments should not be used to describe what the code does, they should be reserved to explain why you're doing something a specific way vs another, which means explaining your design decisions.

Additional benefit is that extracting functional blocks into functions allows you to unit test then separately.

7

u/wjsoul Mar 22 '22

I think what most people in this thread have already said, often times, its not beneficial to over comment. If you name your functions/methods and variables in a way such that it explains itself, you'll find that most of the time, you don't really need comments.

An example would be something like this:

wait_for_it_to_rain()
cover_yourself_in_oil()
fly()

Every one of these functions describes what they're supposed to do, you do not need any comments to explain them.

2

u/Buckets1337 Mar 22 '22

Upvoting for the evocative method names. Though I wouldn't have to read the code in these methods to understand what they are doing...I really want to!

3

u/vi_sucks Mar 22 '22

Yes. Write comments. Please, for the love of all that is holy, write comments.

4

u/vi_sucks Mar 22 '22

Ugh, this is a bad philosophy.

I've worked in a code base from the goddamn 70s and let me tell you, seeing comments in that shit is a godsend.

People swear up an down "code should be self-documenting" "revision history will tell you what you need to know".

Sure, and then you port the codebase from SVN to Git and lose everything older than the last two years. And now you dont know of that line of code that says

Boolean billingUnpaid = verifyBillPayStatus()

Is a typo or valid code. Was the dev intending to return true when billing is unpaid? Was he intending to return true when billing is paid and simply wrote the wrong variable name? You don't know! Nobody knows! Until the users test it and fail it because it doesn't "work like it used to"

11

u/Inderpreet1147 Mar 22 '22

If people don't update comments while updating code, that's on them and the reviewer honestly

1

u/OddBet475 Mar 22 '22

Not really man, you are thinking small where you have a realm of control. It can be so easy, have you ever seen a massive corporate code base?

5

u/Inderpreet1147 Mar 22 '22

I have, but imo I've found modules with some partially accurate comments are far easier to grasp than module with no comments.

2

u/OddBet475 Mar 22 '22

That's a good company then, we have many million lines of code and some comments you come across are 20 years old written by folk that English was not their first language. Debugging through 50+ service programs and such to find a bug and fix it you don't note the comments down on your travels that need correction (you don't even read them tbh).

1

u/psioniclizard Mar 23 '22

If you have a good review process, half the issues people have brought up here in favour of commenting everything would not he issues. Which is what you should be moving towards anyway.

I don't think anyone is saying don't comment code, they are say it is not a perfect solution and there are often better ones.

Put it this way, everyone who is saying "it should be commented" are assuming the comments will actually be good. That is not really any different than saying "it should of been written in a better, more understandable way originally" both of them rely on the idea that something in the past should if been different. But you can't change the past.

2

u/OddBet475 Mar 22 '22

I'll add while attempting best to avoid I also comment out code at times. Some things are so complex no amount of unit testing will confirm the change is correct if messing with epic complex code written many years ago even with the best of UAT.

Not everything lives in a clean version controlled repo.