mobile panel: cross-platform frameworks

I’m excited to be moderating a WCA Mobile SIG panel on May 10th in San Jose. On the panel will be leaders of the top cross-platform mobile frameworks, Rhomobile (Rhodes), Nitobi (PhoneGap) and Appcelerator (Titanium) along with the relative newcomer AppMakr.

Walk-Ins are welcome if there’s space, but I would advise reserving your spot ahead of time — sign up here.

Speakers:

I have my own ideas of what would be interesting questions for the panelists having completed a tour of cross-platform frameworks when researching my book. I very interested in what questions other people have. Whether or not you are attending the panel, please let me know in a comment or send email to sarah at this domain and let me know your thoughts.

Posted in general | Leave a comment

on choosing RSpec as a test framework

There was some discussion, if you can call it that, on twitter yesterday about the proliferation of RSpec and Cucumber, over Test::Unit. I don’t believe that Twitter is an effective medium for well-reasoned debate. I do believe that it is worth discussing why we like one technology over another, so we can learn from each other and possibly refine our approach.

Since I started using RSpec there has been some evolution in the world of ruby test frameworks. I understand that Test::Unit now supports marking a test case “pending” and we can use strings to describe test cases with it syntax. It is great to see the test frameworks borrow ideas from each other and to be part of such a thriving ecosystem.

A couple of years ago, I evaluated ruby unit test frameworks for teaching a class. I found it a happy result that based on my evaluation of test output, I chose RSpec for teaching over Test::Unit, since that is what I use for development. I don’t belong to a cargo cult with test coverage falling from the sky. My reasons for using RSpec have not changed, but I think it is worth elaborating on why I continue to believe that RSpec is the right choice. I look at teaching and development as two separate use cases.

For Development

I practice test-driven development where RSpec really shines, but even when I occasionally must use a test last approach, I find RSpec to be a good solution.

I love the simplicity of starting to code, by writing something like this:

describe Whatever do
   it "should be empty when it is created"
   it "should fly faster than the speed of light"
   it "should sparkle in the moonlight"
end

I start with pending tests, then write the code to make them fail and in the process I design my API and refine my implementation. I can easily focus on that initial inspiration because my test failures contain the language of that design. Nested describes and it strings are concatenated to form sentences, which I can easily review with the –format documentation (or -f d) option:

Here’s an example of that from one of the exercises I use in teaching Ruby:

$ rspec --format documentation perf_spec.rb

PerformanceMonitor
  runs code
  reports how long it takes
  runs code a number of times
  reports the average

Finished in 0.00414 seconds
5 examples, 0 failures

I look forward to checking out Turn in Rails 3.1 which looks like it offers a similar kind of synopsis. I choose RSpec for my projects, but I will happily use Test::Unit if I’m contributing to another project where it is well-loved. I can still accomplish what I need to do for testing with Test::Unit, I just don’t find it to be as easy to use.

David Hansson argues that Test::Unit is simpler, but I think what he means is that it is simpler to learn. David says this is “mostly based on feedback from people learning Rails and RSpec at the same time. And then being introduced to t/u.”

It is helpful to separate how easy something is to learn from how easy it is to use. I first heard this difference clearly articulated by Douglas Engelbart in describing his approach to the development of the Augment (NLS) system. He invented the mouse and a chorded keyboard, so you could use one with each hand. It took some time to learn to use them, but once you knew how, you were very fast at creating and editing documents. He loved to draw the analogy of learning to ride on a child’s tricyle versus learning to ride a bike. A bicycle is harder to learn to ride, but lets you go much faster.

3 + __ = 5 is simpler to understand than 3 + x = 5, but once you start bring able to work with variables, you have more powerful tools that feel simpler to work with. You need to understand addition and subtraction, before you can absorb the conceptual complexity of a variable, but once you get the concept of variables, going back to fill-in-the-blanks equations is awkward and would be even harder for complex equations.

