Pairing on the prime factor kata

I spent my first days working on Vim and the Ruby koans. The ruby koans are ok because it makes me look at the documentation quite often to remember how Ruby works. I also started doing touch typing to improve the speed of my typing.

But, the highlight of these past days, was the pairing I did with the apprentices. Pairing is really cool because not only you are not alone to solve a complicated problem, but, sharing with someone else your reflections make you realize that you could be totally wrong. Also, pairing with someone more advanced than you will inevitably make you learn faster.

Yesterday, I paired with Nick on the prime factor kata.

The purpose of this kata is to create a general rule that will generate for any given number its prime factors. It means it will return prime numbers that when multiplied together make the original number. A prime number can be divided evenly only by 1 or itself. And it must be a whole number greater than 1.

  • Class methods: self.generate -why did we use a class method and not an instance method? You use instance methods when you want to act on a particular instance of a class. This is often when the functionality concerns the identity of the instance, such as call properties on the object or invoking behavior. Here, we want to act on Prime numbers which is the class. We are going to work directly on the class.
  • Loops: Loops are not totally new, but I always had a hard time understanding how they work. Here the while loop that starts with ‘while’ and finishes with ‘end’ will repeat itself until the condition is no longer true.
    How going from an if to a while can improve the “flow” of your kata.

I will pair with Rabea tomorrow; I’m looking forward to it!

Ps: few things on the katas for myself so I can remember it easily next time. As usual, when I do it with someone, it’s crystal clear, and when I’m on my own, it’s not.

  • prime is set to an empty array because that’s how we want to return the result
  • candidate stands for tester number, a number we will pick to be our starting point. In this case, 2.
  • /= is the same as a number= number / candidate