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

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.

1

u/psioniclizard Mar 23 '22

I never said go through the git history line by line, you should be able to tell what what does from the source code, commit messages are useful to explain why something was done, when and by who but I wouldn't rely on them as primary source of documentation. I'd much either use an IDE that allows me to jump to references, definitions etc to see what the source says.

It this example that might be the case, or they never implemented properly, or they were completely wrong and added to comment that is just flat out wrong The real way is to go back to the original spec (if available) and read it or find out from the business what they expect it to do.

Either way, personally I don't like methods/functions with and in the name, it normally means too much is going on. It's nice to have wrapper, but if multiple places are calling the same bit their needs might change, if only one place is calling it you don't need a wrapper.

I'm not opposed to comments and a good comment is worth it's weight in gold (for example in my job it might be a weird requirement from a customer), but a lot of people see comments as a bandaid for badly structured code. Tests generally are much better at telling you what should happen. Even in that example I still think it is too tightly coupled together, that method alone has at least 3 side effects and make future growth likely to be a headache.

1

u/vi_sucks Mar 23 '22

Mainly what I'm trying to push back here is against people who think that as long as the code clearly documents what it does, you don't need comments.

And that's just not true. Worse, it's actively dangerous as a practice to teach new devs and learners.

Code can document what it does really, really well. Tests can document expected behavior. But you still might need a comment explaining why it does that, or what the dev was thinking when they did it. Even if you think it's obvious now, you (or whoever replaces you in a year) probably won't think is so obvious later.

And I've just been bitten by too many "senior devs" who refuse to write comments and whose idea of self-documenting code is long-ass variable names.

1

u/psioniclizard Mar 23 '22

No, you should teach new devs to write unit tests :p! But I get what you are saying, it is wrong to make new devs feel like they should never write comments but they should learn when to write one and when one they don't need one. I agree 100% you need to learn to read your code from a third party point of view, if something can bot be documented via a good a good name or test it needs a comment. You should be able to look back at code 3 weeks later and understand it!

Also I agree on the long-ass names, a lot of people think that is enough and it becomes a bandaid just like I said comments can be. Senior devs can be weird! Also reading your other comments it seems you have to deal with some really old code and I can understand with legacy code you need every tool in your belt to make it make sense often!

Sorry if I came across as argumentative, I honestly think it's a good discussion and one that is good for new devs to here because it can help them understand the issues people face when codebases grow.