Lenovo Thinkpad T400

After a lot of research to find a good programmer’s laptop, I finally bought a new Lenovo Thinkpad T400 to give a boost to my programming efforts. This is my first personal laptop. Although I have still not fully recovered from the hole it burnt in my pocket, I am very excited about the flexibility it provides. Last week, I took it to the National library and spent some quality time there practicing programming. Just like any creative pursuit, programming requires its practitioners to work in an environment that allows their creative juices to flow. As for me, the ideal environment for hacking is a busy (but not very noisy) public place in the outdoor. Surprising though it may sound, I really do feel most creative when there are lots of people around me. So my plan is to visit all the cafes in the city and make a list of preferred locations ideal for working.

In order to discipline myself into doing some real work and not simply wasting time browsing the net, I have purposely not enabled online mobile broadband. This compels me to plan my work in advance since I am forced to download all the reading material, software, source code etc. that I need before stepping out of the house.

Advertisements

Traits of a good programmer

In his latest blog, Peter Bohm has written a review of Chad Fowler’s “The Passionate Programmer” and has basically summarized a few qualities that a programmer should have. I think that analytical and programming skills apart, it is the amount of “passion” a programmer has that makes him/her a good programmer.

One of Peter’s arguments is about how good programmers never stop to learn. I couldn’t agree more. If there is a one key difference I have seen between good and average programmers, it’s their passion for learning. Good programmers see learning as an investment and spend a lot of time upgrading their skills and improving their knowledge. They read, browse, blog, network, join forums, attend conferences and above all get into the trenches try out new things whenever they can.

Good developers hate bad code. I was reading “Secrets of the Rockstar programmers: riding the IT crest” by Ed Burns. This book is a collection of interviews of some of the “celebrities” from the IT world. One of the traits that sprung up as a common characteristic among most programmers interviewed in the book was their passion for continuously improving the systems they were working on. In the interview with Kohsuke Kawaguchi, the creator of continuous integration tool Hudson, he says that he wrote Hudson because he wasn’t happy with CruiseControl (and other CI tools). Good programmers will feel very uncomfortable on seeing poor code or existing bugs and would want to fix them. They will cringe every time they are forced to put in a tactical solution/bug fix for the benefit of time and will always go back and put a better fix whenever they can.

In my opinion, good programmers are lazy. They hate doing manual tasks and therefore invent creative ways to automate mundane jobs. One of my friends recently spent several hours converting a manual build task into a fully automated one-click build and managed to reduce her effort from several hours per build to almost zero. She was almost delirious with joy on seeing all the time she saved. As Yukihiro Matsumoto, the creator of Ruby once said, “Be lazy. Machines should serve human being. Often programmers serve machines unconsciously. Let machines serve you. Do everything you can to make you lazy”.

I have also often observed that many, if not all, smart programmers are tool junkies. Nothing excites them more than tinkering with new tools, installing new plugins or customizing their development environments. I am not sure if there is a pattern here but the child like pleasure we programmers derive from these activities make our work all the more interesting.

Another interesting characteristic of good developers is that they are not scared to step out of their comfort zones. For example, they share their code with others and welcome feedback. It takes a lot of courage to show your code to the rest of the world. Good programmers work hard to swallow their pride and overcome their fear and shyness of letting other people review their work. For the same reason, they enjoy company of programmers who are smarter than them. This gives them an opportunity to observe and learn from others. Joel Spolsky once said in his podcast, “if you’re not doing a few things where you’re failing a little, you’re probably not trying hard enough. I say if everyone likes you, you probably aren’t doing anything interesting.”

Finally, the one quality that I hold in very high esteem is humility. Good programmers are humble. They don’t mind admitting that they don’t know something. While it is easier said than done, I have seen that the biggest disservice we do to ourselves is by being egoistic and snobbish about our knowledge and programming skills. You end up losing by shutting off other people if you are continuously trying to prove the superiority of your programming skills to others. Smart programmers don’t do that. They know that there is nothing wrong in saying “I don’t know”, nor do they harshly judge another team mate who says that. There is a lot that can be learnt from each other if only we let our guards down and stop being defensive about our skills or lack thereof.

Tagged ,

Main method not supported in scala companion object

I was working on some practice problems from the Ninety-Nine Scala Problems website when I stumbled across an interesting behavior.

I wrote a dummy class and a main method in its companion object.

package arithmetic {

    class S99Int(val start: Int) {
      import S99Int._

      def hello = println("hello")
    }

    object S99Int {

      def main(args: Array[String]) :Unit = {
          println("hello world")
      }

    }

}

To my surprise, I got a NoSuchMethodError: main from the compiler.

Turns out that this is not supported in Scala (as of now). Companion objects can’t have a main() method. There are some esoteric explanations available on the Scala mailing list for this behavior.

