Tuesday, December 11, 2012

Coding Practices

In the name of ALLAH who is most beneficent and merciful 


Few days back , while we were working on CMS projects, I made a list of few practices that I think one need to adapt to have better code and work. May be I will add more items in following list and I think that one can do work in much better way, if take following guidelines in mind. So below are the items of my list.

  • Try to always safely remove extra code, there shouldn't be code unless there is reason
  • Try to adopt standardized approach as much as possible
  • Avoid setting criteria based on string formatting, it is confusing and sometime not as much easy to process, in terms of performance also, specially when case sensitive and not defined properly that can be very messy
  • In any software system, if filesystem and DB are being used side by side then they should either be kept seperate or need a proper way to sync them
  • try avoiding function call in condition in both control statement or loop
  • In comments tell what a function expect, what it return and what it set to other variables that are available in same request's scope
  • Naming convention everywhere whether it is table, keyword , field or variable or function , should be consistent to avoid confusions
  • In DB, whenever there is need of having only one of different option per record then in that case enum or association with another table can be better than having many boolean fields while only one field will get data
  •  A function should be only doing exactly one thing
  • Try to limit function to maximum 20 statements

  • Function should be understandable in almost 30 seconds

  • Whenever you are struggling at one problem and every thing seems fine but still problem is not fixed then may be you are struggling at either wrong solution/way or wrong problem, so need to think again

  • Avoid global variables and functions usage as much as possible, it can cause serious problems if user don't use it properly

  • If anything is changed during work , something on which other things depends, communicate about that if others team members are working around that, even it is one line of code
     
  • Even in good communication environment, there is not always time for communication, so follow standard coding practices defined as good practices or set by your team.
  • Classes and methods should have their useable data in passed arguments or there in their object, shouldn't be depend upon outside world
    I hope that using above guidelines can help you in doing your daily development work better. 
    thanks

Sunday, September 16, 2012

Installing django on ubuntu and updating to version 1.4

I know there is a way to install django on any OS is to just go to django site and download it from there but many programmers and/or linux users always want to have a command to install anything. If you have experience of installing ruby on linux then believe me installing django is much more piece of cake from terminal. To install django the simplest way in my opinion is to just type:

sudo apt-get install python-django

and Django is installed, however please note that in this way you will may be not get latest version. At the time of writing this post, the above command installs django version 1.3 while django version 1.4 is also present.  So if you want to install django version 1.4 then just copy paste the following commands one by one in your terminal.

wget "http://www.djangoproject.com/download/1.4/tarball/" -O Django-1.4.tar.gz 
tar xzvf Django-1.4.tar.gz 
cd Django-1.4 
sudo python setup.py install

However if you have django older version of django already installed then you need to first remove it via following command before installing newer version.

         sudo apt-get remove --purge python-django

To make sure that django is installed and to see what version you have open python by just typing python in terminal. And then type:
       
>>import django
          >> django.get_version()

And if there is django installed then there will not be any error and last line will show version number of django. I hope it is clearer, if not then please tell by commenting. And if there is some better way then please tell. If there is something more you want to know then please tell. Don't forget to subscribe and comment if you like the post.

Monday, February 27, 2012

Installing django on Windows

In the name of ALLAH who is most Beneficent and most Merciful

Salaam ,

   If you are web developer who have done lot of work in PHP, Joomla, CI and wordpress e.t.c. and you think that you can be a better web developer only by doing more and more work in PHP then think again. PHP have lot of good frameworks but there are lot of things that are used to determine a better framework. Also a single language dependent framework is not as good as developer who have tasted different frameworks. Then why not start a framework that is much better than many other frameworks present there. I am talking about Django and going to tell about installation of Django on windows while there is xampp already installed, as there are some people who find some difficulties in installing.
 
  So following are steps to follow.
1. Go on http://python.org/download/  and download python 2.x  not python 3 as Django right now at the time of writing this article, don't support Python 3. So download python 2.x  windows installer version and install it and restart your command prompt before next step if it is already running and more better to once restart system.
2. Go to Django website, download page: https://www.djangoproject.com/download/  and download latest official release. Currently it is Django 1.3 . Then extract it and navigate through that extracted folder using command prompt.
3. Type " setup.py install " and it will install django. To check if  it is install correctly, open your python console and type "import django" if it doesnot give any error then it means that django is successfully installed. But remember that normally MySQLDB module that is used by django don't come by default. So you will need to do some thing to connect django with mysql.
4. MySQLdb is just a connector for MySQL. When I tried installing it, it didn't find it easy to get and install MySQLdb. But thanks to pymysql another connector that can be used as an alternative of MySQLdb and there is no difference in using any of these. I found about pymysql from here:
http://web-eng-help.blogspot.com/2010/09/install-mysql-5-for-python-26-and.html
Please go to this site to know the way of installing pymysql in detail.

Please ask any question if you find any problem installing Django on windows. I will try to solve your problem.

thanks