Tuesday, June 18, 2013

Book Review : Beautiful Code – Leading Programmers Explain How They Think

As programmer I am always looking for improving myself. One of the ways to improve is to study ‘the masters’. This is a norm for artists, architects etc. A new painter studies how other past master painters done their work. Initially they mimic their style and later develop their own. The mathematical equations, science concepts, programming have their own beauty. For a software developer, a really well written piece of software has its own ‘elegance’. It is a ‘work of art’. It is ‘beautiful’. But it is hard to describe that beauty to someone who is not a programmer. So this is book for software developers to understand the beauty in software.

This book gives you examples from master programmers and what they think about their work or about other master programmers work. It’s a great way to gain insights on how master programmers think about a particular problem.  This book has articles written by masters like Brian Kernighan (Inventor of C), Karl Fogel (lead developer of Subversion), Tim Bray (inventor of Web), Charles Petzold (Famous Windows programmer and book writer), Sanjay Ghemat (of Google), Yuihiro Matsumoto (inventor of Ruby) etc. It also has articles from diverse domains regular expressions, version control, language development (Ruby, Python),  numerical programming, bioinformatics, Web and search, etc.

One of most interesting article is by Arun Mehta on how he developed the hardware and software so that Prof. Stephen Hawking can interact with the world. The spec given was ‘Prof. Hawking can only press one button’. This article explains in detail how they developed the actual specs from the one liner. He explains basic design models, input interface, simple typing, word prediction, scrolling/editing/searching/macros etc. This software was developed in VB. Imagine you have given this ‘one line’ spec, what you will do? How you will proceed? It’s fascinating to understand thoughts behind all these ideas and design decisions.

First article is from Kernighan about ‘regular expression’ matcher that Rob Pike wrote for book ‘Practice of Programming’. It is truly ‘Beautiful code’. Small, powerful, elegant, does its job well.  I am really tempted to show you the code.
    /* match: search for regexp anywhere in text */
    int match(char *regexp, char *text)
    {
        if (regexp[0] == '^')
            return matchhere(regexp+1, text);
        do {    /* must look even if string is empty */
            if (matchhere(regexp, text))
                return 1;
        } while (*text++ != '\0');
        return 0;
    }

    /* matchhere: search for regexp at beginning of text */
    int matchhere(char *regexp, char *text)
    {
        if (regexp[0] == '\0')
            return 1;
        if (regexp[1] == '*')
            return matchstar(regexp[0], regexp+2, text);
        if (regexp[0] == '$' && regexp[1] == '\0')
            return *text == '\0';
        if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
            return matchhere(regexp+1, text+1);
        return 0;
    }

    /* matchstar: search for c*regexp at beginning of text */
    int matchstar(int c, char *regexp, char *text)
    {
        do {    /* a * matches zero or more instances */
            if (matchhere(regexp, text))
                return 1;
        } while (*text != '\0' && (*text++ == c || c == '.'));
        return 0;
    }

Just three small functions handle following regular expression constructs
c              matches any literal character c
.               matches any single character
^             matches the beginning of the input string
$              matches the end of the input string
*             matches zero or more occurrences of the previous character

I am always fascinated by small, powerful code. In 30 lines, this is one of most powerful code that I have seen. Here is the online version of this article

Personally I also like following articles,
  1.  Subversion’s delta editor By Karl Fogel.
    It helped me in understanding how subversion works behind scene. It also helped in developing versioning/delta storage scheme for a project. 
  2.  Framework for integrated Test : Beauty through Fragility by Michael Feathers
    Here Feathers talks about design of FIT (Framework for Integrated Test) framework by Ward Cunningham. (NOTE : Ward Cunningham is inventor of Wiki). FIT framework is just 3 classes. 
  3. Distributed Programming with MapReduce by Jeffery Dean and Sanjay Ghemavat
    This article explains the concepts and infrastructure ideas that drive the Google search. Hadoop project implements these concepts and brings it to open source world. 
  4. Linux Kernel Driver Model : The benefits of working together by Greg Kroah-Hartman
    Linux operating systems runs on everything from your mobile phone (Android OS is a derivative of Linux), to desktop, to servers to supercomputers. The driver model has to support diverse hardware requirements and various memory scales.
This is a book where you go back every few months, read different articles again and gain new insights. Enjoy.

Here are some links about the book.


Wednesday, March 20, 2013

Software Performance Optimization - A Different Skill

For almost every project I worked on in last 18 years, required performance optimization. Now I have become somewhat of an expert in Performance Optimization in various domains. I have worked on optimizing performance in CAD/CAM algorithms, database queries, caching. I have considered alternative algorithms, alternative data structure usage, impact of page faults, impact of caching etc etc.

