There exists no "best code".
If you mean "most performant" code, then don't even try to do that, or you need to right away write assemble instead of a higher level language.
Also it usually is not worth it.
80 % of the lifetime of a program is spent in 20 % of its code and there is not much point in optimizing the other 80 %.
Generally:
1. make your code work like you intend it to (optimally with tests, will give confidence for later steps)
2. make your code nice by refactoring (for example moving things into methods, renaming, whatever, so that the code is better readable and maintainable, remember, the code is much more often read than written, so you should write code for readers)
3. make your code fast, but only where you actually measured a problem. There is not much point in making some part of the code as performant as possible while loosing readability if the code is run once an hour, but it might be worth to squeeze every bit of performance out of a code line that is executed once every 100 ms.