I think the only aspect of RSpec that is hard to learn is the should syntax, as contrasted with the Test::Unit assert which many people are familiar with from junit (the Java unit test framework). I think you can now use asserts in RSpec, if you like ‘em, but I don’t. I do agree the syntax is a little weird at first, but for me it really helps me remember what I’m testing. I’m not a big fan of positional arguments, especially when they are similar, but act differently, because I sometimes forget which is which.

In Test::Unit, when I want to assert that num is equal to 4, I write

assert_equal(num, 4)

In RSpec, I write:

num.should == 4

I might make an error and write assert_equal(4, num), but I would never write 4.should == num. The latter just feels wrong, whereas with assert I need to think about the order of the parameters. Some people are probably really good at remembering that, but I think I’m like most people who have to work at remembering arbitrary binary things.

Lastly, the reason I most value RSpec is in its clarity of output for a test failure, which I covered in detail in my earlier comparison. When I’m working on production code and a test fails, that is when I want to be most efficient and when I want to have the least cognitive load. I don’t mind that I spent a little more time familiarizing myself with the RSpec syntax and writing clear descriptions of my tests, because when I see a failure, I see a high level description of the failure and can easily parse the specific technical failure without extraneous text.

For Teaching (and learning)

I agree that trying to learn how to develop tests in RSpec while learning Ruby and Rails all at the same time is very challenging. I don’t recommend that. I think it is easiest to learn Ruby first, then learn Rails, and while learning both use tests as a kind of lesson planner in the test-first teaching style that has been independently developed by so many engineers. (You can learn more about Test First Teaching with Ruby Koans and at testfirst.org.)

But why use RSpec for teaching? As I’ve said, my initial bias was toward using Test::Unit for teaching. Like David, I thought it was simpler for people to understand that instance methods were being executed as test cases. However, with a teaching approach where I deliver failing tests to students as exercises for them to solve, I feel that the format of the output for failing tests is critical and RSpec still has much cleaner failure reports. Students only have to be able to read the tests, not generate them, and I find that people can quickly do that. Later, after they understand Ruby and Rails, developing tests with RSpec is straightforward since they have already become familiar with reading and understanding the syntax.

Even after reviewing results from the test framework comparison, I was still leaning toward Test::Unit because I thought it would be easier to explain. Then I started to think about how I would explain this:

require 'test/unit'
require 'whatever'

class WhateverTest < Test::Unit::TestCase
    def test_something_is_nil
        assert_equal(nil, Whatever.something)
    end
end

which we run with:

$ ruby test_whatever.rb

I imagined myself saying...

Just like any other ruby file, we can run our test on with the "ruby" command. In our test file, you can see that WhateverTest is simply a subclass of Test::Unit::TestCase, and then we define instance methods with each aspect of our code that we want to test. Then... um... when Ruby reads the class definition it executes the instance methods of that class.

Eek. There's no way I wanted to say that to people on their first introduction to Ruby. I didn't want to introduce that sometimes classes and instance methods work differently -- at least not on day 1!

I concluded that there is a whole lotta magic in every test framework and you need to reach a certain level of sophistication with a language before you can understand how a test framework does its job. And just as I used a C compiler long before I ever knew how it really worked, I figured students could learn to how to make failing RSpec tests pass without needing to understand all of the nuances of syntax and how it all works. I also teach that there are other test frameworks and usually demonstrate Test::Unit. After people know Ruby, Rails and one testing framework, I figure they can make an informed choice.

No teaching methodology is perfect. I've found this works for most students and is more effective than the old "build something that does X" methodology. At least after they finish the exercise, they know that they accomplished the assignment. The RSpec output does a good job of surfacing the Ruby and Rails error messages that I am trying to teach, and along the way, I believe I am introducing students to best practices in terms of development.

Posted in general | 7 Comments

legacy testing with capybara/mechanize

I spent a little time working on some tests for a legacy web app that we plan to re-write in Ruby. Before the big re-write, I thought it would be wise to write some integration tests to call the app via http and verify responses. I wanted to use vanilla http and not have the overhead of launching a browser and was frustrated that Capybara didn’t have a driver to suit my needs.

