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

13

u/vi_sucks Mar 22 '22

I think even when it's semi-obvious,it should be explained.

Because what is obvious to you now may not be obvious to some new guy 3 years from now.

2

u/Traches Mar 22 '22

Sorry but I have to disagree. That's what commit messages, careful naming, and separation of concerns are for. Comments don't get tested, which means there's nothing to prevent them from going out of date. Bad information is worse than no information.

By "not obvious", I'm talking about weird situations or subtle problems with the first, cleanest solution that comes to mind. Things you want to know about before you waste a few hours trying to refactor only to rediscover the same problems.

11

u/vi_sucks Mar 22 '22

Cool so what happens when the code base gets ported from SVN to Git and the commit history disappears?

And trust me, that super neat naming you think is so obvious? Some dude who isn't you years and years removed will not agree.

I had a senior dev with this same attitude and it always led to method and object names that stretched halfway across screen like workerToRunPricingEngineBeforeSavingToDatabase.callPricerOneTimeAndThenBackupDataToCachePriorToSavingToDatabase()

Just straight eyebleach. And then someone decides to save to the Cache and to print to a log, so now you either need to change the method signature in every single line where it's called, or leave to become less and less accurate with every change.

5

u/sus-is-sus Mar 23 '22

your example is just a method that does too much. it should be broken down to methods that only do one thing. runPricing, Cache, Save, Log.

1

u/vi_sucks Mar 23 '22

Ok, so when you need to run this same logic in 50 different places innthe application, you'll just copy and paste those four method calls everywhere?

No. You will create a wrapper method. And then you need to name the wrapper method. If you name it something that describes exactly what it does, because you don't have a comment to explain a shorthand, you end up with the above.

1

u/sus-is-sus Mar 23 '22

i have seen wrapper happy code. it is hot garbage.

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.

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.

2

u/psioniclizard Mar 23 '22

Not sure why this was voted down. Developers are notorious bad ad updating comments. Its one reason wjy you don't comment everything. Also another goof reason for comments are when a customer requests something specific/odd. I would day commit messages are not full proof (a lot of devs also wrote bad commit messages) and certain languages do test comments (rust for example) but and out of date comment is often worse than no comment.

Code should be reasonable self documenting, this comes from good structure and naming. Both of which are way more important than comments.