The Joy of Science

Put a Little Science in Your Life:

Science is a way of life. Science is a perspective. Science is the process that takes us from confusion to understanding in a manner that’s precise, predictive and reliable — a transformation, for those lucky enough to experience it, that is empowering and emotional. To be able to think through and grasp explanations…

Since my run in with Bachelor of Science-grade Physics, I’ve considered myself someone who sucks at science. However, I suppose by Brian Greene’s definition, I am a consummate scientist. I really enjoy diving into a deep subject (economics, linguistics, etc.) and trying to figure out what makes it tick. Its a fun way to go about life.

At the root of this pedagogical approach is a firm belief in the vertical nature of science: you must master A before moving on to B. When A happened a few hundred years ago, it’s a long climb to the modern era. Certainly, when it comes to teaching the technicalities — solving this equation, balancing that reaction, grasping the discrete parts of the cell — the verticality of science is unassailable.

A hearty "Amen!" here. So many topics seem intimidating to the neophyte. "You can’t do this until you’ve learned this, that and the other." Stacked knowledge as barrier to entry is a total bummer.

I think something immersive is more rewarding. They say the best way to learn a foreign language is to surround yourself in it. I think this is true of any endeavor that, at some level, rewires your brain.


Yurii Rashkovskii's Blog: Top 10 Reasons to Avoid Document Databases FUD

Yurii Rashkovskii’s Blog: Top 10 Reasons to Avoid Document Databases FUD:

And… you said “relational”? Facebook and others do a lot of denormalization, they don’t ever use JOIN, they’d rather do several consequent requests and build intermediate results on a webserver (when you have 20 times more webservers than DBs it’s obviously good to move some load there). They treat good old MySQL as object storage with very fast B+ tree indexes. Finally, the resulting database is not a relational one. One thousand of MySQLs is just a distributed object storage with simple fast indexes and a bunch of hand-written code in php/ruby/python/whatever around it.

I’ve come upon this sort of idea several time recently (and the above was written a couple months ago). I’m warming up to the idea. Without piles of cash and able systems-type folks, scaling databases out is a really nasty problem. Even then, my reading is there are definite bounds for how far you can go.

Assembling datasets on the more easily scaled app server is appealing. It sounds fun (hey, real programming!) and is interesting to think about. But I wonder if it leads to having to figure out consistency in your application. From where I sit, its the hardest part of ACID to reason with.


Microsoft's spin on memcached

Microsoft cargo cults memcached! , via Simon Willison.

Back when I worked in a semi-.NET shop, we needed to cache some pretty expensive operations somewhere. In memory would have been ideal, but we had no way to coordinate between application servers. It ended up going in the database, because that made everyone feel safe. In hindsight, it probably wasn’t the greatest idea.

Long story short, my internal clock that tells me how long it will take Microsoft to clone a forehead-slappingly-obvious-idea is well set.


Fake Rails environment

For testing some bits inside of ActiveRecord proper.

module Rails
  def self.env
    o = Class.new do
      def production?
        true
      end
    end
    o.new
  end
end

Evil and fun. Uses Class.new, my favorite Ruby method.

Muahahaha.


Oh, The Fail I've Known

Please to enjoy my presentation for RailsConf 2008: Oh, The Fail I’ve Known (PDF).

Its on the things that aren’t normally covered in books and websites programmers read. The things that you really need to know if you’re going to achieve truly awesome developer status.

Obviously I think they’re really important topics. Digging into them has really helped me as a software developer. I hope its helpful to you too.


Thanks to everyone who caught me afterwards or emailed to say they enjoyed the talk. And of course, if you enjoyed those videos, kudos to you as well!


See me at RailsConf '08

As hordes of Ruby and Rails folks begin the annual migration to Portland for RailsConf, I thought I’d let you know how to find me there this year:

That rounds out the conference activities. But I’d be remiss if I didn’t inform you that FiveRuns would like to buy you a drink or two Friday night at Jimmy Maks from 6 to 8 PM. Please to be joining me there!

