Third Day of the Craftsman Swap

Day Three of the Craftsmanship Swap:

Micah and Paul had to head out of town for an Iteration Planning Meeting/Demo so it was just Colin Jones, Eric Meyer, and I in the office. We decided to do some tri-ping pong development on an internal product 8th Light is planning to release soon. So Eric would write a test/spec, I'd make it pass and write another spec, then Colin would make my spec pass and write one for Eric and the whole process would repeat. An enjoyable way to work made easier by their 24inch Cinema Displays and wireless keyboards.

I've noticed that everybody shakes hands at 8th Light. A lot. When someone arrives in the morning the new person tends to shake the hands of all who are already there. The same thing happens when someone leaves for the day. I asked Colin and Eric about the origins of this but they had no answers for me. It seems kind of weird, but it's also sort of a cool affirmation of camaraderie. It may just be better than the grunt I give people when they leave for the day.

At some point I found myself arguing for these lines of Ruby today:

return false unless coupon = Coupon.find_by_token(session[:coupon_token])
return coupon.active?

Instead of a 5-6 lines of if/else code. Have I become the very person I despised when I got into Ruby? That guy who can't rest until lines of code have been reduced to unreadable terseness? Even inject doesn't look so bad to me anymore...

Of course, if we were using a current version of Rails we could do this:

!!Coupon.find_by_token(session[:coupon_token]).try(:active?)

Now that's teh hotness.

Comments

Dan Kubb said…
I'm not a big fan of using !! in my code, but I have been known to golf down my code in an attempt to find something more readable for me.

With that said, I'd probably write that code like so:

(coupon = Coupon.find_by_token(session[:coupon_token])) && coupon.active? || false
Hongli Lai said…
This '!!' idiom is getting old and I can understand that some people are confused by it.

How about monkeypatching Object, Boolean and NilClass to add a #to_boolean method? (expression...).to_boolean probably makes more sense than !!(expression...)
Corey Haines said…
I remember my first time at the 8th Light offices and being so impressed by the hand-shaking. I now try to do it when I go places, although I sometimes fall back to my old habits.

There is something really invigorating about coming into an office, and, before you do anything else, you walk around and shake everyone's hand.
Adam Walters said…
why would one use '!!' ? I've always wondered this when i've seen it in others code, but have never gotten an answer
Jake Scruggs said…
Adam -- first of all the one liner I ended the post with was kind of a joke. I don't think I'd put that line in productions -- it's a bit obfuscated. As to !!, I like to return a real true or false from interrogative methods like "active?" instead of things that merely evaluate to true or false. So !! will take anything that evaluates to true (anything object that isn't nil or false) and turn it into a real "true." !! also turn nil into a real false.

Some hate it. I use it sparingly.
Stephen Wooten said…
stupid jake and his stupid hotness. Glad to hear you're still rocking out

Popular posts from this blog

Point Inside a Polygon in Ruby

SICP Wasn’t Written for You

What's a Good Flog Score?