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.

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.