In the agile development world, I often hear the term ‘kit of parts’. Typically it refers to a goto set of practices or ideas that one might use to put a project together. The “parts” are things like activities to get clients and developers thinking in tandem, tools that will be used during the lifetime of the product, etc.
As a developer, on a much lower level, I have a small kit of parts that I bring to most projects. These are little bits of code (often recorded as gists – http://gist.github.com/bunnymatic) that I go back to often, or include in most every project. Hopefully other developers out there might also find them useful in their projects.
- Ruby One(ish) liners
- Git Flow and Pivotal Tracker Story Management – git_pivotal_tracker
jQuery plugin template
After writing a few jQuery plugins, I found a good chunk of code that I used every time. I put that together as a template which I use to start any new jQuery plugin. Simply start with this template, do a global search/replace to change myPlugin to the name of your new plugin, then start fleshing out the real plugin.
As I use CoffeeScript more and more, I modified wrote up the CoffeeScript version as well.
console.log(). The following snippet seems to do the trick. It is designed to be off by default. In a dev setting, you can set
__debug__=true to turn it on.
Ruby one(ish) liners
Here’s a one-liner that came out of some discussions from a different project. I’ve used this many times on admin dashboard pages that have some kind of tally of events per user or method calls per day etc.
Tally expects an array of pairs where each pair is a key, value and the values are numbers. It will sum up all the values for each key and return a hash with those totals.
Histogram expects an array and will return the number of times each key appeared.
You could imagine counting the number of times the word “the” appears in a text. A script using this histogram method would look like this:
wordcount = histogram(File.open('myfile.txt', 'r').read.split) puts wordcount['the']
Pagination Helper – upto and including
In recent work on a search results page, I wanted a dropdown that allows a user to choose how many results to show on each page. I wanted it to show options [12,24,48,96]. But if the number of possible results was small, it should figure out the maximum value to show. This could be done with some group of if/else or when/case. I decided to use this little one-liner:
In the app, it looks like this:
@per_page_opts = upto([12,24,48,96], @num_search_results)
Git Flow + Pivotal Tracker Story Management
git_pivotal_tracker is a tool I use every day, many times a day. It simplifies the whole process of finding a story in Pivotal Tracker, starting it, building a branch, merging the branch back in, and finally marking the story done. If, as a team, everyone is using it, it also makes the git log terribly consistent and easy to read.
My typical work flow is something like:
- start a story in Tracker via a browser
git checkout development # get on the source branch git checkout -b feature-12345-my_new_feature # develop the feature git checkout development git pull # make sure we're up to date git checkout - #jump back to the feature branch git rebase development # run tests git checkout development git merge --no-ff feature-12345-my_feature
- click ‘finish’ on the story via a browser
Using git_pivotal_tracker, that sequence looks more like this
git feature # develop feature # run tests git finish
Process simplified. If you use Tracker and git for your projects, I’d encourage you to check it out. It follows basically the git flow laid out here by Rudy Jahchan at Carbon Five.
There are certainly more goto scripts and tools that I use, but these are a few in my arsenal that I’ve worked on and with and which seemed worth sharing.
Let me know what your favorites are – your goto tools, your one-liners that end up in every project.