I'm all about Ember.js recently

Now That’s What I Call Web2.0: Ubiquity

I’ve stumbled upon a great Firefox plugin, Ubiquity. It basically accesses several web services (GoogleMaps, Gmail, Flickr, etc.) via its own panel and with its own commands so that common online tasks (e.g looking up something on Wikipedia you have just found on a page) become amazingly simple. I was really stunned by how much this plugin is capable of doing after watching the briefing video and going through the tutorial. And that does not stop there: power users can add their own commands to extend the already vast capabilities of the plugin. It’s been a long time I’ve been so excited about a “web 2.0” tool.

Here is the blog post about Ubiquity on Aza Raskin’s blog who is the main developer of the plugin.

Note: If you use a Mac, you’ll need Growl for Ubiquity to display its command panel.

Can Less Work Be More?

I saw the keynote speech David Heinemeier Hansson gave at this year’s Railsconf in Portland and I liked it a lot. I really like the idea of following the pattern of “Less software is more” to the work we do each day. In his speech he says developers should spend one day a week discovering a subject of interest, writing code for a pet project, contributing to an open source project, etc. Basically anything but doing the work they do on the other four days of the week. This “day-off” raises the developer’s productivity so it is a net gain for the main work he is involved with, too.

I tend to agree. However, in this post I want to elaborate rather what ‘be more’ means in both the ‘less software is more’ and the ‘less work is more’ phrases. Less software means less lines of code to achieve the same tasks. Less work means less time spent working producing code to achieve the same tasks. There are two underlying concepts here that make this possible: abstraction and productivity. More precisely: the ideas in your (hacker) head and the tools that allow you to implement these ideas in abstract, high-level terms and thus raise your productivity.

When you write a program, there are basically two phases of producing code that solves a problem: first, you think about how to solve it and second, you convert the ideas into code. There is always a gap between the ideas and the code you write and that the computer comprehends. The more high-level the language is, the smaller the gap that needs to be bridged. But there is a backward effect, too. Usually when you are in the first phase you already think in terms of the second. The expressive tools the language provides have a strong effect on how you think of the problem (generally you know what language your solution will be implemented in). And that is bad because it dumbs down your thinking before you get to the point where you should consider the possibilities of the language.

I tend to think less of the language specifics in the first phase if I know the language provides all the high-level tools and provides them neatly. “For” loops skews your thinking more than -all kinds of- iterators. If I know functions are first-class objects and metaprogramming is available I worry less about how (or whether!) my abstract solution, only existing on the plane of ideas can be converted to functioning code.

So, back to where I started, your choice of the language counts and it counts a lot. It can help you be more productive in several ways, not just the fewer-lines-of-code-to-do-the-same-thing way but also the minimizing-the-skewing-of-your-thinking way. However, a high-level language is no guarantee to achieve that. One can churn out C-like code in Ruby, it is just easier to realize you are not using the full arsenal. And that’s where it really kicks in: if you know you can basically do anything painlessly you will dare to think big, your ideas can get off the ground. Consequently they will be abstract enough for you to be more productive.

So armed with daringly abstract ideas and tools that make it possible to put them into code, one sets out to conquer the world. The limit to the amount of code generated is the number of hours one can spend each day programming. After about ~8 hours of work is it worth to put in some more time to produce more code? Probably not. Our thinking breaks down pretty fast after that. I have experienced on several occasions that I could do in 10 minutes the next morning what I could not do in 2 hours the evening before. This is not about dropping the keyboard when the clock strikes 5pm, of course. It is about planning for measurable progress without too much overtime.

I think David is right when asserting that replenishing your mind so it can come up with constructive ideas is equally important. I am sure there are numerous ways to attain that. Some of them are constructive activities in themselves (David’s example was playing the banjo), some may just give you peace (e.g friends, family) and some may simply drift your thoughts away from the very problem you are working on (e.g playing soccer) and help you that way.

