r/learnprogramming • u/BullCityPicker • 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
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
xtells me nothing about what it's there for, and likewise with a function calledget. What are you getting?getAllUserstells me exactly what that function should be doing. If it needs to be something longer likegetAllExpiredOrganisations- 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