Before writing any line of code, you have to ask the right questions. Assuming you need to write a new function, make sure you understand the purpose of it. How will it be used by other parts of your software. Should it be only available for some classes or from everywhere? Is it a shortcut for a repetitive task or is it specific to some process?
Make sure that you fully understand how this function should be used and what is its main purpose. That is called designing your architecture and you'd better spend more time designing than refactoring everything a few months later.
It may sound easy but writing a good source code is not about implementing the latest and greatest technologies. It's about stability, clarity and efficiency.
Your code must be bug-free but more than that it must be able to handle all scenarios. Bulletproof your code so that nothing can go wrong. If something can be at 0 (zero), null or empty, make sure that you've handled the case.
Keep in mind that someday, someone else will read your code. Make sure that it's easy to read and understand. Comments will help but if comments are mandatory to explain the logic behind your code, you got it wrong in the first place. A good code is when any developer can have a look at it and understand the whole right away.
Speed is another factor to consider. Be careful, do not over-optimize everything. Time is limited and you should put your effort on critical processes like when loading a million rows. Optimizing the press of a button just to save a few milliseconds is not worth it.
Identify what can be re-used, put in a cache or save locally to improve speed and avoid repetitive processes that can take a lot of time. For example, establishing a new TCP/IP connection for each element may end up with a major delay for the user. Reuse the same connection for each element. It may not sound much but a simple connection process can take 10 milliseconds to be established. For a thousand element, you will loose easily over 10 seconds to complete everything.
Test, test, test!
Everything is working, everything is fast... Wait a second! Testing is required. You have to validate that is does work with everything, with every input possible and in the worst conditions. Never assume that user will input the right data, he won't. Never assume that no one will have more items than you have expected. Never assume that the computer will have enough memory or storage space.
You have to validate the good data, the bad data and the ugly data as well. Any experienced developer will tell you that a user can enter a wrong date format, a text instead of a numeric value or close the dialog before saving a document on the had drive. It happens all the time.
Once you have tested everything, pass it on to someone else. Preferably a user that has no technical knowledge. Just provide basic instructions and let the user do whatever he or she can think of. This will be the ultimate test and you'll know if your code is stable, clean and fast.
"Make it work, make it clean, make it fast!"