Saturday, September 22, 2007

Some points on code and stuff

I have learnt a lot in past 4 years of PHP and here are some tips I want to share:
  • Think in code: Many times when we learn a foreign language we are told to think in that language itself instead of just thinking in our own language and converting to the foreign one. this stands true in coding. Think in terms of logic, even in actual syntax, dry runs in your head. It seems somewhat difficult at first but as you gain on the language it will get easier.
  • Think of the big picture: Will the site or application go to million users, million page views? Does it need to speak to other stuff in future? (RSS, RPC,...) Will your team expand from a small one into one with multiple departments? Think in those lines. It will not takes much time. Just spend a day, figure out the future and try to be future-proof.
  • Depend less on re-architecture: In many development cycles in current web trends where people are trying to make too many sites too fast, many make a same mistake: bad architecture deliberately! Why? because they all feel they just need to make something good enough for investors and then re-architecture. No one can create a time proof system, but it helps a lot if you at least read up what others did, and what problems they faced or mistakes they made.
  • Do not re-invent where it is not needed: Sometimes you can surely go ahead and use other codes, do that. Its easier than building from scratch. But do not overdo it. Use only what you will need. If something of high performance is needed then it may justify making from scratch. But remember building a full web application is not a days job. Even with the finest coder it will take at least a week to get a decent social network coded from scratch.
  • Simplicity !!!! Well the less I talk on this the better: Release simple versions. Let users play with it. No one assumes you to be the Google and have all code perfect. And even Gmail has Beta... Just tell your users you want to try out things slowly, they will understand. Do not try to launch with a universe of features. Try not to postpone dates. Read Getting Real and you will understand.
Now on the more code intensive front:
  • Use PHP! Hah! I am a PHP fanboy, blah! blah!, should I say otherwise. I have had this discussion with many folks who still feel somehow there is some other language out there that is more suited to making web sites. My answer: well prove it. PHP will give you advantages of starting fast, more support from community, low cost of starting up, low running cost... etc. Should I mention more?
  • MySQL is NOT a data store: Most dynamic sites have the database as the real bottleneck rather than PHP. Unless you have many thousand dollars to spend on costly hardware just remove MySQL!!! Am I kidding? NO. MySQL is an RDBMS, not data store. Same for most others like PostgreSQL. Use memcache as a datastore. To just give an example: Our site used to do 80% average CPU for MySQLd. We shifted almost all the user dynamic parts to memcache: memcached does the same job at 5% average CPU.
  • Optimize when you have time:Most code out there can be further optimized. It is like evolution: it keeps going on... So keep biting into that code and chew out unnecessary parts. Replace if sections in loops with better non-if methods. You will find many articles on these and more.
  • Clean code: Well we can not emphasize enough on this one. From the day one you start coding, code clean. Try looking at how more popular, older and well maintained code bases have been written (Drupal, Joomla,...). See what rules they follow that help keeping things organized. You know you need to get back to your code 6 months later. Unless its clean and understandable you can not do that. This will grow into a habit too.