All posts by Eumir

Courage to View Source Code

As mentioned in a previous post I have been reluctant(before) to “dive into” a gem. Before, I always trusted that a gem, especially if it is widely used to have fixed obvious bugs. I have since learned that this is not the case and that we must always look into these gems as much as possible or at least know how they operate.

A recent bug we had was a bit confusing indeed: in Radiant, we were skipping a before filter because one of the features called for unauthenticated users to create a resource(nomination). Since all other actions required an admin, then we just basically added

skip_before_filter :verify_authenticity_token, :only => [:create]

Radiant also has a no_login_required method but we couldn’t figure out how to call it only for create. So we just tried testing it out and it worked.

A couple of weeks later, we received a bug report that in IE9, creating a nomination redirects to the admin login form, which was totally weird since everything works in other browsers(okay, not totally, since it is IE – but it IS a ‘modern’ IE)

Anyway, we double checked our code and found that we left the no_login_required clause commented out on purpose(we usually just delete code that isn’t used). We then decided to just get on with it and look into radiant as to what that notorious no_login_required does. Back when I was still a noob, I would’ve been scared to do a bundle open radiant just because “I wouldn’t understand the code…” – but we’re past that now.

We found out that what no_login_required does is basically just:

skip_before_filter :authenticate
skip_before_filter :authorize

It was pretty simple – just add those two filters and add the :only => [:create] clause on each and we’re done!(quick fix, refactor to override no_login_required to accept arguments/options).

These are the times I am proud that I am not that scared in opening up gems anymore. I don’t trust gems that much anymore that I now do bundle opens regularly to understand more on how something works. It’s the equivalent of fixing your own stuff – fluorescent light not lighting up? Check the bulb first before calling the repair man. It might just be a problem with the bulb or the fuse which could be easily fixed.

Don’t be afraid to jump. Be curious.

Narrowly Escaping Git Hell

Recently, I have been working (again) on a Compass port of the famous Twitter Bootstrap framework. There have been many ports of it, but none that used mixins. Almost all just translated everything to SASS and made it into a plugin. What I wanted was a bunch of mixins so you don’t have to put “span4″s in your markup (which I really don’t like).

Anyway, I had been working on it a while back and left it in a dirty branch. I started working on the develop branch and now, I wanted to merge it back to master – only I knew there were so many changes, renames etc, that would bring me to git hell.

A quick Google search gave me my ticket to bypass git hell.

Basically:

git checkout develop
git merge -s ours master
git checkout master
git merge develop

Sweet!

Trusting Tools Too Much

This morning, we had a ticket regarding the publish times of posts in a site that was using Radiant CMS. The clue we had was that the publish time and the created_at time were different – which meant that somewhere, the published_at is being set and saved in the db without UTC. I searched the code for a good hour or so for terms like “published_at”, “publish!”, “timezone”, “TimeZone”, “local”, “utc”, etc. to no avail. I still couldn’t find where it is happening. We already had a debugger right before saving it and here are the results:

(rdb:2) page.created_at
nil
(rdb:2) page.published_at
nil
(rdb:2) page.save
true
(rdb:2) page.created_at
Thu, 23 Aug 2012 10:30:36 EST +10:00
(rdb:2) page.published_at
Thu, 23 Aug 2012 10:30:36 EST +10:00
(rdb:2) page.reload
#
(rdb:2) page.created_at
Thu, 23 Aug 2012 10:30:36 EST +10:00
(rdb:2) page.published_at
Thu, 23 Aug 2012 18:30:36 EST +10:00

As you can see, right after saving, the published_at is the same as created_at, but after reloading, we see it change! I checked the db structure and I didn’t see anything different. At that point, thoughts on Radiant bugs flew into my brain.

“Maybe this is a Radiant bug?” “No it couldn’t be – I searched already and no one seemed to point it out.” “It has to be – it’s not in our code!” “No, if it was a bug, somebody surely would have fixed it”

Needless to say, I trusted Radiant too much to even check. Finally, I did relent and tried opening the Radiant gem. And there it was. Right before saving, Radiant used Time.now when saving published_at. From this StackOverflow answer it is mentioned that Time.now used the machine’s OS no matter what you set in Rail’s config.time_zone. This was the flaw! And it WAS in Radiant and not in our code. Having fixed it, I checked Radiant’s github again and saw that it was fixed at some point. We were just using an older version – it probably was fixed without people knowing about it.