What I am perhaps most excited about is the RailsEnvy videos that will premiere this weekend. You see, Jason and Gregg were kind enough to invite me to join them in making the funnies this year. Making them was a blast! I’ve seen the finished product and, in my completely biased opinion, I think you’re going to like it.


Of course, I’d love to chat with you (yes, you) at any point in the conference. So if you see me (and I will probably stand out), come say “Hi!” I’m hoping to have something interesting for those that do…


Pinky and the LOLdog

puppies

I made you a muxtape

I hope you enjoy listening to it as much as I enjoyed compiling it.


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.


Frogged

DSC_5651

My current foster-hussy-girlfriend. She’s kind of a spaz, but oh-so adorable. Doubly so when she’s “frogged out” as above.


More fu in your versions

Lazily Announcing version_fu - an update of Rick Olson’s acts_as_versioned that works with dirty attributes. Jordan McKible’s plugin is nascent, but since I have a soft spot in my heart for most things data versioning, I thought I’d point it out.


Info viz with JavaScript

Massive kudos to John Resig for his JavaScript Processing port. Take this plus the new-to-me JavaScript Information Visualization Toolkit, and it seems likely that data on the web is going to get a lot prettier (and less Flashy) in the next 6-12 months. Huzzah!


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.


Ruby for non-Rubyists

Yesterday I spoke to a pleasant mix of Java, .NET, Ruby, Python and PHP developers at Dallas TechFest. My goal when speaking to enthusiast crowds of this sort is to show the light that I’ve found in my programming journeys over the past couple years. This time around I tried to take a page from the inimitable Richard Feynman by structuring my talk into two sets of “Six Easy Pieces”.

The first part starts off with the stance that programming shouldn’t suck. From there I talk about the intercontinental railroad, Sapir-Whorf, Pattern Languages, the Gang of Four and flattery. In the end, we have an idea of how to better approach programming so we can have fun doing it.

The second half is partially showing off Ruby and partially a gauntlet thrown down to other languages. The main point is to show a progression of ideas I see in lots of Ruby code, from sensible naming to closures ending up with metaprogramming powering declarative programming and internal DSLs. You can implement the ideas from the beginning in any language. However, the ideas towards the end require a more progressively designed language. I’d love to see non-Ruby implementations of the programs towards the end of the presentation, if only for comparison and Rosetta Stone purposes.

Thanks to everyone who was in attendance and especially those who stopped to chat with me before and after the presentation. Without further ado, please enjoy Six Easy Pieces (Twice Over).


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?


Halo Photography

Joshuadamon’s Halotography is utterly amazing:

Shadowrun.jpg

I’m really impressed with what he’s done with some clever camera manipulation and probably some Photoshop loving. All Halo fans should check this out.


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.


Some language twins teach each other

SNL Transcripts: Luke Perry: 02/06/93: Weekend Update with Kevin Nealon:

That wasn't English, Keith! I mean, you're talking in Esperanto, or some language twins teach each other! I mean, the King's English, man! I mean, throw us a bone man - alright!

I wanted to post a video of the Weekend Update skit where Mike Myers plays Mick Jagger and Mick Jagger plays Keith Richards, but it appears such video does not exist on the web. You’ll just have to read the transcript and make it happen in your head.


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!


Reverse shoulder surfing

Rands In Repose: Saving Seconds:

This is the presentation I want to see at the next conference: in a room full of people, anyone is welcome to walk up to the mic and plug their laptop in to the projector. They’ll be asked to complete three simple tasks:

Send a mail to a friend

Find something on the Internet

Save a bookmark or an image.

I would be fixated.

I’ve been independently wanting to do this for a while now. Clearly, Rands was telepathically borrowing ideas from my brain when I met him at SXSW this year :). I’ve been wanting to do something like this at a BarCamp for a while now. Personally, one of my favorite past-times at conferences is to shoulder surf other people. The idea above takes shoulder surfing, turns it around and formalizes it. I’d have a blast watching it, especially if you get a good mix of Windows/Linux/OS X people and GUI/terminal folks.