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.

Wednesday, April 04, 2007

I uninstalled sqlite on my Linux box !!!

Well I somehow felt I could yum erase sqlite since I do not need it. Well I did not see the dependencies that would be removed too! The result:

yum, rpm, php, httpd, rpm-python and many related packages all Erased !!! OMG I said. :P


I am not very friendly with Linux as yet at this level. I knew I had screwed all hopes of getting that box in clean shape. I searched the net for "reinstll RPM", "install RPM" and similar. Got a link to How to reinstall rpm

In case you can not reach to it, it says something like:

I guess you could try :
- Copying the "rpm2cpio" binary from another machine
- Copying the rpm package locally
- Running something like :
cd /
rpm2cpio /path/to/rpm-4.*.rpm | cpio -dimv

Then you could clean up what you just did (in case it's a different version of rpm, mainly) :
rpm -e --justdb --nodeps rpm
rpm -Uvh /path/to/rpm-4.*.rpm

YMMV...
Matthias

That gave me some hope. I followed the instructions. Switched to root user, went to / and executed rpm2cpio with the downloaded rpm of RPM itself. RPM

Then got an error about librpm shared lib missing. So downloaded librpm. Now rpm works!!!

Now I just searched for the latest versions of rpms for getting yum up and running. This included sqlite, python-sqlite, rpm-python, yum-metadata-parser, urlgrabber, etc. And finally installed yum !!!

I mainly used rpm.pbone.net for searching rpms

Well thats it: I am almost saved :D

Thursday, March 08, 2007

My laptop: my local web server

Well two months back I bought my first laptop. A Compaq V3155AU with following configuration:
AMD Turion64 (single core) 2.0Ghz
Hyper Threading support
512MB DDR2 system memory (I upgraded that to 1GB)
nVidia 6150 graphics chip-set with shared memory
14.1" display
60GB SATA hard disk drive
DVD/CD R+RW
Bluetooth
Ethernet
Wireless Lan
At sub Rs. 40,000 (sub US$ 900) this was a great deal. I was using Fedora Core 6 (64bit edition) on it but was not using it much since I still love my desktop and do not have enough work to justify simultaneous usage of two computers. Recently my friend planned to join me on web development work and I thought this was a good time to use it regularly. So I had to first make sure the Wireless Lan worked since I Fedora had not done that automatically. I read:
"Compaq Presario V3000 with Ubuntu 6.06" and it helped a lot. Actually I got the Wireless Lan running in no time. It was very easy. If after a shutdown or restart you need to connect just use the usual commands (iwconfig, iwlist, dhclient to scan for network, connect, etc.).
I setup up my usual structure for web projects. PHP was already there, so was Apache. A few needed PHP modules were added (APC, MagickWand for ImageMagick) and I had a working local web server !!!
Things are really that easy if you are using the right software :) and there is always Google to help you when need it.

Wednesday, March 07, 2007

Problems with installing Yum on Fedora Core 4

"There is a problem importing one of the Python modules" : ever faced this error after installing an running yum. Well I faced that. And searched on Google for it. But in vain. Most results suggested the usual missing packages:
libxml2-python, python-sqlite, or python-elementtree
I tried installing all of them and no use. Then did lot of finding around and finally i tried to install other package managers. Finally I cam across a yum rpm package at rpmfind.net and while installing I got the dependency error for urlgrabber (http://linux.duke.edu/projects/urlgrabber/). Installed that and all was fine !!!