So there you have it. Don’t trust your tools too much and always consider EVERY option.

Contributing to Open Source

Here at Aelogica, we love open source. All of us have, one way or another, created an open source project or contributed to it. Contributing to open source is very fulfilling – people contribute to each other’s work and it is a great way to learn new things. I’ll talk about this in detail in a little while so hold on to your seats. Most of my points may have already been said before but hopefully, if you haven’t contributed or created any open source projects yet, this post might make you think about doing so.

Creating an Open Source Project There are a lot of posts (and even books) about creating/maintaining open source projects so I’ll only state what I think and have experienced about it. FIrst, being forked (for the non-technical, basically someone copies your code to modify or use for his own purposes) in Github gives me the feeling of importance and that my work was valued. It gives you that sense of accomplishment comparable to seeing people use a phrase or word you just invented (or a fad you started – not that I have started any fads). Most of us have created open source projects and I’ll use this opportunity to showcase some of them:

Contributing

For me, contributing to open source is very fulfilling. It gives me almost the same feeling as creating an open source project that was forked. You don’t have to be a really bad ass programmer just to contribute. Contributing can come in many forms: creating issues (bugs that you encountered and don’t know how to fix), participating in a discussion on how to move forward with a project (some individuals that have a huge amount of followers usually consult with their users through a mailing list – asking for suggestions on how to tackle a problem), and even correcting spelling/grammar mistakes that might lead to confusion. Pointing out bugs (don’t forget to also show how to reproduce it!) helps developers greatly since most of the time, they don’t have the means to QA everything their apps/tools do. That means it is up to the community to help with spotting (and fixing, if possible) the bugs that appear.

Interacting with our Heroes

I am easily starstruck by my own heroes and idols. I’m pretty sure a lot of people are too and that’s also a great motivation to working with their projects. A couple of my heroes are Aslak Hellesøy (creator of Cucumber) and Chris Eppstein (creator of Compass and core team member of SASS). I pretty much use their creations everyday so when the time came when I wanted to give something back, I helped out as much as I could. They are also VERY active in their respective mailing lists – almost replying to each of the emails there – making sure that the community is active and that newcomers feel welcome.

Back when Aslak requested people to come into the cucumber chat room to help out with merging all the Cucumber Textmate Bundle forks into one (and making it officially under cucumber’s repo), the best I could do was to join in and look at the logs to see which ones were good or not. I knew NOTHING about Textmate bundles, and the only reason I was there was because I USED Textmate (before) and I loved Cucumber. Even if Aslak and the others did the coding and stuff, they made me feel like I was still of help even by just checking the commits of each one.

Another time I helped my hero was when Aslak requested someone to create a script to mass message all the forkers of the Cucumber Textmate Bundle. I did that script during my free time and that knowing he used it (he even helped me test it!) made my day. It’s like helping Spiderman or Batman by throwing a rock at an enemy he’s fighting with – you know they can beat that guy up even without your rock, but you also know that somehow, you made their fight much easier.

Contributing: Learning New Things

Contributing to open source also makes you learn things – things that you would otherwise not care about given other circumstances. Case in point: I suggested to YADR (this is the current dotfile package most of us use now) that vimrc.afters should be in /vim/after, not in root – to minimize the files in root and so everything will be in your yadr folder. To be honest, I was expecting them to actually fix it, since I knew nothing about customizing Vim. The reply: “Please do implement and pull request :)” made me stop in my tracks. It was very polite of Yan (the owner) to say “if you really like it, do it”. Having no experience whatsoever with Vim plugins (I relied on other people/plugins to customize my Vim), I didn’t know if I should just leave it until someone else does it (someone actually agreed with my suggestion).

Instead, I thought of it as a challenge so off I went and experimented – besides, it was just a simple path fix, what could go wrong? Sure enough, that simple fix took me longer than expected. I perused the Vim help files and looked at the definitions for filereadable, expand, and the filename expansions for Vim. It was indeed a challenge (for me) so in the end, I had to concede and assume that the file was in ~/.yadr/vim/after instead of ~/. My original solution was to direct relatively so even if yadr was installed elsewhere it would have been okay. In any case, I still learned something new in the process of contributing, so in the end, it was still a good experience (not to mention it was merged to master a few minutes after the push!).

