Thursday, September 04, 2008

Ruby: Recording Method Calls and Playback With Inject

Sometimes you want to call methods on an object, but you want to delay the actual execution of those methods till a later time.

For example, in expectations you create a mock at parse time, but you actually want the mock to be available at execution time.

class SystemProcess
def start
puts "started"
new StartedProcess
end
end

Expectations do
expect SystemProcess.new.to.receive(:start) do |process|
process.start
end
end

In the above code you define what you expect when the file is parsed, but you actually want the process expectation to be set when the do block is executed. This can be (and is) achieved by using a recorder to record all the method calls on the process object. At execution time the method calls are played back and the initialized process object is yielded to the block.

The code for a recorder is actually quite trivial in Ruby.

class Recorder
attr_reader :subject
def initialize(subject)
@subject = subject
end

def replay
method_stack.inject(subject) { |result, element| result.send element.first, *element.last }
end

def method_stack
@method_stack ||= []
end

def method_missing(sym, *args)
method_stack << [sym, args]
self
end
end

Here's an example usage of a recorder.

class SystemProcess
def start(in_seconds)
puts "starting in #{in_seconds}"
sleep in_seconds
StartedProcess.new
end
end

class StartedProcess
def pause(in_seconds)
puts "pausing in #{in_seconds}"
sleep in_seconds
PausedProcess.new
end
end

class PausedProcess
def stop(in_seconds)
puts "stopping in #{in_seconds}"
sleep in_seconds
self
end
end

Recorder.new(SystemProcess.new).start(1).pause(2).stop(3).replay
# >> starting in 1
# >> pausing in 2
# >> stopping in 3

The only thing worth noting is that by using inject you can use a method chain that returns different objects. Traditional versions of a recorder that I've seen often assume that all the methods should be called on the subject. I prefer the version that allows for object creation within the fluent interface. In practice, that's exactly what was needed for recording and playing back Mocha's expectation setting methods.

Tuesday, September 02, 2008

Passionate, Not Dogmatic

Ted Neward recently wrote a blog entry that began with the following text:
... the debates have begun, with all the carefully-weighed logic, respectful discourse, and reasoned analysis that we've come to expect and enjoy from this industry.

Yeah, right.
Ted's comment is funny because it's true. Ted's comment is also disappointing... because it's true.

In the past 3.5 years I had the opportunity to interact with some of the smartest people in our industry. I consider many of those smart people to be among the best software developers in the world. Unfortunately, some of the smart people I met weren't much more than assholes. The big difference I noticed between the two groups was -- The assholes were dogmatic, while the best developers were passionate
passionate: expressing, showing, or marked by intense or strong feeling
dogmatic: asserting opinions in a doctrinaire or arrogant manner
--dictionary.com
The difference between passionate and dogmatic is slim, but the result is dramatic.

Martin Fowler is a great example of someone who is passionate without being dogmatic. For example, Martin is a classicist, he prefers state based testing. However, in Mocks Aren't Stubs Martin examines both points of view and makes no assertion on which was is absolutely correct. That's a tough thing to do, but the result is a classic article that both mockists and classicists often refer to. That's just one example, but almost every article by Martin provides at least 2 points of view. The result is extremely valuable.

I used to be dogmatic. I have no problem admitting it. My earlier writing is clearly arrogant and often shortsighted. Part of the problem was lack of experience. When you take an immature industry and give a platform to someone with limited heuristics you are bound to receive solutions with limited applicability.

As I gained more experience I realized that what I considered to be best practices were only best practices within certain contexts. I also realized that presenting something as the "one true way" only benefited those that worked within exactly the same context that I worked. People who follow my advice when it doesn't apply to their context must fail. The advice isn't flawed, but it is incomplete. You need to see the full picture.

Two interesting things happen when you write passionate entries instead of dogmatic entries.
  • Your advice is more widely and appropriately used.
  • Your traffic goes down significantly.
On a recent podcast Joel Spolsky noted that most advice needs contextual information. Unfortunately, contextual information implies that the advice isn't universally applicable. While that's great for a small subset of professionals who are interested in best practices and improvement, the vast majority of people in our industry are still in search of silver bullets. The easiest way to ensure that your advice is missed by the majority of the industry is to spend the first 2 paragraphs of the entry describing the context.

Conversely, our industry loves dogmatic advice. For example, DRY is blindly, dogmatically followed. I'm not a fan of blindly following DRY, so I wrote about the value of duplication within tests. I attempted to give a counter point of view with contextual examples, but at the end of the day the entry got a large amount of traffic solely from the authoritative title.

It's easy to spot the difference between a dogmatic entry and a passionate entry. The dogmatic entry focuses on the best practice alone. However, a passionate entry gives equal weight to context and the best practice. Passionate entries are much more likely to see successful application, even if they don't make the top of reddit.

Preferring passionate to dogmatic entries is ultimately good, but you suffer in the short term. A career in software development is obviously a long term play, but you can't always blame someone for looking for short term gains. Of course, Martin Fowler is an example of the success you can achieve by sticking with passion over dogmatism.