In every domain few things are different but some basics remain constant. First rule of optimization is "Don't depend on your gut feel about the location of the performance bottleneck". 99% of times Your gut feel is wrong. So you need to use tools to locate the performance bottleneck. Essentially the process boils down to 
  1. Identify appropriate tool to generate the performance data. Usually this will be a 'profiler'. But sometimes other tools are required (e.g. for analyzing database queries which are taking long time).
  2. Generate the performance data using the tool.
  3. Interpret the data and locate the performance bottleneck. This requires some practice (and guidance if available)
  4. Study the bottleneck code and find out a way to eliminate bottleneck with least amount of code changes. It is important to ensure that code changes are minimum. Large amount of code change can result in new bugs.
  5. Make code changes and test.
  6. Generate the new performance data and ensure the bottleneck is fixed. If not, revert the changes.
  7. If performance is improved keep the changes and commit it.
  8. Analyze the performance data again for the next bottleneck.
  9. Repeat the steps 3-8.
For most projects 5 to 10 times speed ups are possible. However, usually project teams find it hard to believe. Recently I worked with SigmaTEK Systems India team for improving the performance of their Tube Nesting product. Together we were able improve the performance of  their Tube Nesting product by more than 5 times.

It was a real pleasure to work with Nitin on several projects, especially related to performance development.

Nitin came on board at a typical situation, where the customer was unhappy about the speed of the algorithm, and there was lot of pressure to improve it significantly more than the current speed.

Nitin showed us how to systematically analyze code using simplest tools possible (emphasis was always on understanding, never too much on tools). His inputs and ideas on how to improve the performance, without having to compromise with the quality of the results, very extremely valuable. In addition to just code optimization using performance metrics, Nitin was very keen on evaluating the algorithm techniques as well, and provided us several alternatives right down to the core level, on alternative approaches to evaluate for performance improvement.

This experience has been a real eye opener for us, and although it sounded cliché, when Nitin mentioned the very first time, that he has been involved in several projects with optimization improvements of 5X are more, it was extremely satisfying to see that he guided us using his systematic methods and principles, to performance gains of 5X + in our project as well.



Sunday, April 22, 2012

Teaching mindmapping to 10th Standard Students of Millenium National School

I learned mindmapping about 10-12 years back from Tony Buzan's Mindmap Book. For me Mindmaps work and i have used for everything from documenting software design, planning customer visits, preparing for training sessions/presentations etc etc.  Few years back, I taught mindmapping to my daughters. My elder daughter is now a regular mindmap user and she studies all the subjects using mindmaps. 

Many years I have been thinking that mindmapping will be an excellent life skill if we teach it our students at around 8th-9th grade. So I approached my daughters' school. Millenium National School and asked if I can teach mindmapping to 10th standard students.  School's director Arpita Karakare ma'am and Radhika teacher (coordinator) , Smita teacher were very enthusiastic about the idea. (BTW, I think this enthusiasm about trying out new ideas that can potentially benefit students is the distinguishing character of Millenium school.). Arpita ma'am and few Millenium teachers were already using mindmaps. Hence the concept was not new to school. 

We decided on following format 
  1. 4 hour introductory session for each division
  2. At the end of session give 4-5 topics (from science, history/civics, maths, geography) and students have to submit these 4-5 mindmaps in about 8-10 days time.
  3. I will evaluate these mindmaps and give suggestions for improvements.
So far I have done many corporate trainings on various topics. This was my first experience of teaching students. It was GREAT experience. Students enjoyed it. Some teachers also attended sessions. 

Interestingly many 'C' Division students made really Good mindmaps. 'A'  division students had many more questions/doubts and quickly wanted to try out different ideas. Also many 'A' division students tried mindmapping topics other than the topics for experiences. During followup sessions I could see the gradual improvements in mindmaps as students practiced more. And Mindmapping does require practice. Even after 12 years of using mindmaps, I still have to revise my mindmaps multiple times to completely organize my thoughts about a subject.

I don't know how of these students will remain regular mindmap users. But even if few of them use it regularly, it will give me great satisfaction.

In fact, Arpita ma'am told me about one student. His father called her and asked her that his son is suddenly very enthusiastic study and he is drawing all these pictures/maps. And is that ok ??.  She assured him that 'yes it is ok'. Its a 'great feeling' to be a part of such transformations.

Thanks Team Millennium for the opportunity and your support. (especially Radhika teacher, Smita teacher and Arpita ma'am and all the 10th Standard Students).

As usual here are few good links about Mindmapping.
  1. Tony Buzan's Mindmap Book on Google Books
  2. Tony Buzan giving a quick introduction to Mindmaps
  3. How to mindmap a Text book 
  4. 7 mind-mapping uses for Students. 
  5. Mindmap Art - Have some great examples of mindmaps.
 Some good Mindmap Software:
  1. MindManager Easy to use and has great MS Office integration
  2. iMindmap is developed by Tony Buzan. The iMindmap mindmaps look great.
  3. XMind - Open Source available in Linux, Windows, Mac. Good software to start.
  4. FreeMind -