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

138

u/[deleted] Mar 22 '22

We use self-documenting code. Comments where needed of course, but if we need a lot of comments, the code needs to be rewritten to be clear and concise.

All of the other bullet points, I would have no qualms about taking that developer out back and beating them with a sack of oranges (if it was past their first offense).

59

u/imnos Mar 22 '22 edited Mar 22 '22

Agree. Our codebase has next to zero comments. Tips for writing clean and readable code:-

  • Naming. Name your variables, functions, classes descriptively. A variable named x tells me nothing about what it's there for, and likewise with a function called get. What are you getting? getAllUsers tells me exactly what that function should be doing. If it needs to be something longer like getAllExpiredOrganisations - nothing wrong with that. It's still super readable.

  • Try to break down your code by responsibility, following the Single Responsibility principle (look up SOLID principles if you don't know). For example, if you have a class or module that's 10000 lines long - chances are it can be broken into multiple logical classes. Then if you need to extend this to do another similar task - you can probably reuse some of the classes you've refactored.

  • Likewise with a function that's 100 lines long. Many language style guides enforce maximum lines per function (Ruby recommends no more than 5 for example). This keeps your code nice and modular, makes it easier to read, easier to reuse, and easier to unit test each piece individually.

  • Please don't try to be clever by doing fancy one liners that make the code unclear.

Lastly, a relevant quote from Jeff Atwood's blog which I think helps to enforce the above:-

"Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live."

https://blog.codinghorror.com/coding-for-violent-psychopaths

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.

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.

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)