There is one large upside to being passionate instead of dogmatic: You gain significantly more opportunities to learn. I can't count the number of times in the last year that I've said "I prefer the way I'm suggesting because I know it works, but let's do it your way and see if it's superior". (credit: I'm fairly sure I stole that phrase from George Malamidis while we worked together at TrafficBroker) Sometimes I was right, sometimes I was wrong, but I always learned something by trying a new approach.

That upside is what I believe truly separates the best in our industry from the assholes. The passionate leaders are constantly learning the best ways to do things, while the dogmatic leaders have stopped evolving their approach. As I said before, both groups are smart, but the dogmatic developers can only get by on their wisdom for so long. Eventually all dogmatic leaders become irrelevant.

Thursday, August 28, 2008

Great Developers Are Hard To Find, Regardless of Language

Matt Aimonetti has a client that believes Rubyists don't scale. Sorry Matt, your client is wrong.

It's as easy to hire great Ruby programmers as it is to hire great Java programmers or great programmers for any given popular language.

The problem is, it's not easy to hire great programmers in any language. Worse, companies still think they need to hire one or two great programmers and a supporting cast of average programmers. As Martin Fowler pointed out, cheaper developers are actually more expensive than experts. To make things worse, if you end up with a NNPP or two you'll be headed in the wrong direction.

As I've said before, 50% of all programmers need to find a new profession. In the short term this will create a much larger demand for developers, and salaries will rise. However, since one expert programmer produces significantly more (up to 28 times more) than two terrible programmers, companies will ultimately save money. It's unlikely that salaries of experts will rise to the sum of 28 terrible programmer's salaries.

Some people believe that the average programmers are valuable because they do the jobs the experts don't feel like doing. This is simply wrong. Average programmers will do average jobs on mundane tasks, which often turns into a mess for expert programmers to fix. Even if the solution proposed by average programmers doesn't require an entire rewrite, it is often sub-optimal and can have implications with other pieces of software that rely on it.

There's a better way. Give mundane tasks to expert programmers. Expert programmers remove or automate mundane tasks. The solutions will be superior, and either way the experts won't be doing mundane tasks.

If you have great programmers producing up to 28 times more, and you only hire great programmers, how many programmers do you think you need? Of course the answer depends on how much software needs to be written, but in general teams can become a lot smaller.

And this is the problem with Matt's client's statement. While they may not be able to easily build a team of average Ruby developers, it doesn't matter, that's the wrong goal. Of course, I have no way of knowing the caliber they were looking to hire. But, I do know if they wanted Ruby experts that will travel, ThoughtWorks employs several. So, I'm left to guess that they fell into the same old misguided pattern of trying to pay average salaries to average developers.

I'm left wondering why they would take expert advice on what technology to use, hire an expert, achieve success and then stop pursuing experts. Of course, I'm not surprised. As a consultant, I saw it time and time again.

The question isn't whether you can hire rubyists or not, it's whether or not you are willing to put the effort in to hire experts. It isn't easy to hire experts regardless of the language, but it is worth it.

Monday, August 25, 2008

Lean Software Development

While working in London for TrafficBroker I had the opportunity to try out Fred George's Lean process. To date, it's absolutely my favorite way to deliver software.

Stories
Stories truly are placeholders for a conversation. They are short sentences such as "The system should process pending orders when inventory is available". If you want to work on the story you (and your pair) go find the business person who wants the feature and you talk to them until you understand what needs to be built. Once you know what you need to do, you create tasks.

Stories are estimated using the techniques I detailed on InfoQ.

Tasks
Tasks are the technical tasks that need to be completed to finish the story. They are written on a sticky and attached to the story on the wall. When a task is finished you put a green dot on the sticky. At any time you can see what stories are being worked on, how many tasks are associated with the story and how many have been completed so far.

Retrospectives
Even though we never felt like we needed a retrospective we got on a schedule of doing them every two weeks. I walked into every one thinking it was unnecessary, and walked out thinking it was critical that it happened. Great retrospectives really make a world of difference.

We all put stickies on the wall in the "not so well" or "well" columns, then we talk about each one. Everyone gets the opportunity to elaborate on their sticky. The common themes become action items.

Showcases
We also had weekly showcases to demo new functionality for anyone who was interested. In the showcase we also used a burndown chart to show progress. We only estimated the stories that were necessary for the next version or release, so the burndown chart was often a short term picture. Of course, any further detail would be greatly unreliable anyway. The burndown numbers are based on taking the sum of the estimates and applying the velocity, based on a weekly velocity total.

Story Wall
We also used a story wall to track progress. The stories moved from Definition -> Awaiting Development -> In Development -> in QA -> Awaiting Business Acceptance -> Complete. The stories in Awaiting Development are ordered from top down by priority. The business could come over at any time and reorder based on priority.

Waste Removal
I loved the process because we constantly removed waste (effort for zero value items). We didn't need to commit to points per iteration because it didn't gain us anything. We didn't bother tracking actuals, hours, or anything else that wasn't important. We also didn't discuss or estimate anything that wasn't in the next release. We only did what was necessary, and we delivered faster than I've ever delivered before. Every detail is judged by it's ROI.

Conclusion
I'm a huge fan. Context is king and it might not work for you based on your constraints, but it's definitely worth considering some if not all of the ideas.

Thursday, August 07, 2008

Be Your Start-Up

It's impossible not to ponder striking it rich at a start up, but there's another alternative if you're looking to make some good money -- build your brand.

In January of 2005 I created a blog. I truly can't remember why I created it, but I do remember that I was making $60,000 a year. Then in February I joined ThoughtWorks and my blog became part of blogs.thoughtworks.com. I had instant readership, but not a lot to say.

Two and a half years and a lot of blog posts later I turned down an offer that was $174,000 a year. This wasn't $174,000 a year because the work sucks. It was a good job, but I wasn't ready to leave ThoughtWorks. It's been a year since then, and I know it was the right decision.

Now, I didn't make millions, but tripling your salary in two and a half years is still quite nice. There's no question that my brand was responsible for my ever increasing salary requirements. In fact, when I was interviewing two of the three interviewers started their interviews with "I read your material, you've got exactly what we need here. So why don't you ask me the questions instead". My brand had already gotten me the job.

Building a brand isn't as hard as you might think. There's really only 3 things you need to focus on.
  • Writing
  • Presenting
  • Contributing
If you do all three of those, you'll have a brand in no time.

But, before you do those three things, you'll need to read two books that will give you the proper guidance.note: both books are required reading whether you are building a brand or not.

Once you have the requisite reading covered it's time to make your mark.

Writing
Writing isn't hard. Writing well is hard, but you're a programmer, so you needn't worry about that. No one expects you to be a good writer, they're happy if you are, but they're forgiving if you're not. Don't use your poor writing skills as an excuse not to write.

Don't know what to write about? The answers are all around you. Anything you do that's interesting, there's 100 people searching Google for how to do it. Any question a colleague asks you, someone is searching Google for the same answer. Anything that's valuable to you... yes, someone is googling for it.

Take the time to write the answer in a blog entry. People will remember if your blog consistently shows up in their Google searches.

Create a blog. Blogger is free and definitely good enough. Here's a few thoughts on blogging.
  • Don't roll your own, you don't need the maintenance headache.
  • Name it something simple, but be sure to include your name. Disco Stu's Ramblings is funny, but you want to build your brand, not Disco Stu's.
  • Buy your name as a domain and put the blog as a directory (I didn't do this correctly). If I had it to do over my blog would be at http://jayfields.com/blog. This is important for Google Page Rank. If you do it the way I did you'll have different page rank for your homepage and your blog. Not the end of the world, but not ideal.
  • Keep it focused. If you want to write about programming and gambling, create two different blogs. Also, avoid "Sorry I haven't written in awhile" posts. People aren't dying for your next post. They're subscribed because they want technical content, not stories about how you've been busy remodeling your home.
  • Keep blog posts short if possible. Anything over 1500 words is article material. If you can't find somewhere to publish the article, then your blog is fine. But, remember that people don't like reading long blog entries (like this one)
At first, stick to writing blog entries. They're not too hard and don't require as much polish. They also let you start building a catalog of material. Once you have enough similar posts, start rolling them into articles to post on InfoQ or other similar sites. Eventually, if you write enough articles, you can roll them up into a book. It will take years, but so does writing a book from scratch and doing it this way allows you to get constant feedback without any deadline pressure.

Presenting
Of the 3 things you need to do, I think presenting is the hardest to get going with. You aren't likely to pick up a speaking spot at a good conference if no one has heard of you. So, you'll need to rely on your writing to build your brand a bit, then you can present on the topics that you write about. Alternatively, if you create some open source software that generates buzz you can also pick up a few speaking engagements. Getting started is the hardest part, but once you give a few good talks you'll quickly be turning things down because you just can't fit it in your schedule.

If you're serious about speaking I also highly recommend SpeakEasy. I spent two and a half miserable days at SpeakEasy learning how bad I was at public speaking. The experience was priceless. I can't say enough good things about what I learned in those two and a half days.

Contributing
The last suggestion is the easiest. Contribute some open source software. There's so many ways to contribute.
  • Pick an existing project that you use and create a patch for an outstanding bug.
  • Extract something interesting from your codebase and release it as open source. You don't need millions of adopters. If you create something valuable to 20 people you'll gain 20 people advocating your brand.
  • Be the documentation guy. Almost all open source software suffers from poor documentation. Make it your speciality. You'll quickly make friends if you do the work others don't feel like doing.
  • Join a mailing list for your favorite open source project and be active. Be the guy that's always willing to help the recent adopters.
I'm sure there's other ways as well. It doesn't matter how you contribute, just get out and start contributing.

Oh, and don't submit patches as disco_stus_brother. You're name is your brand, use it.