Sunday, 15 March 2015

Week 8: Linked Lists

I found working with linked lists to be a very fun and productive challenge. While initially learning about them in lecture my first impression was that implementing linked list methods was going to be extremely straightforward. They have two parameters and a linear arrangement, how hard can it be? But once I started writing methods for lab 7 I realized that iterating over a linked list properly wasn't quite as trivial as I thought it would be. When I first began writing these methods my instinct was to use for loops. I don't remember exactly what the conditions or the bodies of these loops were but I noticed that as the methods became more demanding my code was becoming very ugly. I was able to simplify things by using break statement but in both CSC108 and CSC148 the use of break has been highly frowned upon. I felt that there should be a simpler strategies for implementing these methods. Eventually I was able to get all doctests passed but I was still dissatisfied with the aesthetic dimension of my code. I like my code to be as lean as possible. This improves its readability but I also view elegance in code as preferable for it's own sake. Given two equally effective solutions to the same problem, I would rather go with the more elegant one.

A few days later the solutions to lab 7 were released and I saw that the instructors used while loops to accomplish any iteration in linked list methods. I saw that this resulted in code significantly more simple and readable that what I had written and I was a bit disappointed in myself that I never thought to experiment with while loops. I only looked at the solution to the first couple of methods and I immediately began recoding all of lab 7 on my own with while loops. I was much more satisfied with the result this time around. I suppose this experience helped me become a bit better at problem solving. From now on I'm going to make a stronger effort to consider all tools I have at my disposal in order to come up with more simple solutions.

No comments:

Post a Comment