I started down the path of writing tests from scratch using Net::HTTP, but figured that there had to be someone else who had run into this before. @kakutani told me about capybara-mechanize which seems to be exactly what I need.

Here’s my first really basic experiment using RSpec to test a well-known web site:

require 'capybara/rspec'
require 'capybara/mechanize'
Capybara.default_driver = :mechanize

describe 'web app' do
  include Capybara

  before do
    Capybara.app_host = "http://www.google.com"
  end

  it "has a copyright" do
    visit '/'
    page.should have_content('© 2011')
  end

end
Posted in general | Leave a comment

kicking off remote learning for ruby on rails

Last week’s distance learning experiment was a success! We had a few technical glitches… I forgot to hit the “record” button at the beginning and after the break and didn’t set the meeting time to last the whole session, so I’ll be repeating the class today at 4pm PST. The Blazing Cloud blog has more details on this Ruby on Rails class webinar.

I’m very excited about this format. Every time I offer this class, one or two people ask if they can attend remotely. And every time I feel like it is just too hard to get it all together. However, this time I decided to try this as an experimented and Anita Kuno suggested that we use InstantPresenter which seems to be working quite well. I am excited that we had remote participants from as far away as Cambodia and have felt honored by the warm response to the first class.

Posted in general | 2 Comments

start learning ruby on rails tomorrow night 1/25

Tomorrow night we’re planning an experiment in distance learning. If it works out, we’ll offer our evening Blazing Cloud classes with a (for pay) online option, but for starters we thought we would try out the tech. If you want to join us at 6:30pm for an introduction to Ruby on Rails, please join this google group.

The audience is people who already know how to program, but don’t know Rails. If you want to join us, you should install Ruby and Rails before you arrive.

  • If you are on Mac or Linux, I recommend rvm for installing Ruby and Ruby Gems, use either 1.9.2 or 1.8.7 and then
    • gem install rails
    • install git — you can use instructions linked from here
  • If you are on Windows, I recommend the shiny new Rails Installer from the fine folks at Engine Yard

Also, please create an account on Heroku, since we will host our apps there

Posted in general | Leave a comment

cross-platform smartphone development

In the past few years there has been significant changes in mobile development. More than half of the world population now has a mobile phone — more people than have access to personal computers. Mobile phones have compute power that exceeds that of personal computers when I started programming and smartphones are gaining adoption fast.

My work has focused on application development on mobile phones and on the web. I’ve seen an interesting trend in mobile development that is explored in my new book: Pro Smartphone Cross-Platform Development: iPhone, Blackberry, Windows Mobile and Android Development and Distribution. If the title seems a bit long and complicated, it speaks to how mobile development today is a bit longer and more complicated than it should be given what we are able to produce. Every platform has its own APIs and often requires that you code in a specific language as well. The book addresses an alternate path which borrows patterns from the web and allows for rapid application development.

The cross-platform methodology explored in the book is where the UI of your application is in HTML with your business logic in Javascript, Ruby or native code. The focus is on native applications, which have access to local storage, camera, contacts, geolocation, etc. The book is written in three parts:

  1. Platform Development and Distribution covers how to write using the native language, tools and framework. It includes a “hello world” application with simple UI and how to embed a web browser component in your application. It also includes how to distribute an application, what you need to get into the platform marketplace or app store and whether there are other options, like “over the air” distribution.
  2. Cross-Platform Native Frameworks covers Rhodes, PhoneGap and Titanium Mobile. Originally we were just going to cover Rhodes and PhoneGap, which both use HTML UI with Ruby and Javascript respectively (although Rhodes lets you access native components). Titanium Mobile was a late addition and uses Javascript to include native components and supports just two platforms (iPhone and Android).
  3. HTML Interfaces covers how to sue HTML to make your application UI look and feel native on WebKit browsers. It also includes a chapter on how to craft HTML for BlackBerry if you need to support the older BlackBerry browsers — that chapter alone is worth the price of the book if you need it. There is also a nice appendix on core CSS concepts that you’ll need if you are new to app development in HTML.

