GotchaCode

Seven Tips To Get Better At Writing Code

20 Jul 2013

Every developer dreams of getting better at writing code. He/She wants his code to be clean and pragmatic. But this doesn't happen on its own. Here are some tips to get better at writing code.

  1. Follow TDD: Test driven development is a great way to improve your code. It ensures that whatever functionality you have implemented is doing what it is supposed to do. One should make sure to spend some time writing tests before writing a feature. It is a difficult practise to inculcate in your developers, but believe me the amount of time and stress you will save with it  is phenomenal.  There are basically two types of tests that should be present with your code:  unit tests and integrations tests. Both are very 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. 
  2. Write detailed commit messages:  I can't recommend this one enough. Ever since i am introduced to this practise, i could easily feel the improvement in me and my code. It 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 codebase gets bigger.A new developers could just take a look at the code and commit message and will be more comfortable getting up to speed. Also, if you are working on an open source project or contributing to one, 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.
  3. Teach yourself about writing maintainable code: Always, try to think about writing maintainable code. It might not be possible every time, since sometimes deadlines won't allow it. But make a point to revisit that code and fix that hackery you did back there to resolve the issue. This is really important, because sooner to later, that hackery/bug will come to bite you again and things could get really ugly.
  4. Know your editor: You should and must configure your editor for the language your code on. It must have all the required plugins to do live linting while you code. 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.
  5. Automate all repetitive tasks: At this time, we are pretty much equipped with the technologies such as grunt and yeoman which could automate a lot of part that you had to waste your time doing again and again. You must also learn about git hooks. For example: I made a git pre-commit hook to compile less into css and run jshint, concat and uglify operations on my javascript code any time someone changes them. This is great because my workflow prevents any bad code to enter the codebase.
  6. Read good code, data structures and Algorithms: There is no end of 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.
  7. 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. 



One thing which comes in my mind is to make use of less keystrokes i.e. to write less but should be understable. Even character layout works great, character displays everything in a simple way.

Tweet