I hope that a lot more people will start their own projects and contribute to projects they love and use. It just makes the (software) world a better place to live code in.

The Aelogica Style Guide

In one of our Tuesday lunches, we discussed what our favorite tools were. We discussed the pros and cons of each and we mostly agreed with everything. This was so that we were all at the same page when coding and so everything is consistent. As a side note to this, we also planned to create a style guide for everyone so we all followed the same coding conventions, code style, etc. as much as possible.

We based most of our stuff from existing guides, and added some examples and changed some based on our preferences and own opinions. Our primary source was Github’s own style guide here: https://github.com/styleguide/

Some were also taken from existing blog posts (and rightfully credited – if you are the author and have issues, don’t hesitate to send us an email!). We think that this might help other people as well, so without further ado:

The Aelogica Style Guide

Weekend Project: Vimercises

Buddy and I were discussing how to develop our Vim skills and it led to us thinking about a game much like http://vim-adventures.com/. We discussed about creating a game where given a task in Vim, you’ll compete with other players on how fast you can finish the task.

We thought about creating a server and making people log in etc, but that’s kinda hard to set up since we’ll have to deal with security, server load, etc. So I made a quick prototype of our idea and modified it a bit.

Enter Vimercises.

These are a set of exercises that will test your knowledge of Vim. It is designed for beginners and intermediate Vim users such as ourselves(Ramon is our resident Vim expert having used it for a couple of years I think). Anyway, most of these tasks are based on our every day experiences while pairing.

Hopefully this gets to help people and make them better Vim users. Next up: a post on how the project page was setup using Jekyll, HAML, SASS, Compass and some thoughts on Vimercises version 2 – going social!

Compass, Rails 3.1 and the asset pipeline

I am part of a volunteer group of developers working on a volunteer portal for the Philippines. I haven’t been helping out a lot because the main state of the site right now is that it has a lot of bugs which I think can probably be fixed easily by adding compass into it.

It is a rails 3.1.0 app and I’ve done my fair share of googling, asking in the chatroom(no replies), the mailing list(also no replies) and even in stackoverflow. At one point, I filed a bug in compass-rails and eventually they did see that it was a bug. For compass-rails to work on your rails 3.1 app, it has to be at LEAST 3.1.1

I upgraded the app to rails 3.1.3 with the ever so amazing bundler and kept trying. Every couple of weeks(yes it took me that long!), I’d try to start a new branch and try to make it work, but after a couple of hours of fiddling and googling, I’d give up. I always followed the steps mentioned in compass-rails’ github page, and I still couldn’t make it work. It still keeps giving me the error:

File to import not found or unreadable: compass/reset.

Finally, I created a new rails 3.1.3 app and then tried the instructions there. And voila! It worked! Now the only thing I had to do was to compare the files and changes on each project. I first looked into the config/ directory and by process of elimination, found these offending lines in config/application.rb

If you have a Gemfile, require the gems listed there, including any gems

you've limited to :test, :development, or :production.

Bundler.require(:default, Rails.env) if defined?(Bundler)

While I did upgrade to rails 3.1.3 using my Gemfile and bundle install, I didn’t think that the generated configuration files weren’t updated in turn. My config/application.rb was still a rails 3.1.0 version. The [working] rails 3.1.3 version had this:

if defined?(Bundler) # If you precompile assets before deploying to production, use this line Bundler.require(*Rails.groups(:assets => %w(development test))) # If you want your assets lazily compiled in production, use this line # Bundler.require(:default, :assets, Rails.env) end

I copied this over and everything magically worked!

My Experience with Pair Programming

Here at Aelogica, we pair 95%(or more) of the time. As the first developer here to join Steve, I experienced and learned A LOT from pairing – either with Steve or from my colleagues. The first time we paired, I was still part-time, so the session was just a little under four hours long – but I felt like I have coded the whole day and I had a week’s worth of experience under my belt.

