One of the things beginners do when learning how to program is to make things more complicated then they need to be. It’s a natural thing because they often don’t know the shortcuts. They may not know enough library routines or even enough language features. So they build complicated ways around what they don’t know using what they do know. It’s that old story of when you only have a hammer all your problems look like nails.
Everyone once in a while my students are the ones with the shortcut and I’m the one who over complicates things. That happened to me with a recent project. I assigned my students the project I blogged about in Simulations Are More Fun recently. As is my practice I coded up a sample solution myself. I liked my solution. It was cool. My students took an easier path though.
I over thought the problem. The problem includes the detail “If the biggest number rolled is five or six, player 2 wins” so my first thought was to determine the biggest number and then check it’s value. I got even more clever by creating a function that took two numbers and returned the value of the larger. It worked wonderfully but it added additional complexity without adding real additional value. My students just checked the two values with an OR expression. Sort of like this:
Nice and simple with no extra method call overhead.It’s actually sort of elegant.
My problem, if I can call it that, is that I tend to think of solutions that scale. I’ve spent a lot of time in my career dealing with big problems and big data. This colors how I look at some problems. This problem is simple with a very small data set and this simple if works great in this case. If I had to compare a value to the highest value in an array this simple solution could be limiting. It would get complicated very quickly. Having a method that found and returned the highest value in an array (something we talked about in class when introducing arrays) would make things easier. Or at least simpler in the if statement.
This is something I will talk about in the future as I try and continue to work scalable solutions and how to design for scalability. But we’ll also have to keep an eye out for simple solutions as well.