Virtues of a programmer

  • Laziness – The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful, and document what you wrote so you don’t have to answer so many questions about it. Hence, the first great virtue of a programmer. Also hence, this book. See also impatience and hubris.
  • Impatience – The anger you feel when the computer is being lazy. This makes you write programs that don’t just react to your needs, but actually anticipate them. Or at least pretend to. Hence, the second great virtue of a programmer. See also laziness and hubris.
  • Hubris – Excessive pride, the sort of thing Zeus zaps you for. Also the quality that makes you write (and maintain) programs that other people won’t want to say bad things about. Hence, the third great virtue of a programmer. See also laziness and impatience.

* Taken from the book Programming Perl

Posted in Programming | Tagged , | Leave a comment

Facebook, Facebooker gem and ssh-tunnel

Well, it was long hours of endless frustration and little fun, I tell you. That is how I spent most of my saturday trying to follow the instructions to create a ssh-tunnel so that I could test facebook application as I developed it. Putting an application up on facebook is a walk in the park. You create a new application, give it a callback url, where it can find what to display in your application and you are done. As far as your callback url is legitimate your application will work like a charm. If you are interested, please go ahead and check the request-response artchitecture.

I am using the facebooker gem which gives a you a lot of functionality out of the box and one thing it does is gives you rake tasks to start a ssh-tunnel. The purpose of ssh-tunnel is simple port-forwarding. So, any requests to a publicaly available domain at a certain port should be forwarded to the local server where I am doing facebook application development. The latest version of the gem sits on github and you can install it as a plugin, just do:

script/plugin install git://github.com/mmangino/facebooker.git

There are several tutorials that describe how you can set it up, just google it.

And I will go straight into the ssh-tunnel part of the problem so I assume that you have facebooker gem installed and you have made necessary changes to the facebooker.yml file by adding your public host name and ports where you want to achieve port forwarding. For example: any publicly available domain that you have ssh access to can be used as a port-forwarder. For the sake of this post let us say:

public_host :andhapp.com
remote_port: 4007
username :andhapp
localhost :localhost
local_port :3000

As per the above details, when I run the facebooker rake task to start the tunnel I will basically be doing this: Any requests to andhapp.com on port 4007 should be forwarded to my localhost on port 3000 (which is where your dev facebook app is running).

Once it is all set-up you can use facebooker’s nifty rake command to start the tunnel, like this:

rake facebooker:tunnel:start

This would prompt you for your password for sshing to your public host. To check the ssh-tunnel status, just do:

rake facebooker:tunnel:status

If this command comes back with “Seems, ok” you know the ssh-tunnel is up and fine. But when you access http://andhapp.com:4007 (or whatever domain you have used) it just gives you a browser error – “the connection could not be made”. It took me a long time to figure out what exactly the problem is. The ssh-tunnel is fine its the way ssh is configured. To fix the issue, just uncomment the line:

GatewayPorts no

in your ssh config file on your server, most probably found in: /etc/ssh/sshd_config and change it to:

GatewayPorts yes

Restart ssh, and restart the tunnel and it will start working. What the hell? Why do we need to do that for? Here’s an explanation.

Posted in Programming, Rails | Tagged , , | Leave a comment

BitBucket

I recently read this article comparing BitBucket and GitHub and did mention in in a previous post on Git. Apart from well defined differences like BitBucket is a social repository for Mercurial writting in Python whereas GitHub is obviously for Git written in Ruby with bits written in Erlang.

So, I decided to try BitBucket out and I must say although the UI is quite similar to GitHub but it is still not as intuitive and can be improved massively in terms of overall navigation and organisation. But, hold your thoughts, if you are going to write Mercurial off on the basis of BitBucket then atleast check this article out by Geoffrey Grosenbach before you do so.

Posted in Git, Mercurial | Tagged , , , | 1 Comment

IRB on roids

