Archive

Archive for May, 2008

Case for E-Books on Programming Languages

May 28th, 2008

I am a big fan of books, blogs, screencasts and articles which are related to computer programming and software design. I spend almost 90% of my productive time with computers that are connected to internet.

I have noticed that buying e-books is the right way for me. This can be attributed to my way of living on the edge and the volatile nature of the latest programming languages. E-books provide an easy way to upgrade the book and not to mention greatly reduce the usage of paper (so eco friendly ;)). I know purists will hate to give up the paper book but I am not a purist.

I have recently bought couple of rails books, pickaxe (Ruby) book and Erlang book from The Pragmatic Programmers and have been very satisfied. I get regular email alerts whenever there is an update or new version of the above books and its very easy to get hold of the new copy for no extra charge. However, I am not sure whats the deal with other e-book sellers.

Other than the above there are plenty of other uses like, hyperlinked resources/references which are just a click away from the book, one can copy/paste the sample code instead of laboriously typing the same stuff, no need to context switch between computer and book, no need to switch between keyboard/mouse and pen/pencil, easy to take notes, easy to share snippets of books with friends and many more.

E-books are also advantageous to authors as they can bring the social networking aspect in editing of book. This is suitable in computer programming related books which would require lot of useful code snippets and examples. There are many people who want to contribute for this cause but are not ready to write a book! One of the recent experiences, where e-book might be the only way to go was Rails Recipes by Chad Fowler, this book as the title says is destined to constantly evolve and its not desirable for the reader to buy a copy of this book with each new recipe added or get a seperate addendum for each revision. With e-books its just a click away.

I am sure there are many out there who would want to adopt e-books. So a clear indication of when to buy an e-book (I would say always but …) is if you know or feel that a book will have more revisions in near future.

programming

Selenium RC with .Net 3.0

May 27th, 2008

Seems like a sidetrack from my work. I was helping out my wife in setting up Selenium-RC with C# client driver. However the documentation assumes everyone is using .Net 2.0. So I wanted to share what I have done to achieve the same with C# 3.0. Here are the steps,

Requirements

  1. JDK 1.5, to run selenium rc server
  2. Visual Studio 2008 (obviously)
  3. Selenium RC, and
  4. Nunit

Steps

  1. Create a sample C# class library, say SeleniumSample
  2. Change the build target framework to .Net 2.0. To do that go to Project > SeleniumSample Properties and select .Net Framework 2.0 under Target Framework drop down
  3. Remove the non .Net Framework 2.0 references, namely Linq etc.
  4. Clean and Rebuild the solution, fix the using errors
  5. Add the following class (GoogleTest) to test selenium
    using System;
    using NUnit.Framework;
    using Selenium;
     
    namespace SeleniumSample
    {
        [TestFixture]
    	public class GoogleTest
    	{
    		private ISelenium selenium;
     
    		[SetUp]
    		public void SetupTest()
    		{
    			selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com");
    			selenium.Start();
    		}
     
    		[TearDown]
    		public void TeardownTest()
    		{
    			selenium.Stop();
    		}
     
    		[Test]
    		public void GoogleSearch()
    		{
    			selenium.Open("http://www.google.com/webhp");
    			Assert.AreEqual("Google", selenium.GetTitle());
    			selenium.Type("q", "Selenium OpenQA");
    			Assert.AreEqual("Selenium OpenQA", selenium.GetValue("q"));
    			selenium.Click("btnG");
    			selenium.WaitForPageToLoad("5000");
    			Assert.IsTrue(selenium.IsTextPresent("www.openqa.org"));
    			Assert.AreEqual("Selenium OpenQA - Google Search", selenium.GetTitle());
    		}
    	}
    }
  6. Rebuild the solution
  7. Now open Nunit-Gui, Program Files > Nunit X.Y.Z > Nunit Gui (.Net 2.0)
  8. Do File > Open Project and select the SeleniumSample.dll which should be in projects bin directory
  9. Now you should see the test class GoogleTest with GoogleSearch test case under it.
  10. Open an command window and cd to directory where you downloaded and extracted selenium rc
  11. cd to selenium-server-1.0-beta-1
  12. run java -jar selenium-server.jar, check that the server is running and listening on port 4444
  13. Now back to Nunit-GUI, start the tests by clicking on run. This should launch internet explorer and run the steps/checks mentioned in the above code
  14. Feel free to add more test cases and then back in Nunit-gui do File > Reload Project and re-run the tests

