Besides some of the very, very obvious (don’t copy/paste 100 lines of code, make it a function! Write comments for your future self who has forgotten this codebase 3 years from now!), I’m not sure how to write clean, efficient code that follows good practices.
In other words, I’m always privating my repos because I’m not sure if I’m doing some horrible beginner inefficiency/bad practice where I should be embarrassed for having written it, let alone for letting other people see it. Aside from https://refactoring.guru, where should I be learning and what should I be learning?
Practices and practice mean two different things here. Decide which you mean.
Please clarify on how they are different. I’m really confused by this comment and want to know what I’m missing.
A practicing doctor and a practicing footballer are not the same. The doctor follows ‘the practice of medicine’, but the other goes to 5 practices a week or so. The meaning is different and so are the rules.
Alright. I thought from my post it was clear I meant “How do I write code that follows good standards? How do I learn the rules of thumb about how to write good code?” by “code that follows good practices”. I suppose you can sum that up as “the practice of writing good code”, closer to the sense of “the practice of law”, although I wasn’t originally thinking of it that way, hence my confusion. Did I make some grammatical error that made it hard to interpret? I’d like to know so I can avoid such problems in the future. If it’s worth anything, English is my native language and I am aware of those two senses of “practice” you mentioned.
The most important thing is that the codebase can grow without too much refactoring. Then you know you got the overarching design right. The rest then doesn’t really matter that much. You can always rewrite certain parts when/if needed.
A good way to do this is by making the core really solid, this is called bottom up programming: https://paulgraham.com/progbot.html
You should try to write code using SOLID principles. You should then write one using CUPID, try following OOP patterns, functional patterns and good old procedural ones. You should develope one huge code base and then try to maintain it for years. You should build a binary in Ubuntu, in alpine, in nixos, in Mac, in Windows and try to ship them to each other. You should run your code on a bare metal server and you should try writing lambdas. You should start a green field project and a try to maintain a legacy system that none of the original authors are there yet. You should write code in a company that hires 500 people per day and lays off in thousands. You should write code for a company that has 2 engineers that have been there for years. You should write a backend, a frontend , and one that does not do all these. And you should ship them. And use them. You should write codes idiomatically and you should write them idiotically.
The idea is not that a good programmer had done all these, the idea is that no simple tip or priciple can apply to every situation. Anyone claiming that, is no more than a snake’s oil salesman. You should learn to code and design and engineer your software critically based on each situation. You should constantly learn. And you should not be afraid to go against the grain or break from the beaten path or go with the most popular mediocre choice.
I’m always privating my repos because I’m not sure if I’m doing some horrible beginner inefficiency/bad practice where I should be embarrassed for having written it, let alone for letting other people see it.
Well that’s something not to do. Make you “horrible code” public, and ask people to do a code review. Or see what contributors want to change through a PR (if you’re so lucky). You’re not going to learn anything from others by hiding your mistakes. And no one besides you really cares if you’re committing horrible code.
It’s pretty hard to just give generic advice on how to write clean code, but if people can just tell specifically what you can improve it’s much easier
Someone said it before: write for test-ability. I find that code which is easier to test, is easier to read.
Also, use a linter and a code formatter. Those can be used from the get-go. They cost little and will allow you to write code without thinking too much about what it looks like.
What more, I can recommend letting the code rest for a while after having written it (1 week or more), then try to read it again and see how well you understand your own code. You’ll notice things like variable names being confusing, function names being non-descript or missing documentation, methods being too long, code branching too much, and so on.
And finally, ask for people to review the part of your code you are unsatisfied with. https://programming.dev/c/code_review isn’t very active, but I check it sometimes and try to give advice when I can.