A bit of a backround: I was more of a front end Rails dev focused on HAML/SASS and I knew about intermediate Rails concepts. I knew the basics, but was not able to do metaprogramming to save my life. In any case, after my first week of pairing, I felt I had learned a month’s worth of experience already.

This trend continued when I finally jumped in full time. Eight full hours of pairing felt like I was thrown in the sea to learn how to swim when I was only training on the 6-feet end of the swimming pool. I never knew that I could go on that pace for eight hours. I usually go on bursts of two hours until I’d take a break then go on again. Pairing wasn’t like that – it was very tiring since you’re on fifth gear the whole time.

At that time, I thought I’d burn out but a project came to us where I had to do it alone while Steve did some other project. This was a bit of a breather, but at some points where I needed help, Steve was always ready to jump in and help out.

Today, there are eight of us and we still pair every day. It makes life easier for us. Whenever I feel lost, as everyone does at some point, my pair is usually be able to explain and make everything clear again. Of course, when I think my pair is lost, I suggest we both go to the whiteboard to visualize the problem and the solution. Pairing now has become such a normal part of our workflow that whenever we’re alone on a task it feels so dull. The day seems to slow down, the task gets harder (even if it’s not) and your brain drains faster.

Without a pair, you feel like a hermit crab without a shell. You feel less certain. “Am I really on the right track?” “Did I leave an obvious bug in there?” “How should I fix this?” It feels a little strange to get so accustomed to working in pair. I still code alone sometimes but when there a big project awaits with an epic new feature to build or a new critical bug demands attention, I prefer to tackle it with a pair. I don’t think this is a bad thing – since we’re dealing with large projects that clients and customers use, I wouldn’t want to break it or commit a grave mistake. Pairing makes you more careful than you might be by yourself.

It’s better to have a second set of eyes to catch details you can easily miss. Sometimes you process things too fast (or too slow) and you can overlook or forget things that might break half of the app. When dealing with inherited legacy code that might not be fully tested, it is especially important that someone else checks what you are doing.

Looking back on the last year and a half at Aelogica, working primarily in a pair configuration, it is hard for me to imagine doing it any other way.

A Git Trick for Pivotal Users

I always keep forgetting the pivotal ticket number of the bug/feature I am working on(who doesn’t?) What I usually do is to alt-tab to my Pivotal Dashboard, look at the ticket number, copy it, then paste on my commit message.

Pretty tiring for a ‘lazy programmer’. So, since we’re all lazy, I found a way to get the ticket number faster than you can type “woot!”. Actually, this only works when you’re committing multiple times to a single ticket(which we usually do).

I basically just do a git log, look at my most recent commit and copy the ticket number from there:

[#15757415] fixed fonts and restructured blog sidebar to make it more DRY

Now take another step back and being lazier, I bound a command to an alias so I can peek at that log by just typing three letters. So of we go to edit .bash_profile and add this line:

alias 'git log --oneline | head -1'

Tada. If you’re THAT lazy, you can even add a grep and throw in a pbcopy so all you need to do is paste it. But I like to double check that the ticket number I’m copying is the correct one, so no pbcopy for me.

Simplest things in life: Vim

Not trying to start editor wars here, but changing from TextMate to Vim was one of the best things that have happened to me. Recently, I was investigating a bug involving something about a ruby script that’s 1000 lines long. I didn’t have access to the original file so I had to create a 1000 line ruby script.

My first try was to create a ruby script that will print out 1000 lines using puts. This was easy, with File.open, although I still had to format etc and took me about five minutes.

Enter Vim.

I had to rewrite the code by not using puts and using a real, non-cpu extensive ruby line that will just check if text exists in a browser. Instead of writing a ruby script again, I remembered about Vim’s power. To cut the long story short, all I did was yank the line I wanted to execute 1000 times(can’t do a loop since the bug was supposed to have happened because the script was 1000 lines long) and then just typed:

1000p

which basically just pastes the line 1000 times. Trying to think about that in TextMate without any plugin, I would have had to do an exponential copy-paste, where I duplicate the line, copy the two lines, duplicated the 4 lines, etc.

It would have took me more or less 5-10 minutes also, along with just writing it in ruby which would take about 1-3 minutes. Vim did it for me with just 5 key presses which roughly translates to 1-2 seconds.

Sweet stuff is sweet.