Iteration vs Recursion… Or Andre vs Sabre.

So, I dig pro wrestling.  Pretty much always have.  It’s a curse, for sure.  There’s really no cool way to tell your friends that you can’t go to the pub because you’re going to spend your night watching oiled up men express impotent rage at one another before engaging in a pre-determined act of PG friendly fist a cuffs.  I am who I am, man!

Either way, let’s take my embarrassing pass time an make something useful out of it.  In programming you have two major paradigms that are fairly important to understand when building out algorithms.  One is Recursion and the other is Iteration.  Now, iteration is like Andre the Giant… This guy.


I mean, how could anyone not think that’s cool?  Either way, Andre is like iterative ideas about building out algorithms.  He’s well known, easy to understand, and pretty cool at first glance.  The ugly truth is, and I do mean ugly, is that Andre kinda sucked as a pro wrestler.  I mean, we was big, for sure – and his strength was prolly pretty high – but when it came to the art of wrestling, he was just kind of a stiff, hulking worker who only had like 5 moves.  Iterative principals are similar to that.  Everybody knows ’em (programmers = everybody), but really they can be kind of stiff.

Now lets talk about another guy:  This is Zack Sabre Jr.


I know, right?  Why would anybody ever like this guy?  I mean, the hair, the jacket, hell – he ain’t even huge, man!  Oh, but dear friends, if ya just saw him in action!  I mean, he’s like beyond versatile!  High flying one minute, technical ‘on the mat’ wrestling the next, chaining together submission at a dizzying pace.  Ah, it’s like art, man… Let’s compare Sabre to recursive paradigms.  At first glance you have a hard time understanding why anyone would like them.  It’s just overly confusing, man!  Iterative does the same thing!  Well, iterative and recursive do do the same thing in the same way that good ole Jack Sabre and Andre both wrestle.  It’s the subtle differences, man!

A recursive function is one that calls itself again and again in the code until a given answer is reached.  This, at first glance, seems counter intuitive and a bit confusing.  Just like my man Jack Sabre.  Fear not, though, once you get into the swing of working in a recursive manner you will wonder who you ever held iterative up on such a weighty pedestal.  Huh, just so happens I made a video tutorial about!  You lucky bastard… Watch, enjoy, get into pro wrestling in your spare time and then lie to your friends about it.


Bubble sort and the old rain coat.

People are always askin’ me what is the best way to learn how to think like a programmer.  Well, I don’t know that I’m the best bloke to ask, but nevertheless I always have a simple answer – sorting algorithms.  Yep, that’s it.

There are several, and I’m going to teach all of them, but for right now we are going to focus on the JV 3rd stringer  (I mean, it might even be the team stat keeper or equipment wrangler) of sorting algorithms: Bubble sort!

Look, bubble sort is like totes not effective – for sure.  It’s got heart though, and ya can’t teach that.  Bubble sort is like an old rain coat that has a few holes in it:  It’s bulky, ugly, and barely works – but, in a pinch, it can save the all to important first impressions on an unbelievably awkward tinder date.  That was terrible, MA’BAD!  Either way – Bubble sort is a good first sorting algorithm to learn, and ruby makes it all the better with it’s sugary sweet syntax.

I tried to explain in the video what bubble sort actually does, but alas, my monotone ‘not for radio’ voice and my generally poor communication skills betray me.  So, I will go ahead and let the nerds at wikipedia show you what bubble sort actually does.

Nerds at wikipedia’s take on bubble sort

So, common, stop standing there waiting for things to get easier – LEARN! NOW! I WISH I WOULDA HAD SOMEONE TEACHING ME THIS STUFF!  I HAD TO LEARN THE HARD WAY!  Through cheating… Jokes. Video below, NJOY! Don’t troll me, BRO!

Fakin’ namespaces in Javascript

Look, namespaces can be faked!  Nuff said!  If something can be faked, do it!  That’s my motto!  Got me through college, for sure.  But – I digress.

Hmm, take aways?  Javascript doesn’t have namespaces – but wait!  What are namespaces?

Namespaces are wrappers to keep variables seperate and to stop variables with the same name from colliding.

So let’s say you got a variable with a generic name like ‘greet’ or something.  Then, somewhere else in your application, someone decides to make another variable called ‘greet’ that has a different value… Eww wee, that can become a problem – like quick.  SO, you can take your variables and attach them to another object other than the global object.  simple.  The video below will show you exactly how to do it!  Enjoy friends!



I mean, it’s hard to believe if you don’t know me all that well, but it’s true:  I dig humans.  And not like in a cynical way where I’m forced to back door myself into believing something because it might help my psyche or something – nah, nothing like that.  I actually, really, honestly like people.  A bunch.

A few years back I read a book called ‘The Monk and The Philosopher’ by this dude.


I know what you’re thinking.  Buddhist dude sitting in a field of flowers….. Mmmkay… How much more of a cliche can we squeeze outta this blog post?  Well, here’s the thing – That dude, Mattieu Ricard – is supposidy the happiest man alive. Crazy right?  How can they even measure that?  Well, they can – Microspectrometers and whatnot.

