Saturday, March 14, 2009

Happy coding php with xdebug

Today I started working on drupal. I am reading about AHAH, then went to drupal development tools page, from there I stopped at xdebug.
I know about xdebug but I didn't think it really works well. I did not find good tutorial before about how to use it.
I am working with php and python. I always used to think php is not having good debugging support like python. But xdebug is there to fullfil it.
I found good xdebug tutorial. xdebug is very good. It tells about how to print the stack trace of errors, local variables, global variables, parameter values. Different configuration settings how to display them. Now I am happy to code in php.
We can not debug the code without using any IDE. The tutorial explained by using Eclipse PDT.
It supports profiling and code coverage. It generates results which another tools can understand. The tutorial explained how to use those tools also. I hope xdebug tutorial is good tutorial to read.

Saturday, December 6, 2008

My first jQuery plugin, jquery connect

I released my jquery plugin that is jquery connect. This is same as dojo.connect. One thing that I like very much in dojo.connect function is we can add a handler for function also. This is very cool and it helps me lot of times while using third party plugins customization. This kind of functionality is not there in jQuery. I felt like this is very cool feature and I also needed most of the times, so I decided to write plugin for jQuery.
The plugin url is http://plugins.jquery.com/project/jqConnect

Saturday, May 24, 2008

Using Emacs to edit PHP files

When we are programming on any language a good editor(emacs :)), lint (which gives syntax errors) and interactive debugger helps us to code fast.

I want to use emacs to edit php files. There are other IDEs which supports lint and debugger but I feel much comfort with emacs. So my idea is to find good php-mode for emacs and I want to find sytax errors in emacs itself. And I want to find good interactive debugger like python debugger.


I found php-mode and compiler for php but I could not find interactive debugger in the command line for php.

PHP-mode for emacs:

http://sourceforge.net/projects/php-mode/

Download php-mode from the above location. Extract the compressed file and copy php-mode.el to emacs lisp path.
Refer this link for emacs lisp path http://www.gnu.org/software/emacs/elisp/html_node/Library-Search.html.
or you can put this php-mode.el file in some directory and you can add this directory to lisp path. To add this new directory to lisp path add this line in the .emacs file.

