What does it take to write a good code?
So you want to write a good code, then you need to care about your code more than you do now. You need to respect your code, and always write your code as if you have to support it for the next 10 years. If your boss calls you in the middle of the night to ask you about the choices you have made regarding a particular method example(), then it should be clear to you by now that you didn't write a good code.
You don't have to be a genius to write a good code. Generally speaking, I have come to work with two groups of programmers, in the first group are those who could create a magnificent and elegant algorithms that run in the most optimal way one could image but are very hard to understand, painfully inflexible and could be very expensive to maintain. The second group are programmers who could write simple (not necessarily optimal) code, easy to understand, very well documented and supported with enough amount of test units. Personally I see my self somewhere in between, so does some of my friends, therefore we could say there might be a third group which falls in between.
To write a good code, you need to have a positive attitude about the professional way of crafting software, requires you to avoid blindly hacking out a solution for a problem without even knowing why it has occurred in the first place. A good coder always makes sure his code is readable and easy to understand for other programmers. Even though most programmers don't like to write tests, not because tests are complex to write but they could take the fun out of your programming, good programmers will always put effort to write enough tests.
To sum up, writing a good code resonates around having a respect and positive attitude towards the world of software crafting. It's equally important to also have a passion for your profession, you should ask weather you like your profession or not. A previous colleague of mine once told me, when she was young her father used to tell her, “Listen dear, you can be what ever you want when you grow up, but you have to be good at it”. If you are destined to be a coder like me, then try to be good at it, respect your code and enjoy chopping problems with your sharp edge axe(your code).