The "It's good because it works" Theory

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.

-Myhi

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.

Most of the time my code is as clean and short as possible, but sometimes it really gets expansive (800 lines a function).

And half the time I accept the fact it cannot be any shorter.

Show me a single 800 LOC function that can’t be any shorter, and with 99.5% probability I can show you a better way of doing it :stuck_out_tongue:

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.

One awnser did say its good because it works?

Well duh. Your over 200x more experienced than me haah :stuck_out_tongue:

I more ment to my capabilities and effort in doing so

Stange question.

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.

Or you stuff it up the first time and forever hate yourself for not re-writing it before other features started to rely on it :stuck_out_tongue:

Code is good because it works!

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…

Remembering that ‘someone else’ includes ‘you’ because it didn’t work.

I’ve never met a teacher who’s taught that, in fact the exact opposite - so I’d call your ‘always’ into question.

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.

How do you know that it works? Maybe it only worked those times when you ran it under specific conditions.

–Eric

Shorter is not always better.

I think optimization is better it may mean it will run on a weaker device.

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.

I also noticed no-one has chosen All of the above cough Hippocoder cough

It comes down to time… if you want a game made quickly then don’t optimize, if you spend longer optimizing then it is going to be better.

Now one cares how something is done, only that it is.

The only exceptions to this are critics and specialists within your own field.