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

Show parent comments

2

u/Traches Mar 22 '22 edited Mar 22 '22

Sounds like you're working in a different world than me, because porting from SVN to git is just not a thing I'll ever have to deal with. I'll concede that if you have a lot of technical debt, comments are the least bad way to document it. I also don't have a problem with a brief description at the top of a class, or method descriptions used to generate API docs and the like.

"Neat" sounds a lot like "clever", which is usually the opposite of obvious. Names should be simple. Also if your names are long, your namespace is too big. Long names suck.

Two hard problems in computer science...

Edit: just noticed your username. Guess we disagree on quite a few things!

1

u/vi_sucks Mar 23 '22

porting from SVN to git is just not a thing I'll ever have to deal with.

So think git to whatever fancy new code repo tool replaces git, then. It'll happen.

I'll concede that if you have a lot of technical debt, comments are the least bad way to document it.

The problem is that if you always end up with technical debt. It's like a house, no matter what, eventually you'll need to repair some plumbing. If you wait until then to start writing comments, it's too late

Also if your names are long, your namespace is too big. Long names suck.

It's not a namespace issue. The long name is self-documenting and you can tell exactlt what the code does. But it's too long to be readable. Better to give a shorter name, with the understanding that it wouldn't be solely self-documenting on it's own, and then add a comment that explains what the short name truly means.

//This worker will run the pricing engine before saving to db

priceAndSaveWorker

Some people would say that just naming it as priceAndSaveWorker is enough. Those people are wrong. Again, you and I get what we are talking about cause we've talked about and know what the full intention is. But some jr dev looking at it for the first time? How is he going to know that price and save should price, then save. Why not save then price? Or price twice and save. Or save, price, then save again. Etc. Or maybe priceAndSave means the worker is used for both pricing and saving. How does this young whippersnapper of the far flung future know unless we tell him. And how do we tell him? With good commments.

3

u/Traches Mar 23 '22 edited Mar 23 '22

Pricing and saving are two different jobs, related only by the fact that you want to do them at the same time. They each deserve their own functions, so priceAndSave's body becomes something like runPricingEngine() && saveToDb(). Now the comment is unnecessary because the code is just as obvious, except it actually gets tested and is guaranteed to be accurate.

Comments themselves aren't a code smell, but the need for them is.

Edit: I also don't think "something will replace git someday so commit messages are pointless" is a very good argument. Just because something new comes out doesn't mean you have to use it, especially if it means losing your entire commit history. Git isn't going anywhere.