Thursday, June 26, 2014

How to use Ruby 2.1 as the default Ruby in Textmate

1. $ rvm get head
2. $ which rvm-auto-ruby | pbcopy
3. Go to Textmate preferences and Variables tab, add a new variable by clicking the + button. The name of the variable is TM_RUBY and copy the path to the ruby from step 2 as the value.

Wednesday, June 25, 2014

Why using Interactor Gem is a Very Bad Idea

Using interactor gem is a horrible idea. Here are the reasons:

1. It results in anemic classes, classes that don't have any behavior.

Their only purpose is to organize. You can organize using a DSL, something like rspec syntax: organize "Use Case" { specify the sequence of actions to take }. This does not have to be a class, the DSL is just a way wire different classes together.

2. The hash that gets passed around is nothing but a glorified global variable.

If you apply Domain Driven Design, you will use application specific data types instead of built-in data structures. This makes the concepts of your domain explicit. You can achieve 'Ubiquitous Language' in your team.

3. This results in "Hash Oriented Programming" not OO.

4. Failures are silent, which means there is no clear transparency into the system.

5. Class names are verbs. Are you kidding me? Why do you want to ignore the basics of good OO design?

What is the alternative then? Learn about Hexagonal Architecture and Domain Driven Design. Apply those principles religiously. When you apply these good design principles, your use cases become the core of your application which is not dependent on any technology. They become testable and compose able. It's like legos that gives you basic building blocks that can be combined in different ways to be used in different external and internal adapters.

Creating Paper Prototype to Design a Web Page

If you are getting lost in details and overwhelmed by the amount of data you have to deal with, this post will bring some sanity to your webpage design process.

Tools Required

Wall pads, scissors, colored pens, tape

What to Do

1. Buy a Post-it-Self-Stick-Wall-Pads from any office supply store.
2. Get all of your notes and lay them out into the big ass paper.
3. Arrange and categorize the notes into sections.


Focus on structure and copy existing design of a website that you like. In this phase you only worry about where different elements go in the layout. For instance, where does the screenshot of a feature go? Where does the headline and paragraphs go?

You can take screenshots of a website that you like and use Skitch to label different elements on the page. These labels could be just numbers. You can also blur or strike-out elements you don't want in your design in this phase.

You have to let your structure evolve over time by letting it marinate. Post it on the wall where you can look at it everyday and make quick corrections using red pen, using small sticky notes and pasting over the existing structure to note down some notes for yourself. Things does not have to be perfect the first time. The draft version can be approximate. It will be refined by revising it over a period of one to two weeks.


Write down the content is smaller size paper and you can start pasting them over the existing structure.


In this phase you focus on the font color, font size, font style, background grid colors etc. You make it more real.


This allows you to reduce confusion and clarify ideas before you can handover your design to a graphic designer. You can get a PSD file and hand it over to a front-end developer to create the HTML and CSS for the web page.

Saturday, June 21, 2014

Godel : Consistency or Complete

Less software as a design technique, not a goal. Thinking about decisions as potential energy vs kinetic energy. Decisions take energy and creates stress. It uses mental processing power.

Defer decision as much as possible

1. Estimating work.
2. Pure development time vs Fighting fires
3. Premature speculation (promises)
4. Requirement vs Schecule (out of sync)
5. Not planning for change.

Wednesday, June 11, 2014

Hello Docker 1.0

1. Download boot2docker osx-installer
2. Install it.
3. Check version
     $ docker -v
     Docker version 1.0.0, build 63fe64c
4. Upgrade VM and start docker
$ boot2docker stop
$ boot2docker download
$ boot2docker start

  $ docker run ubuntu echo hello world

 Unable to find image 'ubuntu' locally
 Pulling repository ubuntu
 ad892dd21d60: Download complete
 511136ea3c5a: Download complete
 e465fff03bce: Download complete
 23f361102fae: Download complete
 9db365ecbcbb: Download complete
 hello world

 $ docker run ubuntu echo hello world
  hello world

Sunday, June 08, 2014

Ruby Science Code Review

This article is a discussion of the chapter on Replace Conditional with Null Object. Null object does not solve any problems in most cases. For instance here is the sample code from the book:

if user.nil?

If I cannot use Null Object (which creates unnecessary small objects). What is the right way to fix this? You need to apply the basics of Object Oriented Programming.  Ask yourself:

  • What does it mean to have a user variable with nil value?
  • If user does not exist in our system what do you want to display instead of their name? 
  • Is blank value acceptable in this case?

 If the above code snippet is part of a method, then the precondition for the method is that user object cannot be nil. It is a contract between the client and the API. As l long as I provide a valid user the method will be able to do it's job (fulfill the contract in Design by Contract terminology) and provide me the name. It's the responsibility of the client code to provide the method a valid user. In this case the solution would be:

Client Code

if user.nil?
  'User not found'


def user_name_for(user)

Ideally I would look for the ActiveRecord call that throws an exception instead of returning nil for a record that is not found. I would handle the ActiveRecord::NotFound exception and translate it to business specific exception like MyApp::UserNotFound class that takes the message indicating the context of the application as the exception message. So there would be no nil check required in this case.

Sunday, June 01, 2014

Ruby Science Book Notes

Code Reviews

1. Use git diff and --patch to examine code before commit.
2. Push your feature branch and invite your teammates to review the changes via git diff origin/master..HEAD
3. Share your best refactoring ideas with them.
4. Refactor for readability first.


1. Flog : Detect complexity

Example :
$ flog app lib

flog score of 10 or higher : Method may be too long
A class with a flog score of 50 is a large class that might need refactoring.

$ flog -a app/models/question.rb

2. Flay : Find duplication
3. Reek : Find bad code
4. Churn : Find files with high churn rate
5. MetricFu : Analyze application

Take a look at Code Climate.