Any ruby developer knows about the interactive ruby shell or in short irb and I am quite sure they use it everyday for quickly testing little scripts out. And Rails offers its own console which is basically irb with rails framework loaded with a valid database connection for performing quick tests. Now, in order to make it easy on your eyes one can change the default output of activerecords in console. But the question remains how? Well, I found a gem called hirb which simply does that for you. It is quite easy to install and configure and it improves the overall formatting by leaps and bounds.

Posted in Rails, ruby | Tagged , | Leave a comment

rspec_rails is awesome

It is only when one has to muck about with other mocking libraries one realises how cool rspec_rails is? It bascially provides you so much “rails” functionality out of the box that mocking and stubbing feels like a walk in the park. I have been trying some other mocking libraries and just can’t get my head around the whole mocking and stubbing thing. Anyways, I will keep trying and hopefully post.

Posted in Rails, rspec | Tagged , | Leave a comment

Using factory_girl with cucumber

I have moved away from fixtures and there is only one reason for it. factory_girl is an awesome piece of functionality the gives one the flexibility to easily define the data to use in tests and for populating the database with dummy data. However, I must add that I am also using Populator and Faker gems along with factory_girl to populate the database with dummy data but I will discuss it some other day.

Now, cucumber is the BDD library that allows one to write the features first. The features are in plain English and any client-manager can understand them. However, there was quite a debate few weeks ago over this whole issue of – “Who lets their client managers write stories?”. Follow the link if you are interested and read the comments. Anyways, I have been trying this out recently and I must say after an initial steep learning curve I can see the benefits. You just define the functionality from a user’s perspective and it makes the development a tad longer yet easier.

Now, using factory_girl from within rspec is easy but how can I make sure that the data is there before I run my cucumber features. Because in order to test a feature like a “admin should be able to delete a user” there must be a user in the system otherwise how could it be tested? Well, here’s the solution to it.

Problem: Create test data before the cucumber features.

In order to create an instance of a model object we can use one of the following depending on the situation:

Factory(:model_factory)
Factory.build(:model_factory)

Now, to make sure we create all the factories that we have defined already we need to get a list of all the defined factories somehow. And we need this list before we run the cucumber features. So, we simply add the following code to env.rb (cucumber’s config file) and voila!

Before do
  require 'factory_girl'
  Dir.glob(File.join(File.dirname(__FILE__), '../../spec/factories/*.rb')).each {|f| require f }
  Factory.factories.keys.each {|factory| Factory(factory) }
end

You would ask why does this work? Well, let us see what does the define method do. The same method that we use to define a factory. Here’s the code inside the define method*:

# File 'lib/factory_girl/factory.rb', line 48

def self.define (name, options = {})
  instance = Factory.new(name, options)
  yield(instance)
  if parent = options.delete(:parent)
    instance.inherit_from(Factory.factory_by_name(parent))
  end
  self.factories[instance.factory_name] = instance
end

The last line in the method is basically adding the name of the created factory to the factories class variable and that’s the variable I use to get hold of these defined factories.

* – Code taken directly from the latest rdocs

Update(10 October, 2010):
I think factory_girl has since changed its internal code. I am assuming it has since I tried using this code with Rails3 and cucumber-0.9.2 and cucumber-rails-0.3.2, but the features did not run at all. So, now if you wish to use factory_girl with cucumber add this in env.rb file instead:

Before do
  require 'factory_girl'
  Dir.glob(File.join(File.dirname(__FILE__), '../../spec/factories/*.rb')).each {|f| require f }
end

So, what I am simply doing now is building the factories but not actually creating them and now you can create all your factories in the step_definition files. I think this is a better way of using factory_girl with cucumber.

Posted in BDD, cucumber | Tagged , , , , | Leave a comment

Google Wave and Google Groups

Isn’t Google Wave an extended, better designed and developed version of Google Groups and Google will slowly and shrewdly phase it out? The reason I say that is becuase I have recently signed up for RubyLearning Wave Group and it gives me a feeling that it has been built on top of the same concept as Google groups. How else can one explain the lack of spam management and clunky interface on Google groups?