Happy Testing

.net, testing

Serializing Objects in Ruby

May 24th, 2008

Simple object serialization in ruby can be done using two core ruby modules Marshal and Base64. Here is how its done

Marshalizer.rb


class Marshalizer
  def self.dump(obj)
    return Base64.encode64(Marshal.dump(obj))
  end

  def self.load(str)
    return Marshal.load(Base64.decode64(str))
  end
end

Sample Usage


class SampleObj
  attr_accessor :title, :description
end

class MarshalizerTest < Test::Unit::TestCase
  def test_serialization
    article = SampleObj.new
    article.description = "Test name"
    article.title = "Test title with 's"
    serialzied_article = Marshalizer.dump(article)
    assert_not_nil serialzied_article, "serialized article should not be nil"
    puts serialzied_article
    new_article = Marshalizer.load(serialzied_article)
    assert new_article
    puts new_article.inspect
    assert_not_nil new_article, "loaded article should not be nil"
    assert_instance_of SampleObj, new_article, "new_article should be of type article"
    assert_equal(article.title, new_article.title)
  end
end

ruby

Ruby Mobile Browser Detector (port of JMBD)

May 24th, 2008

I have successfully ported Java Mobile Browser Detector to ruby. I am sure there are other existing utilities to achieve this but I found this to more simpler to understand and use. It works well with my mobile site Fundu.mobi developed on rails. I though it would be useful for someone so here is the code rmbd.zip.

Here is how you can use it in any controller


class SampleController < ApplicationController
    def index
      device_config = DeviceConfigDetector.detect_capabilities_for_request(request);
      logger.debug("device config = #{device_config.inspect}")
    end
end

ruby ,

Emergent Design

May 23rd, 2008

Today I happened to attend a talk by Scott Bain, author of Emergent Design: The Evolutionary Nature of Professional Software Development . And incidentally today is my first day blogging. So I took the liberty of using the theme of book as concept of my blog.

Talk was mostly based on principles and practices which any sane developer would atleast try to follow. But the emphasis of the talk was to summarize the above book and give some concise examples on how to achieve the result in a much easier way. I felt the concepts a bit natural for me now because of my slow transition into ROR (a.k.a Ruby on Rails) which you would see my future posts will mostly focus on. ROR drives developers to follow the famous (or infamous with merb lurking around) Convention over Configuration. This means all the developers using rails follow similar practices making the process of sharing knowledge easier. And not to mention hacking around a code, expecting a particular behavior from the code and many more. Just to reiterate rails has done an excellent job in establishing concrete practices, thanks to DHH and rails core for this.

Coming back to the talk, the entire message was focussed on following Open-Closed Principle of design. This according to the author can be achieved by following,

  1. Pattern-Oriented software development: This does not mean complicating code by designing it exactly to the pattern but to understand why the pattern was created in first place.
  2. Test Driven Development (TDD): All folks should know about this by now. To learn more go here.

Though the talk sounded to me somewhat mundane, I am excited to know how deep the book goes and how it would change my perspective. But before I buy I would love some reviews ;). So the lesson I learn is follow patterns to the extent they need to be followed, follow TDD in a more disciplined way and constantly evolve code. As Scott mentioned this can be done by following some simple steps like,

  1. Refactoring code to fit good practices and not change existing behavior (am I defining Refactoring here?)
  2. Add documentation
  3. Change method/variable names to intended meaningful name

Software Design ,