Tuesday, 20 September 2011

Software Engineering - Continues refinement

Every programmer wishes to write some code, which is both elegant, and readable. A master piece to reference to...
And there may not be a single programmer, who said at least once in their life time that, "If I had time, I'd rewrite it" or any similar one.

Today, let's discuss, how the idea of sorting a list of list went on,  

I had to sort a list of lists, that's the result of a search, comes from Web Service. After that, the result is extracted from the XML document that's returned and formatted for front end. So, now all the data is in the form of strings. And I'm ended up with the task of sorting a list of list of strings, based on different items in the inner lists @ different times. And Obviously we can't compare the data as strings, we have to convert them into their proper data type before comparison.

Since, I works on a Python based project, First thing, that came to mind is to use, sorted inbuilt function, and pass it a function to compare. But the new way of doing this pass a function to key argument of sorted instead of cmp argument.

This is a bad idea, that we are doing array sub-scripting on the lambda itself. Which is a lot inflexible. A quick visit to python wiki showed me, how to convert a cmp to key argument. This is more flexible, Now I can tell, which column to sort, how to convert that particular column to proper data type etc.
Now, compare() is a very simple function, which I don't have to define separately, so I put that into cmp_to_key() function as a default parameter, a lambda.
 
But, hold on, Why should we create a lambda, if we can do that comparison directly, without creating a function first? So, our lambda got removed and comparison is performed directly on __lt__ special function
 
Ok, now we are in good shape. Now, we can shift our focus into, How to specify a a function, which will convert a string in the lists to a proper data type. I'm using DataTables jQuery plugin for client side display of paginating table. So, what I get back to server to determine column is a number. So, I made a list, which I can map to in sorting function based on which column to sort.

Here, we are making a lot of repetitions, with DRY principle in mind, I decided to normalize those, like Database normalizations, you can say. But there's another reason for the decision, we have another type of searches, whose return records varies, and in effect will create a lot of duplicates of anonymous functions which does same thing. So here goes the solution

Here goes final solution, bear in mind this is simplified version, I removed all the complexities and simplifications involved, because of Classes, packages, etc., which is there in real system. But, I do believe, this shows how a piece of code evolved to solve a problem, And why it took time and knowledge to become perfect / or near perfect.
NB: sorted function uses a condition at end to assign a value to reverse, which is actually a Boolean. That single statement alone undergone a lot of transformation in the process. Well, that's for another blog entry.

Tuesday, 6 September 2011

Start on Microchip programming... for hobby or for money


One of my friend asked me today the following question, 
I'm often asked about this, someway or other.
Let me answer this now for all....


Q: "I want to start programming on chip.. Can you suggest a good chip and a device to program it? Also tell me any sites which can help me."


A:"Simple one is Arduino. You will get it packaged with a programmer.
If you want some more powerful and commercial one, Go for Microchip's PIC family of processors. After you are familiar with those, and need even more power, try AVR from ATMEL."





Some resources from my Bookmarks is given below:
http://www.voti.nl/swp/
http://www.embedds.com/
http://www.instructables.com/id/Business-Card-PIC-Programmer/step2/Parts/
http://www.arduino.cc/playground/Main/ElectroInfoResources
http://www.piclist.com/techref/microchip/index.htm
My bookmarks become so messy now a days, and I'm not getting time to organize them.
So, these are the quickest ones that I picked up from it. I'll let you know, if I found some others.
My old Book mark is here athttp://www.delicious.com/atharvan
you may find it useful."
'