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.

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.

1

u/MOVai Mar 23 '22

You should not have methods that do so much, instead the caller should be chaining these together.

Splitting up functions for the sake of having short functions needlessly increases your surface area. Long functions aren't so bad - but long functions that don't document what they're doing using comments are a problem.

Shoehorning what could be a few lines of comments into function names and choice of functions seems like a terrible idea.

1

u/psioniclizard Mar 23 '22

Are we talking about functions or methods? Functions should do one thing, be pure (if possible) and you very much should composable them together, that is what piping if for (for the record I work in a functional langauge). I have never said you should shoehorn in long names. The name should reflect what the function or method does. If you need a long name it suggests your function/method is doing too much and your code structure is wrong. With functions especially you should let the caller chain them how they need.

That why you can tell what a function does just reading the source code. Well written functional code is very self documenting, add in tests and any developer who understands the language should he able to work out what is going on.

I have never said don't use comments, but I don't understand why a comment would he better than reading source code, which I properly structured will tell you exactly what is going one (also using things things go to definition etc in your IDE). I read comments left ny previous developers all the time at work and frankly a bunch of them are not helpful.

It's not splitting up functions for the sake of splitting them up, it's so i don't need to write and maintain the same bit of code twice. The point is you write code once you read it many times, so write it in a way that is easy to read.

It's a separate topic but a lot of these issues come from OOP and the way it mixes data and actions bit that is not something to discuss here.