Moving to WordPress

WordPress has built in code formatting (SyntaxHighlighter) so no more manual formatting of code. My new blog is here

.

Scala by example – Sorting

Here’s an attempt at learning Scala using some basic algorithms. I do realize that these examples look and “feel” like Java and do not use the full power of Scala’s more expressive syntax or its functional approach. This is just my humble attempt at learning a new language.

I use the scala plugin for Netbeans 6.7.

Bubble Sort

package sorts

class Sort {


    def bubblesort(lst : Array[Int]) : Array[Int] =
    {
        val list : Array[Int] = new Array[Int](lst.length)
        Array.copy(lst, 0, list, 0, lst.length)
        var swapped : boolean = false
        do
        {
            swapped = false
            for (i <- 0 to (list.length - 2))
            {
                if (list(i) > list(i+1))
                {
                    swapWithNext(i, list)
                    swapped = true
                }
            }
        } while (swapped)

        return list
    }

    def swapWithNext(i : Int, list:Array[Int]) : Unit =
    {
        val temp = list(i)
        list(i) = list(i+1)
        list(i+1) = temp
    }

Selection Sort


    def selectionsort(lst : Array[Int]) : Array[Int] =
    {
        val list : Array[Int] = new Array[Int](lst.length)
        Array.copy(lst, 0, list, 0, lst.length)

        for (i <- 0 until (list.length -1))
        {
            var min = i
            //find min
            for(j <- (i+1) until list.length)
            {
                if (list(j) < list(min))
                  min = j
            }

            //swap ith number with that at min position
            if (i != min)
            {
                val swap = list(i);
                list(i) = list(min);
                list(min) = swap;
            }
        }
        return list
    }


&#91;/sourcecode&#93;

<strong>Insertion Sort</strong>



    def insertionsort(lst : Array[Int]) : Array[Int] =
    {
        val list : Array[Int] = new Array[Int](lst.length)
        Array.copy(lst, 0, list, 0, lst.length)

        list.foreach(a => print(a + ","))
        println()
        for (i <- 1 to list.length - 1)
        {
            //pick arbit minimum value
            val value = list(i)

            //iterate through the rest of the "sorted" array
            var j = i-1
            while (j >= 0 && list(j) > value)
            {
                list(j+1) = list(j)
                j = j-1
            }
            //replace
            list(j+1) = value
            list.foreach(a => print(a + ","))
            println()
        }
        return list
    }


Quick Sort


    def quicksort(lst : Array[Int]) : Array[Int] =
    {
        var list = lst.elements.toList
        
        list = quicksortBasic(list);
        return list.toArray

    }

    private def quicksortBasic(list: List[Int]): List[Int] =
    {
        var less:List[Int] = List()
        var greater:List[Int] = List()
        if (list.length <= 1)
            return list

        val index = list.length / 2;
        val pivot = list(index)

        for (i <- 0 to (list.length -1))
        {
            if (list(i) < pivot)
              less += list(i)
            else if (list(i) > pivot)
              greater += list(i)
        }

        return (quicksortBasic(less) + pivot ++ quicksortBasic(greater))
    }


}

Here’s the Main object that calls the sort methods

object Main {

    /**
     * @param args the command line arguments
     */
    def main(args: Array[String]) :Unit = {
        val list = new Array[Int](5)
        list(0) = 10
        list(1) = 5
        list(2) = 25
        list(3) = 0
        list(4) = 22

        println("bubble")
        new Sort().bubblesort(list).foreach(println);

        println("selection")
        new Sort().selectionsort(list).foreach(println);

        println("insertion")
        new Sort().insertionsort(list).foreach(println);


        println("quicksort")
        new Sort().quicksort(list).foreach(println);


        println("end")

    }

}

I’ll revisit these examples and make them more “scala” like in some time.

Hello world!

Moving from BlogSpot to WordPress for its advanced formatting features. In a few days, I’ll move all my blog posts from here to this place.

Random thoughts


Slack

Reading a book called Slack by Tom DeMarco.

Very large text files processing

If you are dealing with very large text files, i.e. more than 500MB in size, here are a few tips that might help

  1. On windows, use Textpad for viewing/editing files. It handles large files very well. Alternatively you can use unix utilities or cygwin if you are working on windows.
  2. Java doesn’t handle large files very well. Consider using Perl or unix shell script. You will be amazed at the performance gains.
  3. If you need to save this to a database, consider a direct bulk copy using your database’s load utility e.g. sqlldr (oracle) or bcp(sybase, ms-sql).

Procrastinators logic: Cleaning your apartment is O(1) complexity

N being the number of days since you last cleaned your apartment, for small N, the time taken, t, to clean your apartment will not vary much over N.

This makes apartment cleaning an O(1) complexity algorithm.

.