They basically hooked up ole’ Mattieu to some head nodes at the university of Madison Wisconsin – I know what you’re thinking – Madison Wisconsin seems like a weird place to do that.  Well, yeah – I thought so to.  But there is this other guy – richard.jpg

This guy talking to ole Mattieu is Richard Davidson – and don’t let the generic ass name fool ya, he’s a heavy hitter in the contemplative neuroscience scene… As small or large as that might be… EITHER WAY, back to the point – Davidson has some state of the art facilities up in Wisconsin and he’s super into trying to figure out what makes humans feel certain emotions.  Specifically, he’s after what makes people feel contentment or a sense of at ease or…. Happy, for lack of a better word.

When I was reading the book by Mattieu he went on and on about Altruism.  Altruism…. Hmmm – what is that?

altruism: feelings and behavior that show a desire to help other people and a lack of selfishness

Mattieu said that the key to happiness was helping other, and paradoxically a selfless behavior pattern actually made a given person feel that much more comfortable in waking life.  I thought that was pretty cool – and I was a liberal arts student, so I started reading up on it.

The idea stuck with me like a bad case of… I can’t think of an analogy… But still, it stuck.  I read another book by Sam Harris – another generic name – another neuroscientist – but he also talked about Altruism, and even more – non reciprocal altruism.  That’s like altruism on steroids – true SELFLESSNESS.  Helping for the sake of helping without even expecting to feel better about it.  Almost as principal rather than behavior.

So, I read about all the good that altruism had to offer – figured I would try it out myself.  I started helping people as much as possible while simultaneously taking the spotlight off myself as much as I could.  I’m not trying to virtue signal over here or say that I’m the most altruistic person walking the Earth, but still – it did seem to make me feel better.

I was lying in bed one night and I got the idea about an app that could link people together based on non reciprocal altruism. BOOM, an idea, it stayed – and I started writing the code for what would become SpareHand.

Well, I’m happy to say that I’ve been working on it in my spare time and it’s nearly ready to launch.  There are still a few things that have to be worked out, but it’s better to show that to keep it hidden away I guess.  My buddy Corey who is a designer is going to make the thing look prettier, but other than that I only have a few more features to implement.  So, here it is, the first look at it.  I’m proud of it, and I think when it is completely fleshed out, it’s going to be something really neat.



Unk Bob and Bowling Kata.

I’ve gotten the impression that the Bowling Kata is good for the ole’ TDD skills.  It’s a fairly easy KATA once you understand how an actual game of bowling is scored… I didn’t at the beginning of the Kata.  Either way, it’s good to work with Rspec daily, it’s good to know how to score a game of bowling, and it’s pretty good to make a video about it.  So, yeah – that’s what the KID JUST DID!  Link below!  Enjoi!

STI (revisited)

I wrote a small article about single table inheritance the other day.  After receiving a few questions about it, I figured I’d make a video tutorial… SO, I made a small video tutorial tonight about it.  Cheers.


Eager loading.

SO, What’s the problem?

In active record query interface there is a lot of room to get in your own way and slow down the response time.  The #each method is a prime place to fall into this mistake.  Lets say you have thousands of Accounts – and lets say that the Account model is an STI.  Some of the accounts are type ‘client’ and some are type ’employee’.  What if we wanted to find all of the employees that have ‘billed_time’.  This doesn’t mean that every single employee in the Accounts table will have ‘billed_time.’

So, let’s say that we want to find all of the employees with billed time.  In the active record query interface we might run something like this

Employee.all.each { |employee| puts employee.billed_time.count }

Seems pretty simple, yeah.  Well, let’s take a look at it.

This query is actually running several queries.  First, the Employee.all.each means that EVERY single employee will be check to see if they do in fact have any billed time.  If you have a rather large data set, this can get expensive.  So, it goes through each employee, finds the ones that have billed_time and adds to the count.  Is there a more effective way of doing this?

So, what’s eager loading?

Eager loading is the mechanism for loading the associated records of the objects returned by Model.find using as few queries as possible.

Whenever you ‘pre-load’ a list of items, then you will not have to go through each row of the database looking for records that match your query.

Let’s look at it like this.  Let’s say that you want to take the same query as the above – you want to find all the employees from the Account model and see how many billed_time entries it has.  We can preload only employees who fit the first criteria, then use the .each method to iterate over the list we have preloaded.  Let’s try.  First we use the #includes method to preload the data.

employees = Employees.includes(:billed_time).first(5)

So, what we have done here is preloaded employees that have billed ours and only took the first 5 and assigned that to a variable of ’employees’.  We have ‘pre-loaded’ data that we will need – now we can use #each to iterate over each employee and do whatever we want.  Let’s say we just wanted to #count the number of billed_time entries each employee has.  Well –

employees.each { |employee| puts employee.billed_time.count }

So, we have the same result and had to ask the database far less questions.  That is the idea behind eager loading.  Now, this example was a little weird, having the STI with Accounts, but still.  Eager loading is a way to optimize your queries, lessen time, and ask less questions in retrieving data.  I’ll give a link to a good little resource.  Happy coding.