So what do you think about all that? Can low-level languages still have advantages over high-level ones in certain situations? Do you think the language your are programming in can have an effect on your problem-solving thoughts? What activities switch you off in the most “productive” way? What other factors have an effect on your work? (of course your working environment is very relevant). Are naps worth it? When would the official Railsconf’08 videos be online? :)

First Mexperiences

I recently wrote about me getting a Mac, the reasons I made the switch and how I really like it. I would now like to add nuances to this overall picture. What is really cool? What is hard to get used to? Which are the things I could not find how to achieve on the Mac?

Non-keyboard input

I think I am one of the few who prefers the trackball (Thinkpad-style) to trackpads. For me, trackpads are clumsy, it is hard to position the cursor correctly, they make an incomfortable sound and I often click inadvertently by pushing too hard (but not hard at all). So I had thought I’d get a mouse but then I found out the usb ports are located on the left hand side of the Macbook which does not seem to logical unless Apple specifically targeted left-handed people. Or does everyone already have cordless mice?

Anyway, I gave the trackpad a try and I was thoroughly satisfied. The trademark two-finger scrolling works miracle, clicking is silent and the trackpad is wide enough to be able to lay two-fingers on the pad and click with my thumb which serves as a right-click. Absolutely phenomenal. My plans to acquire a cordless mouse have been postponed. (Even more so since I learned from a colleague that to really get the best out of the mouse you need to purchase a driver. I can imagine paying for software, but for a mouse driver? that seems harsh.)

Managing windows and menus

