I normally have a little snappy line here about whether or not you should continue reading based on arbitrary conditions, well I’ve been staring at my screen for five minutes and nothing has come to mind. So moving on..
This week’s challenge was a program called FizzBuzz. It’s extremely simple. Like. Really. Simple. I had actually done this program before while preparing to start the course, weeks ago, and just to refresh my memory (and confirm that it was simple) I did it again. It took 5 minutes. It took 20 minutes to write 4 different ways. So I was feeling pretty confident.
The program was easy, but the point of this week wasn’t to expand our knowledge of code, it was to teach us about something called Pair Programming as well as Test Driven Development. I’ll talk about both these things a little later on. I might even give them their own headings. But before I do, I’ll quickly explain what FizzBuzz is.
FizzBuzz is a little program that should be able to evaluate a number and return “Fizz” if the number is a multiple of 3, “Buzz” if it’s a multiple of 5, and “FizzBuzz” if both. If neither it simply returns the number. Easy peasey squeeze the lemons.
class Integer def fizzbuzz str = '' str << 'fizz' if self % 3 == 0 str << 'buzz' if self % 5 == 0 str == '' ? self : str end end
Here’s ONE way to solve it. It’s not the prettiest code, at the moment (having done this for only 4 weeks) its more about if it works. So this code adds a method to the Integer class. Within the class a method is defined: fizzbuzz. Putting a method inside a class makes it specific to that class and allows you to call it more naturally within the code. The fizzbuzz method first creates an empty string. Now there are two lines that each evaluate whether or not the number can be evenly divisible by 3 and 5 and will shovel the magic word into the empty string. Because of the order of the code there is no need to add another line for 3 AND 5 as it will shovel both if both conditions are met. It then checks whether or not the string is empty - if it is then it will just return itself.
Not Bad.
Now thats out of the way lets talk about other stuff.
programming.pair
I paired with Jess. You may have noticed I’ve spoken about her before, she's alright. We did the exercise twice pretty quickly. First time she wrote the code and I instructed and then we swapped. It was good to understand the principles behind this practise, though I would say it’s perhaps difficult to fully comprehend it when writing a program that takes 3 minutes to do.
I will say that programming with someone is good though. Obviously I haven’t got the entire scope of it yet but having someone else working with you can open your eyes to different solutions. Different people do, and know, different things (shocker!). So, for example, while I might be fully comfortable writing if statements, Jess might know of a better way to do it. I think this is probably where it shines.
It’s also just nice working with another person from a social perspective - it’s easy to get carried away on your own and if you go too far ahead when you look back you might have left a mess behind you. Having to instruct and explain your decisions to someone else helps to make sure you’re making the best ones.
test.driven(dev)
Test Driven Development, or TDD, is the second concept that we were introduced to this week. This came in the form of RSPEC. RSPEC is a ruby Gem that lets you write tests for your program to check that it works.
Let me give you an example of when this would have been useful for me last week:
So I was extending my method that lets the user add students to the record. I wanted to add hobbies at the end of user input. Adding students looked like this -
1. enter student name
2. enter student date of birth
3. enter student cohort
4. enter student hobbies
When I was testing the last bit of my program I was having to go through this flow every single time I made a change to see if it worked. Then, even if the code didn’t throw up an error I couldn’t be 100% certain it worked without printing out all the students again. Rspec lets you write tests in a single program and then run it. So it would basically do all that for me in about 0.03 seconds - I challenge you to input data that quickly! Further to this, if you test each method as you write it, you know it works - and if its broken you know that whatever you just did is the problem. It makes sure that what you write is less likely to break when you change it. Hopefully.
So thats testing.. but how does it drive development exactly? Again, The scope of FizzBuzz is probably too small for me to have a firm grasp, but from what I understand you can write code more accurately and more deliberately if you simply focus on one tiny problem at a time.
There's a certain mantra that goes with it:
First you write a test that fails. Then you write the code that passes the test. Then improve the code you’ve written. Then start again - with the next part of the problem.
So this is what we did on Monday. This left me with a lot of free time. I used the rest of the week to push myself a bit, do something a bit more interesting. I also brushed up on a few things I know I'm weak at that will be coming up soon. I'll be talking about exactly what I did for the rest of the week in my next post.
spoiler: I have something to show you (and I'll leave my clothes on this time)