Now, we all know that Google groups is terrible considering the Web 2.0 standards atleast. I know you would doubt my credibility and therfore you should read this article from John Resig (the Javascript Ninja and the creator of JQuery). I have been managing the Ruby Shoes group for quite some time now and what surprises me is that Google somehow has forgotten to implement the Gmail’s “spam controlling” strategy on Google groups.

Anyways, we will see what happens in the future but as it stands Ruby Shoes group has since moved to librelist and it has worked out well. Lifehacker has done a really nice post on some Google Wave “not so obvious” features.

Posted in Google | Tagged , , , | Leave a comment

Google interview questions

I got hold of this great article that lists a lot of questions asked in Google interviews and honestly speaking I do not think with my present knowledge I will ever pass an interview there. I guess if I knew a bit more about algorithms I might have a chance. Therefore, I am going to start reading Donald Knuth’s – The Art of Computer Programming – Sorting and Searching book. I will see how it goes. Apparently, it is too complicated to understand and make sense of.

Nah I am just kidding who reads books now days. It is all about finding some tutorials on the internet and hacking things together. Well, I had the same opinion few months ago and one day I started reading a BDD book and really it makes a huge difference. And I am talking about actually reading and understanding and running the code and amending and making changes to the code and ensure you understand the underlying behaviour of a language or a library. Anyways, I am going off topic now.

Posted in Book | Tagged | Leave a comment

Fuzzing

Read about this technique of testing called Fuzz testing or Fuzzing whilst reading Zed Shaw’s essay on Ragel, the state machine compiler. It is quite interesting stuff really once you get a hang of it.

Posted in Testing | Tagged | Leave a comment

Some useful git commands

gitHub is an amazing piece of functionality. The user interface is so amazing that BitBucket simply ripped it off. Yes, without any regrets. I mean people are bound to notice that sort of stuff but still these things happen. Anyways, I came across some really handy git commands and would like to share them and just make a record of it in case I need to refer to them.

  1. git daemon
  2. I found out about git-daemon through a blog post by the gitHub guys. Apparently, they have rewritten the daemon in Erlang. So, as per the documentation git-daemon basically starts up a server in your repository and then you can use it for doing commands like git-clone and git-fetch. It gives a read-only access as default.

    Now, you would say why the hell do I need to run my own service to serve the code. Why do you think I am paying gitHub for? Good question but let us imagine a scenario. You are about to push to staging (I dare not say live because you don’t push your “dodgy” untested code to live servers) for quality assessment team and gitHub goes down (it has an amazing uptime but you never know) and you need to pass your code to the “Lead developer” sitting in a different physical location. git-daemon to the rescue. The only issue would be to fix firewall to allow connections on 9418(default port).

    How do I do it?

    1) touch decoct/git-daemon-export-ok
    2) git-deamon --base-path=. --verbose
    

    The first line creates a file in the repository that you want to serve and its presence tells git-daemon that it is ok to serve files in these repository. git-daemon would refuse any requests in its absence.

    The second line sets the base-path to the current directory and now I can basically serve any git-repository in the current directory that has git-daemon-export-ok file in it.

  3. git instaweb
  4. As the name suggests, creates a web interface so that one can browse the local repository. Just do:

    git instaweb --httpd=webrick
    

    Once you run the command it will open up the broser window and you can basically look at all the information about your repository. Simply amazing.

  5. git shortlog
  6. So, I want to look at a consolidated report of the number of commits by my team. git shortlog to the rescue and here’s what you need to do:

    git shortlog -n  -s -b master
    

    Run this command inside your repository and you would be amazed by what you see. I know you can probably do all this stuff in svn but I am just excited to find the functionality in git.

    This is just a teaser. You should go and explore the commands further. I know I would because I am going to write the git-daemon in Scala. And there is a nice article on all this by gitHub guys.

Posted in Git | Tagged | Leave a comment