Wednesday 29 June 2016

wk_three


So we’re at week three of the Makers Academy PreCourse and you’re still reading? I’m impressed.. with myself. Allow me just a moment of reflection here; I started this blog because I was told it was a good idea (I’m sure the free marketing for Makers Academy is purely coincidental!). I’ve tried blogging before, it’s not easy, you know. You have to think things in your head and then write them down, usually I fall at the first hurdle there. So I’m really happy that I’ve managed to push out a post for every week so far - I hope you are too!


After Chris Pine successfully subjugated my whole cohort last week with his book learn to program morale was lacking slightly. After all it was intense and took a lot of work. So now we’ve learnt to program now what? The first day of the week (Monday, incase you didn’t know) was hard. I had no motivation to do anything, I think I had burnt out a little bit, So I allowed myself a break - I started on Tuesday and I think allowed myself that time was important.




Are we not finished? In short: no. (Surprise surprise!). This week did, however, turn out to be much more manageable. Instead of a whole textbook asking us to write ~20 programs we just had to write one. But it was pretty big.

If you like hearing about big things you’ve come to the right place, if you don’t I’m afraid you’re going to be disappointed.

The program we wrote was a relatively simple directory application that runs on the command line. I’m sure that doesn’t sound too exciting, I’ll do my best to keep you engaged. Like any project there was a spec - but we didn’t know it just yet. The way Makers had structured the task literally let us make simple things one at a time and then slowly bring them together to make something. This makes sense, I know that if I had known the spec from the beginning I probably would have gone straight in with some more complex code - starting simple is always preferable.

A lot of the development of this program felt like a cycle: write something. It works. Write something else. It doesn't work. Change the first thing to make it all work. And so on. Gradually over time things got more and more complicated as I introduced more functions. 

For example;

I began putting students names and cohorts in an array. This was good, its a way to store more than one element of data. Actually it was a Two-Dimensional array, because it was an array with lots of arrays inside it - one for each student. Now, if you want to print all the students names thats easy - you can print all the arrays with all the data. But if you want to search through it to find a specific cohort or name is not quite as intuitive. 

  students = [["Jack", :november],
              ["Jess", :november]]

Enter the Hash. I’ve spoken about them before, the store pairs of data as a key and a value. See it below! So we have an array of hashes - one for each student. This is so much better because it allows you to be more explicit in your code. Sure, if you stored everything the same in arrays you could use it - before it would have been array[0] for the name, now its hash[:name]. That makes it much more obvious what you’re trying to do. Hashes are great.

  students = [{name: "Jack", cohort: :november},
              {name: "Jess", cohort: :november}
 

Next we had to order our students by cohort. Originally I tried sorting them by the cohort value in each hash - unsurprisingly this didn’t order them correctly - it ordered them alphabetically. How did I solve this? Remember I said hashes are great? Yeah, more hashes. I added a hash of months to my program - the keys are months and the values are numbers to order them by. Next I made it so whenever a student is added the cohort entered will actually take the key AND value from the hash of months I made and place it in a hash within the hash. So we have an Array of Hashes with Hashes inside them. It was them possible to order them by the number inside the cohort hash.

  students = [{name: "Jack", cohort: {month: :november, num: 11},
              {name: "Jess", cohort: {month: :november, num: 11}}

Now what? We’re done? Oh no. How about finding out about student hobbies. Hobbies. Plural. I stuck another array in the hash to store a list of hobbies. Things are getting complicated.

  students = [{name: "Jack", cohort: {month: :november, num: 11}, hob: [:coffee]},
              {name: "Jess", cohort: {month: :november, num: 11}, hob: []}  

Fast forward a little bit and I was working out how to save and load files. Now the exercise wanted me to save it as a CSV. That stands for comma separated variable. So it stores all the elements in-between commas separately. Now the issue here is that when you split everything by the commas it stores just fine, but then when you come to rebuild it - what if some students have 4 hobbies and others have 2. I’ll end up sticking a load of nil into the arrays to iterate through all the data - and then it becomes an issue of when hobbies end and the next  student begins. So I thought I could do it better. I went off piste. I understood what was being ask, but I was making so much more work for myself in the end. So I did it differently.

After all I had just learnt to program, why not try something else? It’s all a learning curve after all. And I relish a challenge.




Instead of messing around with iterators and CSV files I used YAML. That stands for YAML Ain’t Markup Language (seriously). It’s a different way to store data and in this case it made so much more sense. It was difficult to implement - I’d never done it before, but thats half the fun. I had to use this line at the top of my code ‘require ‘YAML’’. Never done that before. Require just loads a program into your program so you can use it’s rules and methods.

That's the code-talk over. I hope your head isn’t spinning.




After I got everything working it was a relief. It felt like a real accomplishment - my program actually did something. You could load a file of students, add more students and save them. You could even save to a different file (wow!). But it was a mess. My methods were 30 lines long and did so many things. 

The last part of the development process here was refactoring. That basically means improving the code without changing what it does - making it more efficient, more elegant and more less likely to break.


So that's what I did this week. I actually finished quite early - Wednesday, I think. This is because of that thing I told you about in my last post. Not being able to switch off? Still a work in progress I’m afraid. I enjoyed doing this so much I couldn’t put it down and so I flew through it. I suppose you might call that 'passion'.


No comments:

Post a Comment