It is a survey book, designed for people new to mobile development or who only know one platform and are seeking to understand how to tackle multiple platforms economically. The content has been criticized as something you could “google for;” however, I found that to really understand what it takes to develop and release a mobile application on each platform, you need to build one. This book gives you a good grounding in your options and collects all of the information in one place that you would need to make a decision about pursuing cross-platform development on your own or using one of the popular frameworks. The goal is to give you enough information so that you can productively dive into the right starting point for you.

This book won’t tell you everything. It’s not for folks who want to develop a game or expect to focus on a single platform. It’s for people who need to make quick decisions about how to move into mobile and to get a preview of the challenges they will face. It doesn’t endorse specific technologies, but lays out the trade-offs. This is the book I wish I could have read when I started mobile application development.

Posted in general | Leave a comment

perfection vs mastery

Abhishek Parolkar writes that flawlessness is not about perfection, its about how complete can you become after accepting reality. He offers this amazing video that illustrates this idea:

It reminds me of The Man Who Mistook His Wife For A Hat by Oliver Sacks where he tells stories from his clinical practice about people with severe mental disabilities. His focus is not really on their disabilities, but on other abilities which allow them to compensate and actually excel in a very different way.

Mastery is an important skill. In my field of software development, I believe that the most important skill is the ability to learn. It is something that is hard to assess in an interview, but you can see it clearly when you work with someone for a while. In the past two years, I moved outside of my comfort zone, into new tech and then stepping into a full time role leading a new business. I realize that I’ve become accustomed to the sharp pain of reflecting on a recent failure. It is like muscle pain when I’m working out — a real part of the process. Randy Nelson says that “the core skill of innovators is error-recovery, not failure avoidance.” I’ve seen this in action repeatedly with the RailsBridge workshops. When I’m trying to make something happen, I try to focus on the big goal, that intangible thing which is not the event or the deal. Whatever it is, that goal cares little about errors along the way, as long as we can repair them quickly enough that relationships aren’t damaged, and sometimes the error doesn’t matter at all. I recently read the Done Manifesto (via @ianchia) which speaks to the same concept in a different way. As noted by @sftwrexperiment, we learn and adapt. Sometimes I feel like my whole life has been spent practicing for this moment.

Posted in general | 3 Comments

backwards stratego

This holiday I learned how to play backward Stratego — a variant of the old strategy game. We used a set which has wizards, elves and dragons, but I’m sure it would be just as fun with a standard set. I expected it to be frustrating, but it was surprisingly fun.

Setup

  1. Set up the pieces facing your opponent without looking at them
  2. Then take turns with one person facing away, the other makes sure that the flag is in the back row, has a trap in front of it and a 8,9,10 or trap on each side, with a fair arrangement of strong pieces. If you have to make a choice, ask your partner “pick A or B” or “a number between 27 and 39″ and then apply that to your choices so that it is suitably random. The idea is to make the game fun by being equally challenging.

Gameplay

Take turns moving pieces. You need to tell your opponent if he or she tries to move a trap (or a mine in the standard set). Once you know what a piece is, you turn it around to face you. Aside from trying and failing to move a trap or a flag, the only way to identify a piece is to attack with it (or be attacked). You can also pick a piece and ask if it can fly (to identify a dragon), but that takes your whole turn if it isn’t. We also decided that if you run out of dwarves (miners) then you can use the next higher piece to take out traps.

board layout

Posted in general | Leave a comment

how to get a Rails job

The overall economy still sucks — I read that this is not just the US, but a global problem. Meanwhile, in my little corner of the world, I run into people everyday who are frustrated that they can’t find good Rails engineers to hire. I’ve written that companies need to hire people who are good engineers and don’t (yet) know Rails. I also talk to a lot of unemployed engineers — people laid off from .NET, java or management and young people who are struggling to find their first (good) job. This blog post is for those folks. There are some key skills that are different and that you can (and need to) start to acquire before you can easily find a job.

