Hey as the title says, I have a theory that If the code works and is efficient it is good code.
However I know this is not true because there is so much optimisation that can be put in place.
So my question to you is, how do you know when your code is perfect, or even if it actually can be, as I always see improvements and If I keep adding these improvements I would get nowhere in feature addition.
Think of it this way: At the end of the day, the only thing that matters is that it works. Most other metrics simply help inform you on how it’s likely to work. If you’re code is twice as long as it needs to be, written in a cryptic and confusing sense then the odds of it working [both now, and in the future] are fairly low.
So we write short clean maintainable code so as to end up with working code.
If its net code, it needs to be secure too .
It needs to be well documented , if its some very large code you need to comment it well .
It needs to play well with other code, If I’m on a team and all my functions break if I make them public then its not good code
Bottom line: it’s good because it works. None of the other answers say that and “All of the above” is useless because anyone wanting the best answer will choose that, so I ignored it.
Good code is code that works as expected in the timeframe expected by the user/stakeholder given the technical limitations of the hardware. If any of these conditions are not met, the code needs more work.
Nothing is ever perfect. When it comes to coding, there is always room for improvement, but there may not be time available to improve anything. In many cases, you have one chance to “code it right” the first time.
The only time you really “need” to write clean code is if someone else is going to be using or it taking a look at it.
Teachers/professors always teach optimize! optimize! That program could run a little faster!
There is a point where you’ve spent too much time optimizing your code where it’s no longer beneficial. As long as it runs on the targeted hardware without dragging down the system then it’s good enough.
Edit:
And of course, YOU need to always be able to read your own code…
Code has to be supportable and maintainable as well as working. It also does not have to be “as fast as it can be”, it only needs to be as fast as it has to be. Ie, if it is not a bottleneck, don’t optimize it.
You could spend your whole life optimizing code over and over again. It’s more important that you write working code. You don’t need to optimize things that aren’t causing a bottleneck. It doesn’t matter if it takes 1 second or 1 millisecond to load a saved game, for example. But I’m sure you could spend weeks making it as fast as possible.
Depends if other need to use it or it will be reused in other ways later. If it works and it’s messy that could be a problem, also if you want to add functionality later and it’s messy, well that can be bad.
This. I manage a team that maintains and enhances a nine year old 600,000+ line multi-tier web application. We have added to it continuously throughout that time; which would not have been possible without having a maintainable codebase.
Now, a couple thousand line program… I wouldn’t spend nearly as much time making it maintainable as I would a larger program. As with most things, you need to ‘right-size’ it.