Having said all about non-keyboard input I always try to minimize the time I have to lift my hands from the keyboard in all applications. Switching between applications is easy with cmd+tab and I also learned the cmd+` and cmd+~ which switches between windows of the same application and is very useful. Closing an application with cmd+q and a tab/window with cmd+w also come in handy and was easy to learn. However, one thing which I miss badly is a key combination to just go to the menu and browse there (e.g alt+f1 in gnome). I mean there are several combinations to activate certain menu items but if I do not want to learn a few dozens of them per application it seems a good idea to me to have just one and then be able to access everything. Or maybe there is, waiting for me to discover it.

Command line boosters

Another feature I adore in the terminal is to be able to quickly navigate. Go to the start, to the end, skip a word forwards, backwards, delete a word, etc. One extra advantage of my favorite text editor, emacs, is that its combinations coincide with those used on the command line, which is fantastic. After a while, I learned that they can all be invoked in Mac terminals, too, it is just that they seem less logical too me. For example , Esc-D is to delete from the cursor to the end of the word, why is Ctrl-W Delete the word to the left of the cursor? (found here). Also, Esc is too far away to reach comfortably. I know iTerm can be programmed to respond to any key combination, so I might do some tinkering here to have a more consistent interface.

Installing software

Getting applications installed on your computer is quite easy with the disk image installers or one can always compile from source the advantages of which are described in this excellent article. I have to tell that the provided applications are really top notch like iTunes or iPhoto, and I haven’t even had the time to try all of them (iMovie, Garage Band, etc.). One caveat is that some applications, like iPhoto treats your data as one big library file so you need to export particular files if you need them outside of the application (how do you upload individual photos to a photo company’s site to have them developed? how do you upload an image to a photo-sharing website? you’ll need plugins or you have to export them thus duplicating the photos and wasting space). This surely suits ordinary users but advanced, tech-savvy users don’t like this kind of obfuscation, or at least I don’t.

A big pain for me is the presence of spaces in directory names. It may be an old habit to automatically evade them but it looks amateurish to me. Which Mac (and OS/X) is certainly not. It is a great machine (and operating system) to have.

Post scriptum. I am most interested to see what Fernando has to say about his first steps with his Mac.

Update I realized the iPhoto Library file does contain the actual photos although in a shrunk version. You can’t see this in the Finder, you have to use the command line (which is ok since that can be considered as a habit that distinguishes a normal user from a power user). To be able to burn the photos to a DVD or upload them somewhere, you still have to export them and thus have your photos duplicated (even if only for the time of creating the CD/uploading the photos).

Confession of a Conversion

My company laptop has gotten old and weary. So old that I could not even find a replacement battery and some memory for it. I could, but for half a price of a shiny new notebook that is three times stronger. I liked my old laptop, but not that much. So I went looking for a new one. I was hesitating between a Thinkpad and an ASUS since I was pretty content with both of them but the faint voice was already whispering into my ear: “get a Mac, get a Mac”.

I’ll admit the sysadmin side of things is not my favorite part of working with a computer. I have nothing against modifying configuration files, installing software from the command line and the like but I do feel like I am wasting my time when trying to get Skype voice to work for several hours to no avail, make my laptop “soft-hibernate” when I close the lid (I got that one, finally) or set up the wifi hotspot selector icon. I know it is just a matter of time and perseverance and I like the idea that you can get it to work if you finally understand what needs to be tweaked and how. (In fact, most of the time you don’t. You just find the answer by searching on Google, copy-paste something you don’t understand and eureka, it works.). It is just that my priorities lie elsewhere. It has been said a million times but it is still true: time is too precious to spend on anything but the most important things. For me, these are writing code, learning software, discovering new tools and technologies.

There was something more in favor of a Mac: its design. I don’t primarily mean the physical design of the laptop though in my opinion it is the best looking one there is but how everything works so ingeniously and smoothly. Everything is pre-installed so I basically only had to customize a few things like creating a user (it took me a while to realize who the guy on the default avatar picture was since I was not moving too much), setting the time zone and the default keyboard layout, etc. And the fonts. Yes, the fonts above all. I remember how refreshing it was when I switched to a Gnome desktop from Windows XP several years ago. I felt the same but even stronger when I opened Firefox the first time on my new Mac. My heart is filled with joy using iTerm with the Monaco font. I know font types and the look of fonts should probably be a detail for stone-hearted programmers but on the other hand beauty in details is so important. I spent some time thinking whether using nicer terminal fonts makes me a happier developer and it certainly does. I could do without the whizzing windows, sliding workspaces and flamboyant icons but I can not imagine using anything with less beautiful fonts. In three weeks I have become so fond of my precious little fonts I have a hard time imagining abandoning them.

But it is just me. What about you? What made you change to a Mac? What do you fancy most about it? Or if you are not a Mac owner, what do you like most about your Linux/Windows/etc. machine?

Agile Help With Rails

I have recently taken the habit of checking on Rails forum from time to time whether there are unanswered questions I can help with. The best way to see a problem in its context is to recreate it and I was amazed by how easy it is to set up a rails site and debug e.g a routing problem.

> rails railsforum_help_routing > createdb railsforum_help_routing_development (edit database.yml to set user and possibly database type) > script/generate model Trip > script/generate controller Trip (edit routes.rb, in this case)

Naturally the fact that all this takes about 5 minutes greatly leverages lending a hand to a fellow developer. Now that’s what I call agile!

Code Syntax Highlighting

I realized this would be a key feature of any blog that aims to include code snippets in posts right after my friend, Bronson, told me so. With Wordpress, it seems, the problem is not that there are no tools for a certain task but that there are too many. Anyway, I finally found this one to perfectly suit my needs. It supports a plethora of programming/markup languages and the only thing you should add to your code blocks, is a lang attribute to the <pre> like so:

   
    (ruby code comes here)
   

My code in my earlier post looks a lot better, doesn’t it?

Quick Adaptor - My Role Model

I returned two weeks ago from the fantastic Euruko 2008 conference in Prague dazzled by the outstanding presentations and the power of Ruby. It is hard to pick a favorite but I especially liked David Black’s “Per-Object Behavior in Ruby” (slides hopefully coming up) mainly because I am a sucker for metaprogramming and the true object-orientedness of Ruby in all its glory.

One fundamental concept was the singleton class which I think I’ve finally understood from this session. His example was the OpenStruct class whose instances learn to respond to all messages sent to them (this is Smalltalk/Ruby parlance, we could also say they learn to handle method calls) instead of throwing a NoMethodError. So I thought a great way to gain an insight of how this works is to actually code that up myself. Here is what I got:

require 'test/unit'
class QuickAdaptor
  def method_missing(method_id, *args)
    method_name = method_id.to_s
    if method_name =~ /=$/
      attr = method_name.to_s.chop
      self.class.class_eval do
        define_method method_id do |x|
          instance_variable_set("@#{attr}", x)
        end
        method_name.chop!
        define_method method_name.to_sym do
          instance_variable_get "@#{attr}"
        end
      end
      instance_variable_set("@#{attr}", args[0])
    end
  end

end

class QuickSingletonAdaptor
  # does the same as QuickAdaptor
  # but only defines methods for singleton
  # class of object, so it is not the class
  # that "learns", but the singleton class
  def method_missing(method_id, *args)
    method_name = method_id.to_s
    if method_name =~ /=$/
      attr = method_name.to_s.chop
      singleton_class = class << self ; self; end
      singleton_class.instance_eval do
        define_method method_id do |x|
          instance_variable_set("@#{attr}", x)
        end
        method_name.chop!
        define_method method_name.to_sym do
          instance_variable_get "@#{attr}"
        end
      end
      instance_variable_set("@#{attr}", args[0])
    end
  end

end

if __FILE__ == $0
  class ModuleTester < Test::Unit::TestCase

    def test_quick_adaptor_creates_methods
      qa = QuickAdaptor.new
      qa.age = 18
      assert_equal(18, qa.age)
      qa.age = 24
      assert_equal(24, qa.age)
      assert_equal(nil, qa.name)
      qa.name = 'joe'
      assert_equal('joe', qa.name)
      assert(!qa.respond_to(:nam))
    end

    def test_quick_adaptor_creates_instance_methods
      qa1 = QuickAdaptor.new
      qa1.age = 18
      qa2 = QuickAdaptor.new
      assert(qa2.respond_to?(:age=))
      assert(qa2.respond_to?(:age))
      assert_equal(nil, qa2.age)
    end

    def test_quick_singleton_adaptor_creates_singleton_methods
      qa1 = QuickSingletonAdaptor.new
      qa1.age = 18
      qa2 = QuickSingletonAdaptor.new
      # qa2 at this point should not know anything!
      assert(!qa2.respond_to?(:age=))
      assert(!qa2.respond_to?(:age))
    end

  end
end

As a test enthusiastic I embedded the tests in the same file which also has the advantage of giving a documentation of what the code achieves.

You can see that the real difference between the QuickAdaptor and QuickSingletonAdaptor classes lies in the following lines:

class QuickAdaptor
  ...
  self.class.class_eval do
    define_method method_id do |x|
      ...
    end
  end
end
class QuickSingletonAdaptor
  ...
  singleton_class = class << self ; self; end
  singleton_class.instance_eval do
    define_method method_id do |x|
      ...
    end
  end
end

You can see from the tests that the QuickAdaptor shares the knowledge it gained with its peers (other QuickAdaptor instances), whereas QuickSingletonAdaptor is an egoist, it keeps it to itself! That’s why I agree with David Black that the best way to call the class that only one instance can access is singleton class because this has more expressive power than other terms (e.g metaclass, eigenclass, etc.)

My goal is to act like the QuickAdaptor, to suck in all ruby knowledge and learn as fast as QuickAdaptor does while having fun at the same time!

ps. I have also taken a look at how Matz’s OpenStruct class accomplishes the same (well, much more :) ). It uses a table of fields where each new “member method” is defined. Nice.