I find that most engineers can learn the tech, but think they need a job to demonstrate that they know it. Therein lies the issue. It appears to be a chicken-and-egg problem, but it is in fact a gap in skillset. It is common for me to run into engineers who say they have been learning Ruby and Rails for months or years, but I google their name and don’t see anything they have published –no blog post, no tutorials, answers on mailing lists, bug reports, or patches. If you have tried to learn something, then you must have run into an issue with it. If you have built an app, used a gem — if you live in this world and write any kind of software, you have run into missing documentation, you have worked around an issue or solved a configuration problem. Working in open source, experienced engineers publish or patch issues that they find. If you haven’t documented or fixed issues, a hiring manager has to wonder: did you really build something? did you just follow someone else’s tutorial? are you someone who will push themselves? would you even notice if something wasn’t right? If you don’t participate it undermines your credibility.

The good news is this is easy to remedy. Don’t feel you have to start with a big project. It is better if you start with something small. Just continue to do your own development and whenever you run into an issue, really dig into it. Then publish what you discover. Whether it is a bug or a feature someone else will need the answer you just discovered. If it is a bug, report it. If you think you can fix it, submit a patch or github pull request.

More suggestions

  • start hanging out in a Ruby or Rails forum or stack overflow and answer questions that you know or can find the answer to. Provide links to resources, not just answers. Do it in a friendly tone with humility or just give a simple answer. This is will sharpen your skills and start to build a little credibility.
  • start a blog, write a short synopsis with a code snippet or reference to github repo with an example whenever you learn something, anything that you can imagine someone else might run into or you want to remember
  • add your profile to workingwithrails.com
  • show up at your local meetups, introduce yourself to a few people who look like they don’t know anyone
  • start a study group
  • volunteer for railsbridge (seriously, you could just show up on the mailing list and ask what needs doing, say what your skills are and what you would like to do)

While you are looking for paid work, plan to spend a significant amount of time honing your skills by solving some real problems.  In looking for a Rails job, that activity will serve you better than more hours polishing your resume.  Focus on being very good at whatever you do know and enjoy doing.  Understand the results that you can achieve.  Develop your own sense about what is great Ruby code, but be open to not having all the answers.  Once you get fairly good at something and have written code for it, consider publishing it as a gem.  There are plenty of bugs to be fixed and problems to be solved.

Posted in general | 6 Comments

freedom in ruby and code

After my last post, I had a twitter conversation with @dhh about his talk and my reflections on it. I’m still not sure he understands that his talk wasn’t “weird-cool” like his example of _why, nor do I think it was an example of “weird-angry” that the Ruby community would disdain. Instead it is an alternate brand of weird, on the verge of weird-creepy — over the top for some, but merely boring and off-topic for me. I heard from one person via email that the keynote (and some panel I didn’t attend) caused a first-time RubyConf attendee to feel out of place at the conference — not likely to attend again. I didn’t get a sense of leaving in a huff, rather that this was a closed community with an in-crowd and a sense of humor not shared. I believe there a wide space between delightful and avant-guard experimentation in code and art, and the status quo of corporate America. However, David Hansson and others seem to fear that if they can’t use NSFW language and metaphor that soon we’ll all be wearing suits to RubyConf and the wild creativity of the Ruby community will be stifled. I think not.

I do believe Dave when he says that he sought to inspire…
I can't imagine a more inspiring topic than freedom. I was certainly fired up! But glad you liked Dave Thomas' talk.

Over twitter, he re-stated his thesis for me:
@ultrasarus tweet: @dhh wondering.. did you have a call-to-action in mind? or just that we should appreciate (defend?) our Ruby freedoms? sorry if I missed it / @dhh replies: @ultrasaurus If you don't know and appreciate the freedoms that you have, you're much more likely to casually lose them.

What does freedom mean with respect to code?

David referred to Maslow’s Hierarchy of Needs, suggesting that there is a parallel hierarchy of programming languages. I think assembly language should be considered the bottom of the pyramid, since it allows programs to survive using a primitive communication with the machine and agree that Ruby supports the top of the pyramid.
Maslow's Hierarchy of Needs

Self-actualization

The top of the pyramid includes: morality, creativity, spontaneity, problem solving, lack of prejudice, acceptance of facts
Examples:

  • Open / Dynamic language (spontaneity and creativity)
  • Strong support for and widespread practice of testing supports (lack of prejudice, acceptance of facts)
  • Ruby Gems allows for experimenting with new versions of libraries
  • rubygem.org and gem push (creativity, spontaneity)

Esteem

The next level includes: self-esteem, confidence, achievement, respect of others, respect by others.  These aspects are in some ways strongly supported by the community where there is technical support for publishing our work and strong systems of acknowledgment.
Supporting Examples:

  • Ruby Gems
    • Gem specification with author attribution
    • rubygems.org download stats
  • GitHub
    • free for open source projects
    • fork
  • Blogging, Ruby Meetups, Regional Conferences: diverse venues and respect given to people who publish words as well as code, and who talk about their work

Where we still need work:

  • respect by others: there is a difference between playful and juvenile, growing up doesn’t mean becoming like our parents, we can take responsibility for our actions and our impact on others and still have fun
  • respect of others
    • We’ve made great strides in the last year or two with the diversity of Ruby VMs to support various technical communities. Java, at times reviled by members of our community, can also now be appreciated for its JIT and other tools with JRuby. As Matz noted, diversity is tough, but worth embracing.
    • We still need to do a lot of work on making Ruby work for people who run Windows. It is not particularly helpful to tell someone who wants to experiment with Ruby that they need to buy new hardware.
    • Sexism and racism still exist. At its best, open source is a meritocracy. At worst, it is a white boy’s club. Go out of your way to not be that guy.

Defending Freedom of Code

I agree that the Ruby community uses the freedom of the Ruby language to great benefit. As the community grows, we are in danger of settling into patterns established by our pundits or the conventions of last year’s gems (which reflect last year’s thinking). Each of us needs to understand the core principles by which we code and practice those, in order to retain our freedoms. I’d like to share some examples that would have worked better (for me) in the keynote, which could have been suitable replacements for what I felt were off-topic metaphors.

One of the points David Hannsson made in his keynote is about the freedom from declaring types. It reminded me of Oliver Steele’s article Test vs. Type — originally referencing JavaScript and Python, but the same principles apply to Ruby. David talked of critics who speak of “enough rope to hang yourself” and asked, rhetorically, if hanging was really the most common use case for rope. Should we focus on preventing one rare use case rather than supporting all of the other useful ways to use rope? Instead of being afraid of what liberty allows, we can provide effective measures to protect ourselves. With Ruby, we do that by having strong technical and social support for testing.

The Ruby test framework explosion in the past few years has been a bit overwhelming to many of us and was at first addressed by blog post tutorials and talks at meetups and conferences. Many Rubyists have also worked hard to support the freedom to use your test framework of preference. Here are some examples:

  • Cucumber auto-detects and configures itself based on whether you have webrat or capybara installed
  • Rails 3 has plug-in generators so that any test framework can generate its own tests for scaffold, model, etc.
  • Rspec lets you configure what mocking library you want to use.

In general, Rubyists value freedom of choice. When we can easily experiment with different frameworks and libraries, we can make well-reasoned decisions without overly valuing the status quo. As software developers, we need to work hard to overcome inertia in order to move forward and continue develop great code year after year. A few more examples:

  • Rack: allows us to mix and match web servers
  • Rails 3 Routes can be any Rack endpoint, allowing us to mix Rails and Sinatra in the same web app (and potentially other frameworks)

I’d be interested in hearing about other examples of how Rubyists are using the flexibility and freedom that are inherent in the language to create an ecosystem that protects us from the inherent risks, defends those freedoms and promotes programmer happiness and creativity.

Posted in general | Leave a comment