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 22 '22

Our codebase has next to zero comments.

This is not a thing to be proud of.

The way I see it, comments should always exist.

Comments document what you think the code ought to be doing. Code documents what it actually does. Noticing a mismatch between those two things solves so many issues quickly and easily.

7

u/illkeepcomingback9 Mar 23 '22

The way I see it, comments are a code smell. 99 times out of 100 if the code seems like it needs comments, what it really needs is to be refactored.

-5

u/vi_sucks Mar 23 '22

But ALL code needs comments.

Even if it's readable, it still needs a comment.

5

u/illkeepcomingback9 Mar 23 '22

It absolutely does not. If its already readable and clear by itself comments are just noise.

1

u/vi_sucks Mar 23 '22

No, because "readable and clear" is not the same as "exhaustively explains the logic and the thought process behind the logic.

Code that actually does that latter thing is generally horribly verbose and irritating to read. But can usually be shortened into something easy to read.

I guess the best to think about it is like the difference between a chapter title in a table of contents and an introductory paragraph. Variable and method names are chapter titles. Short but descriptive. Comments are introductory paragraphs. As long as they need to be to fully explain.

4

u/illkeepcomingback9 Mar 23 '22

No, because "readable and clear" is not the same as "exhaustively explains the logic and the thought process behind the logic.

It is, because if it doesn't explain itself, then it isn't readable and clear. If you're writing a comment, you should be refactoring your code instead because it means you've written unreadable code that needs to be explained by comments.

1

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

Nope.

Code can be clear and readable without providing a single lick of illumination into the mind of the person who wrote the code.

Here's an example:

getUserDataFromCache(Userid) { return cache.get(Userid) }

Simple, easy, clear. The code is highly readable. But it provides zero insight.

Now imagine if there was a comment above the method saying "This method returns user data from the global cache implemented to provide availability to users if network interruption occurs."

This is more information. It's useful information.

3

u/illkeepcomingback9 Mar 23 '22

If I need illumination into the mind of the person who wrote the code I'll ask them out on a date. I just need to know what the code does.

2

u/vi_sucks Mar 23 '22

Oh, so when the system goes down because someone replaced the local global cache with a pseudo cache hosted on a cloud or something, you don't think it would be useful to know that the original intention was for the cache to be useful during a network interruption?

I just need to know what the code does.

Knowing what code does is less than a fraction of the difficulty of being a programming. Everyone can google some syntax to figure out what the code is doing. Understanding how it was built, why it was built that way, and how it came to be the way it is now is WAY more important. And that's substantially more difficult without comments. Even bad comments are better than no comments.

Hell I had a bug last week where there was a comment that just said "TODO ask senior engineer X". Shit comment, obviously. But since the bug had been in the system for 3 freaking years, it was way more helpful to be able to ask him directly "yo, is this supposed to do this?" And get an answer in minutes instead of starting a 3 day email volley with the users to figure out if the code that clearly did what the variable names said it did was actually supposed to be doing that when the users said it shouldn't.

2

u/illkeepcomingback9 Mar 23 '22

The naming should describe the intent. Look at the code to determine if it is fulfilling that intent. If your code is decomposed into functional units this is not a monumental task that takes 3 days of emailing back and forth. But honestly it makes sense now, if you're accustomed to an environment where something like that could even be merged in to main in the first place, I can see why you are so reliant on excessive comments. Comments are a workaround for a deficiency in your coding standards and processes. If something like that made it past PR in my environment we'd be having email chains of an entirely different nature.

→ More replies (0)

6

u/imnos Mar 22 '22

This is not a thing to be proud of.

Well, that's where I disagree. Comments lie over time. I use mostly Ruby, and self-documenting code by writing readable code is considered a best practice.

Ruby was created with readability in mind, though. Other languages - not so much.

Here's another general discussion on it - https://www.reddit.com/r/rails/comments/25kstx/commenting_my_code_best_practices/

-4

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

Comments don't lie.

What they do is capture the mind of the developer at a given point in time. Even if that intention is different from the current intended state of the code, that's still incredibly valuable information. First because it's not always true that the current intention is right; but also because when debugging, being able to understand WHY the dev was doing the things he was doing is just as important as figuring out what he did. Since what he did was probably wrong, so you need to backtrack to find out what he should have done.

8

u/illkeepcomingback9 Mar 23 '22

Comments lie a lot, that is the problem. You write code and a comment explaining it (because you wrote so poorly it needs to be explained), great. 2 years from now someone comes along and updates the code but forgets to update the comment. Now you have comments that lie.

Good names are all you need to understand developer intention.

-2

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

2 years from now someone comes along and updates the code but forgets to update the comment. Now you have comments that lie.

No,the comment still accurately captures what the dev who wrote the comment intended the code to do. That it no longer matches what the dev who wrote the code actually did is not a lie. It is infact highly valuable information when trying to investigate a bug.