(add-to-list 'load-path "~/path/to/new/directory/")

And add following lines to .emacs file to enable php-mode

(require 'php-mode)

;; To use abbrev-mode, add lines like this:
(add-hook 'php-mode-hook
'(lambda () (define-abbrev php-mode-abbrev-table "ex" "extends")))

Enable Tab indentation to 2:

The php-mode indentation for arrays is not good. It will indent like this

$a = array(
'foo' => 'bar',
'gaz' => 'gazonk',
) ;

If the associated array is having another associative array. then it will be like this

$a = array(
'foo' => array(
'bar' => 'bar info')
,
'gaz' => 'gazonk',
) ;

If we are having more values like that then the code does not look good. It looks good if it will be like this.

$a = array(
'foo' => array(
'bar' => 'bar info')
,
'gaz' => 'gazonk',
) ;

To get view looks like that add following code in .emacs file

(defun clean-php-mode ()
(interactive)
(php-mode)
(setq c-basic-offset 2) ; 2 tabs indenting
(setq indent-tabs-mode nil)
(setq fill-column 78)
(c-set-offset 'case-label '+)
(c-set-offset 'arglist-close 'c-lineup-arglist-operators))
(c-set-offset 'arglist-intro '+) ; for FAPI arrays and DBTNG
(c-set-offset 'arglist-cont-nonempty 'c-lineup-math) ; for DBTNG fields and values

Enable php mode for drupal module files:

To enable php-mode for drupal module file add these lines to .emacs file

(add-to-list 'auto-mode-alist '("/drupal.*\\.\\(php\\|module\\|inc\\|test\\|install\\)$" . php-mode))
(add-to-list 'auto-mode-alist '("/drupal.*\\.info" . conf-windows-mode))

In place of /drupal you can put your drupal code base path. I put my drupal project at /var/www/ directory I replaced /drupal.* with /var/www/

PHP lint:

Hardly no body use php in the command line, so to use we need to install php-cli (php command line interface). Using php at the command line will make us to find errors easily and fast. We can find syntax error with the following command.
php -l <php file name>

To find more about php-cli option follow this link
http://www.php-cli.com/php-cli-options.shtml

I am using emacs so I can directly check syntax errors in emacs without running the above command on the command prompt. To do that add the following lines in the .emacs file.

;; run php lint when press f8 key
;; php lint
(defun phplint-thisfile ()
(interactive)
(compile (format "php -l %s" (buffer-file-name))))

(add-hook 'php-mode-hook
'(lambda ()
(local-set-key [f8] 'phplint-thisfile)))
;; end of php lint

To check syntax errors press "F8" function key on emacs editor, then currently opened php file will complie and show the syntax errors if any.

PHP Debugger: I tried to find good interactive debugger like python debugger. But I could not find it. So I might be developing one in the future.

Sunday, December 9, 2007

My experiences with test driven development

Test-Driven Development (TDD) is a software development technique consisting of short iterations where new test cases covering the desired improvement or new functionality are written first, then the production code necessary to pass the tests is implemented, and finally the software is refactored to accommodate changes. The availability of tests before actual development ensures rapid feedback after any change. Practitioners emphasize that test-driven development is a method of designing software, not merely a method of testing.

I copied that definition from wikipedia. I am following TDD for the project that am working now. Not fully following, using for the core functionality. This is very new to me and I am feeling great to work on TDD. In the beginning I really struggled to write test cases. In fact my TL removed some test cases that I wrote in the beginning because they are not useful:( .

Am still in the learning stage only. Now I am writing test cases which are useful(I think :) ).
These are the steps that I understand while doing TDD.
  1. Write test cases which fail. (because you didn't write the functionality the test cases should fail.)
  2. Make the test cases to pass. (now write the functionality and make the test to pass.)
  3. Then do the re factoring on the function.
  4. Make the tests to pass.
I am more confident about my code now. Before that I am not having confidence about my code.
Before following TDD these are the problems I faced.

1. I am not sure about my code that it will work on all cases.
2. When I fix one bug , I am not sure that that bug fix will not cause failure to working functionality.
3. I don't have confidence that this functionality works fine and it is not having bugs
4. I am unable to say any functionality is working fine.(I don't have any functionality which is bug free). I don't have any proof to say.
5. When am testing my application there is no proof that I tested every functionality, I used to forget testing many functionality.

After following TDD I am able to solve all the above problems. And am getting much satisfaction about my code because what ever small functionality I write I have confidence that it will work fine and it won't break other functionality.

Some people may think that this method is very slow. It take time in the beginning to get into TDD but when you are trying to add more and more functionality it will be very easy to test and you can save the time of testing.

My learnings from my mistakes about programing.

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

That is true. I am also a fool until 2 months back. I used to write code that only computer can understand. I understood the importance of following programming standards from my mistakes. Here are some good programming practices that am following now.

Commenting the code. I never used to comment the code so that when ever i go back to the code I used to take some time to understand what am doing there. And when ever i am seeing some third party code i used to skip the comments and tried to understand the code. see how stupid i am.

Using Proper Indentation. If the code is properly indented then you can easily understand the code. If the code not indented it won't look good also.

Strictly following the syntax. If we know that semicolon at the end of each statement is optional then we don't bother if we don't put semicolon at the end. But this may cause some other problem later. Use some syntax checkers like jslint for javascript, pyflakes for python to check the syntax before testing the code. So that you can find the errors before running the code and you can save the time also.

Not using recursive if-else blocks. If you use if-else in another if-else and this is in another if-else. Then we get confused and it is much harder to understand. I want to put some example here. This is the if-else loop i used previously

if request.POST{
if request.POST['submit']{
......do some thing
return success page
}
else{
......do some thing
return cancel page
}
}
else{
......do some thing
return some page.
}
The above code is simple and if we are using the code like above the indentation is very much important otherwise we lost, we don't understand any thing . The same code i can write like bellow.
if not request.POST{
......do some thing
return some page
}
if not request.POST['submit']{
...... do some thing
return cancel page
}
do some thing
return success page.

See How beautiful the code is, we can understand very clearly and there is no chance of loosing in the middle.

Trying to make the classes(or modules or functions) more independent. The more independent you make it is more easy to debug or apply changes later. Because it won't effect the other parts of code. And one more thing use only one way of dependancy, i want to give example here.

obj1 ---------> obj2 --------> obj3 (correct)

obj1 ----------->obj2---------->obj3 (wrong)
^
\_____________
______________________________

Suppose obj1 is dependent on obj2 and obj2 is dependent on obj3 this is normal single way of dependancy. if obj2 is again dependent on obj1 or obj3 is dependent on obj1 or obj2 then this dependency will become mess and we don't understand any thing.

Not hard coding anything. If you need to use some max limit as 5, then while coding don't use 5 directly make it as a variable, if we change that value we can change only once otherwise we have to change it where ever we hard coded and there is a chance of forgetting so it will cause problems.

Writing comments on every commit. While committing the code if you write what you did for this commit it will be easy when you are refer back, when you lost some code we can easily trace. When we do some mistake also we easily go back by seeing the comments. Commit after every change that you made. Suppose you added some simple one line statement which is not making wrong also you commit and say why you wrote that line. Before commit see the changes that you made to files by using diff command then you can know what are the changes you made clearly.

And some other things that i have learned are
  1. When ever we are using third party tools or frameworks or any thing, first read the documentation even if we don't understand but we can know all the features supported by that tool or framework and later if we need we can use.
  2. When writing any function don't rush to start coding. First understand what this function has to do and what it should return. Then think how to do that functionality. Then write the steps what you have to do in comments then write the code. Once when try this you came to know how cool this way.
  3. Dry the code as much as possible. Don't repeat any small peas of code.
  4. Don't use big big lines which causes the code look ugly which breaks indentation. Use maximum of 80 columns per line.
  5. When debugging the code. try to use debuggers they will help you to solve the problem quickly.
All these things I have learned from my team leader eduardo.
Thanks to eduardo :).

Thursday, December 6, 2007

My experiences with cometd

Cometd is a scalable HTTP-based event routing bus that uses a push technology pattern known as Comet. The term 'Comet' was coined by Alex Russell in his post 'Comet: Low Latency Data for the Browser'. That is the definition given in the cometd website. This is the link to that web site http://cometd.com/.

When i start working on cometd I didn't understand push technology, scalable HTTP-based event routing bus. What i understand is the server can able to send messages to client without client request the server. In normal way of communication with server and client, the client request for something then the server respond to client but this is in reverse way when any event happened at server will send message to client hey this event has happened update with this data.

So to make this happen the comet will help us with the comet server which exists at the server side and comet client which exists at client side.
This comet is following bayeux protocol. When i start working on comet i didn't understand clearly the bayeux protocol.

So i gone through the chat example in the comet. From here at the client side i have to first start the connection with the cometd.init, then i have to subscribe to one channel with the cometd.subscribe. later to send any thing to server i have to use cometd.publish so the server will publish this message to all clients who subscribed to the channel where i published. I didn't understand clearly what the server is doing to make the server keep sending these messages to subscribed clients. And while subscribing i am specifying one function which will execute when ever any message came from the server. I don't know clearly how this function will execute when server sends message to client.

Then i thought i can start working and i started using comet in my application. With only this knowledge i developed my application.
At that time i am using dojo0.4 at client side and i am using twistedcomet at the server.
Then the problems
1. it doesn't works with IE browser.
2. the biggest problem is connection failure detection.
3. lack of synchronisation with all clients.
4. suddenly one client stops receiving messages from server but it can able to send messages.
5. how to reconnect when there is connection loss with the server

I didn't understand what is happening at the background.
So i started knowing more about what comet client is actually doing and what comet server is actually doing and how the server can able to send messages to client without the client request for something.

Then i started understanding what is push connection. what is scalable HTTP-based event routing bus. and bayeux protocol.


Http is stateless and Http is on top of Tcp connection. The actual communication with client and server with Http protocol is, first tcp connection will establish between client and server then the client sends some request to server using Http. when the server reply to that request the connection will automatically close by either server or client.
The things happening in comet is
1. first the client will send request to server.
2. server won't reply back it holds the connection.
3. when ever any event happens then the server will reply on to the holded connection.

so to make the connection open every time we can do in 2 ways
1. streaming
2. polling.

Streaming means while server replying to client, server sends some fixed length packets and server don't say that the message transfer is over. So the client receives these packets of data and process it. Because the data transfer is not completed the connection was still alive.

Polling means server reply to the client and closes the connection. then client again sends connect message so the server will hold this connection until some event happened. This procedure will repeat to keep the connection open.

I understand that the connection which is hold by server is called push connection.
Then bayeux is a protocol which is used by comet. It specifies how the message pattern should be while sending message to comet server. It tells procedure to establish push connection.
Then when go through the bayeux protocol we can understand clearly how comet is working.
Really this bayeux is very good.