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/psioniclizard Mar 23 '22

Those are not super neat naming and honestly sound like methods that are doing to much. Honestly not to be mean but it sounds like the structure of the codebase is at fault.

You should not have methods that do so much, instead the caller should be chaining these together. I'm pretty sure that is the fundermental issue and I don't see how comments will fix that.

Also how often do you move codebase from SVN to git? That would really be an edgecase and I'm guessing dealing with legacy software, which needs to be handled specially anyway.

2

u/vi_sucks Mar 23 '22

Ok. So let's say you now have:

worker.callPricer()

worker.backupData()

worker.saveToDatabase()

Cool, now you need to do these three lines in multiple places since it's an often repeated action that all needs to happen together.

Now you've created a bug waiting to happen when someone in one of those places decides to add a statement to print to the log but forgets to update a couple of the 67 other places where this 3 line code call is happening.

That wouldn't be a problem if you wrapped the three method calls in a single method. But then what do you name that method? You want it to accurate describe what it does, right?

Also how often do you move codebase from SVN to git?

I would generally expect a repository migration once a decade or so. What, you think Git is perfect and will never need replacement?

The thing is, the migration itself isn't an issue. The fact that it breaks commit continuity is an ongoing problem thaf means you will continually ever after that run into code where the commit that could have explained why it was written is now lost in the sands of time. It's not like you move from SVN to Git, deal with it once, and then you're good again. Once you do it, this issue of missing historical data will come up over and over and over again.

1

u/psioniclizard Mar 23 '22

You first example - yes, that is called decoupling code. You can now write unit tests for them (which are much better documentation than comments most people agree). Why do you need to do them in multiple places? That is a complete violation of DRY. You shouldn't have then in multiple places. Sure call them in multiple places but there is no reason to write the same function/method twice. Which comes back to my point about the structure of the code being wrong.

I'd also say that you shouldn't have a method called saveToDatabase() on the worker. That needs to be decoupled. You could have all kinds of side effects and again suggests the code is structure wrong. I assume you pass a database connection in to worker or is worker but honestly work sounds like a bit of God class in this case and should be broken up.

Yes you need to add these in multiple places. So when someone else comes to read the code they can see exactly what a method/function does just reading it. Which is self documenting code...

Also for the repository change, you are future proofing for a problem which likely won't be one. Any product that wants to replace git will likely need a clean upgrade path from git to gain traction, which more more likely solves the problem.

Lastly you are assuming that any comments will actually be relevant, upto date and useful. If not you can't go back and change that. I'd argue that I'd not much different from saying the code shouldn't of been structured better in the first place.

Again I will say this, a big part of self documenting code is tests. Which are much better than comments for description what code does and tell you when code doesn't do that.

So I have to ask, what will you call this god method that does everything?

1

u/vi_sucks Mar 23 '22 edited Mar 23 '22

Sure call them in multiple places but there is no reason to write the same function/method twice.

Nobody said anything about writing the method multiple times.

Anyway, the idea is that you can create a wrapper method called

callPricerAndSave()

which will do all the subsidiary method calls. You can then explain that it calls the pricer, saves, backs up to cache and logs in a comment attached to the method. You can even explain WHY it does that in that order if you want to.

Lastly you are assuming that any comments will actually be relevant, upto date and useful.

No, I'm not assuming that comment will be up to date. Never assume that.

But imagine if you look at the callPricerAndSave method and the comment says "this method calls the pricer, saves, and then backs up to cache. This is because of a relatively mundane reason." But the code actually does callPricer, backupData, saveToDatabase.

This is useful information because the comment doesn't match the code. It should tell you that at some point the dev intended it to behave that way, and likely wrote the code that way, but now it's been changed and whoever changed it didn't update the comment.

Without the comment, you don't have this info. You might, maybe, figure it out after going through the git history line by line (oof) but probably not. Especially not if the commit got squashed because person who wrote the comment was you six months ago and you never actually committed just the change with the backup after the save. In that case, all you have is the code and a headscratcher trying to figure out why it was written the way it is.

Again I will say this, a big part of self documenting code is tests. Which are much better than comments for description what code does and tell you when code doesn't do that.

Tests are great. But again, the benefit of comments is not to tell you what the code is doing. It's to help you figure out why it's doing it that way. Either explicitly as a statement of intent by the dev, or implicitly as a record of past intentions.

→ More replies (3)