Every developer dreams of getting better at writing code. He/She wants the code to be clean and pragmatic. But it doesn't happens on its own. Here are some tips to get better at writing code:
- Follow TDD: Test driven development is a great way to improve code. It ensures that the functionality you implement is doing what it is should. You make sure to spend some time writing tests before writing a feature. It is a difficult practise to inculcate in your team, but trust me the amount of time and stress you will save because of it is phenomenal. Basically, there are two types of tests that should be present with your code:
Unit tests and Integrations tests. Both are equally important. Sometimes, it might not be possible to get everything unit tested but make sure key parts have units tests and integrations tests are complete.
Write good and detailed commit messages: I can't recommend this one enough. Ever since I got introduced to this practice, I could easily feel the improvement in my code. I prefer it to writing long comments in my code. Wherever you did something that needs to be explained or if you did a hack to resolve some particular issue, make sure you address these parts in your commit message. It really helps because whenever your code base gets bigger.A new developer could just take a look at the code and commit message. He will be more comfortable getting up to speed. Also, if you are working on an Open Source project and follow this practice, chances are really high that your pull request will be merged. One reason for this is that, the person merging your code will not have to dive deep into your code to make sense of that you did back there. Your 'detailed commit message' will serve its purpose then. Though your commit messages shouldn't turn into project documentation: while you should put a summary and a short list of things you changed in a commit, long explanations of algorithms, data structure and overall project architecture should remain and be maintained in project wiki (or whatever tool you use for internal documentation).
Teach yourself about writing maintainable code: Always, try to think about writing maintainable code. It might not be possible every time because of deadlines. But make a point to revisit that code and fix that hackery you did back there. This is really important, because sooner to later, that hackery/bug will come to bite you and things could get really ugly.
Know your editor: You should and must configure your editor for the language your code. It must have all the required plugins to do live linting. If you do python or JS, there are some pretty great plugins available for both the languages in Vim and Sublime Text2. Also, get familiar with some good snippets generating plugins for frameworks, HTML and CSS. There are pretty great plugins to beautify and minify your code as well. They really help in getting the whole code quality better.
Read good code, data structures and Algorithms: There is no end to learning. Teach yourself to read more and more good code. You must spend time getting better with data structures and algorithms. They might not prove useful immediately, but it will make you a better developer and will enable you to take better decisions in future.
Watch talks and presentations: Be regular to watch great talks and presentations from the various conferences round the year. They give some pretty great information about the latest trends and techniques. You could easily find them on YouTube/Vimeo and SpeakerDeck.
For more interesting blog posts, visit http://www.gotchacode.com
Edit: Thanks dpashkevich for your great remarks on commit messages. Added them to the commit messages section.