Wednesday, September 11, 2013

Code for your future self

It's quite annoying to work on existing code that you find hard to navigate and understand. It makes you spend some significant time first to understand the existing structure before you can finally add the feature or fix bug you intend to do in the first place. It does not have to be the code written by someone else, even your own code can give you some hard time when you revisit it again at the later time.

That is why the importance of writing good code is not just so the features runs well and the code looks good to your peer. It is also for your own benefit to have the code done in a well-designed and intuitive way at the moment you wrote it.

There will be temptation to code something quickly when deadline is approaching or you just feel lazy to extract those abstraction that you find. For your own sake, resist them and write code properly in the most natural and the most obvious way you can think of it. Your future self with start with that kind of intuitive thinking before he jump into specialize cases, workarounds and other tricks.

Here is an example of the things you can do to minimize the pain of your future self coding session :

  • Choose good naming. This is simple and highly essential but easy to break at the time when you feel lazy or under pressure
  • Have all the dependencies locally and self-contain whenever possible. For all dependencies that you can not put locally, put detailed documentatio on how to get it
  • Automate and simplify the build system. Again document unscrypted steps extensively
  • Make notes and diagram on general architecture. Give high level diagrams and don't forget to update them when it no longer reflect the actual code. Do not go into too much detail on this. Focus on  describing the big picture of architecture on the notes and diagram and let the code speak for itself whenever it can
Think of yourself when coding. He will thank you greatly later when he has to work on it again in the future.You know, even your current self will appreciate it too. It's much more fun to make a good code than to make a hasty/sloppy/buggy one.

No comments: