Developing fluidly

Here’s a raw idea I’m playing with in my head:

Agile development is great. But, if your team doesn’t map well to it, steal ideas from agile relentlessly.

You want a fluid environment where developers can solve problems (features, defects, chores) as they see fit.

Don’t use procedures to normalize productivity or as a communication protocol.

Do have a way to communicate things that need to get done or could possibly get done.

You need a safety net. Unless you know better, that safety net is some kind of automated developer test suite.

Enable developers, don’t direct them.

Discuss.


Classy Web Development with Sinatra

An admission: I didn’t really do as many awesome things during the Bush administration that I would have liked to. So, now that we have a new president, I’m going to start off right by showing you something awesome.

While it may seem like I’ve had my head up in the clouds of physical computing, urbanism and monads, I’ve been nose down in something else. I’m super-excited to tell you, I’ve just finished the first two episodes of “Classy Web Development with Sinatra”, a screencast for the Pragmatic Programmers.

h2. The Particulars

Sinatra is a great subject. As I point out in the first episode, I think it’s very special in how it puts the smallest possible language over HTTP. Taken with the fun of building your own framework up from scratch, Sinatra apps are a ton of fun to write. Further, Sinatra really shines when you want to write micro-apps or services. Building an API for your Rails app by standing a Sinatra app up next to it to serve the API is a great approach for many applications.

If you’ve known me for a while, you probably know I had at one point aspired to write a book for the Pragmatic bookshelf. That didn’t work out (for the better, I think), but I still wanted to produce something to get the good word out there. I’ve enjoyed the screencast format from the beginning, and I enjoy teaching people interactively, so the format seemed well suited to me. Plus, I’ve always loved the sound of keys clacking in a screencast. Now you can hear my keys!

h2. Shout-outs

Lately, Ryan Tomayko has been absolutely kicking ass with his contributions to Sinatra and Rack. He was kind enough to review the script before we recorded and even put out a maintenance release so that we wouldn’t have to talk around a couple bugs that snuck into the latest release. The finished product is much better for his feedback and guidance.

From the beginning, Mike Clark has been a great mentor and guide through the process of producing a screencast. He helped me write to the beginner’s perspective and avoid speaking in a monotone. Throughout the process, he’s been more helpful and supportive than I could have ever imagined. Needless to say, he’s the foundation that all the goodness of the Pragmatic Screencasts series is built upon.

h2. I want to go to there

Go give the sampler a look, then buy the episodes! If you have questions, drop a message in the forum. And don’t forget to grab the example app and service off GitHub.


People are making cool stuff

This morning I followed a link that _why shared with us, a lo-fi guitar pedal built around an Arduiono. Kyle McDonald made said pedal; he also made I Eat Beats, a drum machine gizmo built with a screen, Processing, and skittles. He also published instructions for how to build a 3-D controller. From there I found a coin-slot detector, again built with an Arduino.

Moral of the story: there is a ton of cool stuff going on with Processing and Arduino. Just let the links light your path.


My notes from RubyConf 2008

This year at RubyConf, I decided to go analog and take hand-written notes. (Someone did this for another conference but I can’t find the link.) I hope you find them amusing and/or educational. Click through to the Flickr set to see them in all their glory.

RubyConf Notes #0

Bonus points for identifying the people whose hair I drew and trying to decipher my handwriting!


Mining my Git repositories

Since I started using Git, I’ve been finding myself creating tons of repositories. Anything I think might someday prove interesting or that I work on for more than a few minutes, I create a Git repository. I’ve yet to discover that ultimate workflow, but between experimenting with using it to put presentations online, managing the Dallas.rb website via Git and using it extensively at FiveRuns, Git is proving quite fun.

But let’s get back to that earlier point: I have a metric shit-ton of repositories laying around in my home directory. This morning I found myself wondering exactly how many I have and how many actually have a remote (i.e. how many have yielded a project worth backing up remotely and/or sharing). So, I did the numbers:

Directory Repositories Remotes
@/Users/adam@ (Home, sweet home) 287 263
@~/FiveRuns@ (Work stuff) 161 160
@~/repos@ (All source-ish stuff) 85 69
@~/repos/sources@ (Interesting code of others) 62 60
@~/repos/projects@ (My own code) 9 6
@~/Desktop@ (Landing pad for the newest of projects) 1 0

Which is about what I’d expect in terms of quantity and ratio. I’m a little surprised I have so many work repositories laying around, but we use submodules extensively so there’s probably only about 40 repositories that are meaningful to us. I’m a little suprised that so many of my personal projects do have remotes though. I guess I’m making progress towards doing more.

For those interested, the script that begat all this fun data. Run it yourself and share your numbers!


Io's intriguing design

I didn’t manage to touch on this in What Has Ruby Done For You Lately, but Io is a really impressive language. Mostly in the minimal number of concepts one needs to grasp in order to deeply understand the language.

To pick on Java (sorry), a list of concepts you need in order to grasp Java:

  • Primitive types
  • Objects
  • Classes
  • Interfaces
  • Inheritance
  • Iteration
  • Conditionals
  • Operators
  • Type casting
  • Generics
  • Method calls

I’m sure I’m missing some. Further, that’s just what you need to write a program with one class. To build a realistic system you need to know about class paths, packages and all sorts of arcane details.

In comparison, to build a simplistic Io program, you need to understand:

  • Lazy evaluation
  • Objects as prototypes
  • Short circuit evaluation
  • Messages
  • Closures

That list ended up about twice as long as I thought it would, actually. Really, all the average practicing programmer needs to learn is prototypes and lazy evaluation.

Io's guiding design principle is simplicity and power through conceptual unification.

That quote, from the Io Programming Guide, pulls it all together and summarizes why the language nerd in me is quite interested in Io.


Refactoring to more code

Refactor my code is a neat site where you can post your code and watch others refactor it. I saw an interesting bit of code whiz past and thought I’d take a crack at it.

Removing conditionals from code is one of the little games I sometimes play while coding. Here, I’ve extracted the logic of the conditional into another class. The resulting class is much more code than the original. So why do that?

Well, I say you get a few benefits:

  • The logic is now far easier to test. It’s a standalone object now rather than a Rails functional test.
  • The flow of what’s being done and tested is more decomposed and easier to follow.
  • Most importantly, the code explains itself. No need for comments (which will undoubtedly go out of sync over time) here!

While I delight in deleting code and writing as little as possible, refactoring this to more code seems the right way. What say you?

Update: Make sure you check out Marcel Molina’s refactoring. Its probably better than mine ;)


Destructuring assignment in block parameters

…which is just a fancy way to say that this works like I think it should:


>> [['foo', 1], ['bar', 2], ['honk', 1000]].partition { |str, val| val > 999 }
=> [[["honk", 1000]], [["foo", 1], ["bar", 2]]]

Which beats the pants off this:


>> [['foo', 1], ['bar', 2], ['honk', 1000]].partition do |pair|
?>     str, val = pair
>>     val > 999
>>   end
=> [[["honk", 1000]], [["foo", 1], ["bar", 2]]]

So, you see, the array elements get unpacked into @str@ and @val@ inside your block. Which is unexpected, but totally and thoroughly lovely. To my eyes, the former is much easier to read.


Changing git submodule URLs

Pro-tip: if you’re using submodules with Git to manage dependencies (say Rails plugins), you can get yourself into trouble. Like half-a-day of wasted trouble.

The rub comes when you need to make a change to some plugin. Suppose you were using Brain Buster for your captcha and then decided you need to make a change. The @git submodule@ command doesn’t really seem to offer a way to change the URL for the submodule. Let me summarize what I’ve found in trying to do this.

h2{color: #f00}. The Wrong Way

  • Fork/create your own version of the submodule in question
  • Change the URL for the submodule in @.git/config@ and @.gitmodules@
  • Cross your fingers and hope for the best

h2. The Right Way

  • Fork/create your own version of the submodule in question
  • Remove references to the existing submodule in @.git/config@, @.gitmodules@ and nuke the submodule directory
  • Commit
  • Add the new submodule URL
  • Commit
  • Make changes in your submodule
  • Commit changes in the submodule (not the parent project)
  • Commit the changes in the parent project (otherwise you’ll only get the old version of the submodule in future pulls)
  • Enjoy the hair you didn’t have to rip from your scalp

Learn from my mistakes, people.


Beautiful multi-line blocks in Ruby

“I need a new drug”:http://youtube.com/watch?v=MMSFX1Vb3xQ. One that won’t quit. One that will let me sensibly structure the use of blocks in Ruby such that they can run across multiple lines and yet still chain blocks together.

OK, so its not really a drug I need, per se. Though, I’m sure what Huey Lewis really needed was a new drug. But what I need is a convention, or perhaps some syntax. It should say to you, “HEY. Adam’s doing something clever with blocks here. Keep your eyes open.”

I find myself desiring a new syntax and/or convention, when using blocks. I’ve been trying to write in a more functional style lately, especially a pure-functional style wherein you never call a method for its side-effects. I don’t think I’m the only person doing this. Jim Weirich sort of alluded to it in a post about “when to use @do/end@ vs. braces”:http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc. Rick DeNatale “took it a step further”:http://talklikeaduck.denhaven2.com/articles/2007/10/02/ruby-blocks-do-or-brace. I want to lay it bare.

Let’s start with something innocuous:


ary = [1,2,3]

result = ary.map do |n|
  x = n * 4
end

result # => [4, 8, 12]

You get the value back of each object in the array, mapped to the result of calling the block. Fun times. Now, let’s put it on one line and do something clever with it.


ary.map { |n| n * 4 }.select { |n| n == 4 } # => [4]

Now we’re cooking! Ruby’s syntax allows us to chain methods and blocks. Which turns out nice in this case where I want to filter down the array of mapped values. But let’s pretend we need to do something clever in those blocks.

  
    ary.map { |n|
      n * 4
      # Some
      # clever
      # things...
    }.select { |n|
      n == 4
      # More
      # clever
      # things...
    }.any? { |n|
      n == 4
      # Gratuitiou
      # clever
      # things...
    } # true
  

That’s the best I could come up with for chained, multi-line blocks. It looks “Weirichian”. Despite that, it kinda makes me vomit in my mouth.

So, since “I got awesome feedback on my last question of taste”:http://therealadam.com/archive/2008/04/28/can-i-send-you-a-message/, I’m tapping you, my favorite Ruby developer, for more. The ground rules are that you can’t extract the logic into a real method and you can’t jam everything onto one line. You have to use multi-line blocks. What looks good to you here?


Can I send you a message?

@Object#send@ - the joy of Rubyists and the scourge of those who would write refactoring tools. Let’s talk about it.

I recently ended up writing some code that would have proven really hideous if I couldn’t call @#send@. So I had a class like the following (written using code generation for brevity):


class Foo
  %w{one two three}.each do |name|
    class_eval <<-EOC
      def #{name}
        '#{name}'
      end
    EOC
  end
end

I needed to call @one@, @two@ or @three@ from a bit of code that takes some user input (in this case, an attribute value in a template language). So I cooked something like this up:


%w{one two three foo}.each do |arg|
  meth = case arg
  when 'one'
    :one
  when 'two'
    :two
  when 'three'
    :three
  else
    raise 'Unknown arg'
  end

  puts Foo.new.send(meth)

end

So I’m using a case statement to convert valid method names to symbols, which I then pass to @#send@ and bam!, my method is called. This made my code a ton easier to write and I’m here espousing the technique to you.

But what is the drawback? Well, if someone were to ever really get up the courage to tackle the task of building a refactoring browser for Ruby, this sort of thing would give them fits. They can’t really tell where those methods are called on my class until they are actually called. Heck, given the code above, they can’t even figure out what methods exist on @Foo@ without running the code.

The other drawback is that this code is a little hard to read. Most new casual Rubyists won’t think to search for the symbol version of a method name. Its even harder if you’re still somewhat new to Ruby and you aren’t aware this sort of thing even happens.

For me personally, the concise code I can write with @#send@ far outweighs the drawbacks. I preach the importance of code reading regularly, and its how one can get over the “hump” that is knowing how to navigate software that sends dynamic messages to objects.

Your homework is to share how you feel about @#send@ in the comments.


Exploratory hacking in TextMate

My first foray into screencasting:

‘Tis a little tutorial on a little bit of joy I use regularly. In TextMate, you can add xmp markers like so:


1 + 2 # =>
String.class # =>
%w{foo bar baz}.each { |w| w.upcase } # =>

Then if you hit Ctrl+Shift+Command+E, you get this:


1 + 2 # => 3
String.class # => Class
%w{foo bar baz}.each { |w| w.upcase } # => ["foo", "bar", "baz"]

This is a great way to do exploratory hacking. Plus, you don’t feel like you’re doing “printf” style debugging. And that makes everyone feel cooler!