I have never run into a situation where having a comment made things harder to debug. Ever. I have run into plenty of situations where users report a bug, I look at the code and it seems like does one thing but there is a comment saying it should do the complete opposite of what the user requested. Now i know where to start the investigation and I've saved so many hours of headscratching.

What i mean is, there is a big difference between "the code used to do this, but then we were supposed to change it" and "the code was never supposed to do this and it just started happening." Triage and diagnosis for both conditions is radically different.

4

u/illkeepcomingback9 Mar 23 '22

The names of the methods and variables already describe what the code is intended to do. You don't need comments to explain code that is well written. You just don't.

8

u/too_much_to_do Mar 22 '22

I can count on a single hand how often code has changed and the next developer amended the comments. comments are mostly garbage. self-documenting code is the way to go.

5

u/imnos Mar 22 '22

If you really want access to the mind of the developer at that moment in time - you have an even better resource which never changes - the git log. Writing detailed commits is also a crucial part of self documenting code.

And of course comments can lie. If a piece of code needs to be updated - the dev changing it now has two things they need to update. Guess which one isn't going to be updated because it doesn't break a unit test if they leave it as it is?

0

u/vi_sucks Mar 23 '22

If you really want access to the mind of the developer at that moment in time - you have an even better resource which never changes - the git log.

Until the code base gets ported. Or the commits get squashed during a merge. Or the commit is cherrypicked.

And that's not even counting how much more difficult it is to read theough 2, 3 or even 10 years worth of git commit logs to try to find the one commit that made the exact change that you care about.

And of course comments can lie.

No, they don't. What people mean by this is that comments don't accurately capture what the code is actually doing. My point is that you should never expect a comment to accurately describe what the code is doing. It's not a lie unless you make an incorrect assumption.

1

u/Metalor Mar 23 '22

'My point is that you should never expect a comment to accurately describe what the code is doing. It's not a lie unless you make an incorrect assumption.'

So what the hell is the point of comments? You've just gone and contradicted yourself entirely.

→ More replies (3)

1

u/RiceKrispyPooHead Mar 22 '22

Comments document what you think the code ought to be doing.

I feel like this can be accomplished with good function/variable names along with unit tests, for the most part. With you test you describe what you think the code does, and you get the added benefit of it alerting you if you add any changes that breaks what you think it does.

If I rely on a bunch of comments every to explain what should be happening I risk knowingly changing how the code behaves, while forgetting to update the comments. Or even worst I unexpectedly change how the code behaves and I don't know that I need to update the comments.

0

u/vi_sucks Mar 23 '22

You need all three, is the point.

  1. Comments describe what you think the code should be doing and why it should be doing that.

  2. Good function names help with readability.

  3. Good unit tests help describe how the code should behave under a specific set of conditions.

The thing is that #1 and #3 are not actually the same.

Here's an example. Let's say I have a method that some dude wrote 10 years ago to calculate when a bad credit notice falls off your credit. A nice readable variable name like getBadCreditExpirationDate is good. And a unit test that puts in a date and verifies that it expires on a different date is also good.

But neither of those will tell me as much information as a comment saying "This method adds 3 years to the date that the bad credit notice was recieved in the file to calculate the date the credit should expire. If it would expire on an invalid day like Feb 29 of a non-leap year, expire on the next valid day."

And sure, if i look at the code 5 years from now and it is actually adding 4 years, that probably means someone forgot to update the comment. But it's still helpful to know that because I can see (a) someone made a change to it between when the comment was written and when I'm looking at it now. (b) i need to figure out whether that change was correct before i do anything else. (c) at some point in time it was three years. Maybe there's some code out there that's still referencing the three years that whoever updated the method just didnt remember to clean up. Might be good to check.

2

u/RiceKrispyPooHead Mar 23 '22

But neither of those will tell me as much information as a comment saying "This method adds 3 years to the date that the bad credit notice was received in the file to calculate the date the credit should expire. If it would expire on an invalid day like Feb 29 of a non-leap year, expire on the next valid day."

All of those would make perfect unit test cases, in my opion.

Also, I didn't say don't use comments at all. Nor did the original person you were replying to say that. I think comments should be used very sparingly, for the most part.

0

u/vi_sucks Mar 23 '22

Also, I didn't say don't use comments at all.

I think my point is mostly that people are lazy and will, if given the opportunity, skimp on comments. I'd rather have more comments to ignore than be missing an important comment when it would be helpful.

Which means developing a consistent habit of putting in comments rather than only bothering to add one when you think the code really badly needs explanation. Because, given cognitive biases, you're going to think that code needs explanation far less often than is the case for people who don't inhabit your brain.

3

u/RiceKrispyPooHead Mar 23 '22

I completely disagree.

1

u/Macaframa Mar 23 '22

well-defined and well-documented patterns are 100x more useful than some asshole's shitty comment. I can use a pattern to follow to get something built, it helps me sniff out bad code and it helps me in reviewing code. Comments are for when something is a work around and cannot be explained any other way.