I'm so glad I just failed

Ok, I didn't really fail (you read my last post right?), but I did mess up, and I learned a few great lessons in the process.

A little background info: My wife asked me to make a program that can sort her students into seating arrangements based on their test scores. For now, she said, she just wanted something that would sort her students into groups of 4, with each group having a student in the top quarter of the class, one in the second quarter, one in the third, and fourth.

Even though I'm not an all-star programmer yet, this sounded pretty simple to me, so I started asking about other features she would like to have. She mentioned the ability to keep certain students from ending up in the same group as a feature that would be nice to have, but nothing else really.

I'm a caring husband who can read his wife's mind and knows what she wants at all times, so I continued to dream up other features that would make this a killer app instead of coding the basic functionality. Eventually, my wife came to me and asked, "where's my app?" I told her I didn't have anything yet because I was still thinking about features. She responded by asking if I would just build the basic functionality because that basic functionality was going to be what would save her the most time, anything more would just be a bonus.

After she said this, I realized I had been doing everything I had heard not to do. I wasn't focusing on an mvp (minimum viable product), I was thinking about extensibility with my massive market of one, and worst of all, I wasn't coding. So I started coding and learned valuable lessons in requirements gathering and brute force solutions.

I tried to do exactly what my wife asked me, just sort an ordered group. I didn't try to create student objects, I didn't try to force functional or object oriented programming techniques, I just tried to sort an array into groups of 4 by brute force. I hard coded some ordered dummy data and got to work. Eventually I came up with a working sort. It was ugly, but it worked.

Here is what I came up with.

//random ordered data
var students =
[1,2,3,4,5,
 6,7,8,9,10,
 11,12,13,14,15,
 16,17,18,19,20,
 21,22,23,24,25];

var sizeOfGroup = 4;

//array to hold groups
var gradeSort = [];

//determine number of groups needed
function findNumGroups(studentsArr, groupSize) {
  if (studentsArr.length % groupSize !== 0) {
    return Math.round(studentsArr.length / groupSize) + 1;
  }
  return studentsArr.length / groupSize;
}
//find # of groups needed
var numGroups = findNumGroups(students, sizeOfGroup);

//create group arrays
for (let i = 0; i < numGroups; i++) {
  gradeSort.push([]);
}

//sort students
for (let i = 0; i < students.length; i++) {
  let groupNum = i % numGroups;
  gradeSort[groupNum].push(students[i]);
}

You may not even have to run this code to see the problem. It creates 7 groups, which is fine, you can't fit 25 people into 6 groups of 4. But if you remember back to school, usually when this happens, the teacher will just make 5 groups of 4 and have one group of 5. My code creates 7 groups, 4 with 4 students, and 3 with 3 students. My code isn't wrong, but I wasn't sure this was what my wife wanted. Turns out, it wasn't.

I asked her what she would want in this situation, and sure enough, she said she would rather have 6 groups with one group of 5. Well, what if there were two extra students? I assumed she would say she wanted 6 groups, 2 with 5 students. Nope, wrong again, 7 groups with one group of 2! Ok...so what about 3 extra students? A separate group of 3 right? Wrong again! A separate group of 2, and one group of 5. Is your head spinning yet? Mine sure was.

The point is, we shouldn't make assumptions. But that is easier said than done. Sometimes we don't even know we are making an assumption when we mistakenly think we have all of the relevant facts. This is the reason I was so glad I created an ugly brute force solution. Instead of working on a pretty and complete solution that would end up being wrong, I was able to identify errors in the assumptions I had made, saving me hours of time down the road.

I can't comment on the usefulness of these lessons on the job. For professionals, there may be pressure to make things perfect the first time around that I am not aware of. A few things, however, are clear to me: taking the time to accurately assess requirements is valuable, and a brute force solution is better than no solution.

Special thanks to my wife for, yet again, steering me in the right direction without even knowing it.

reference: Code Complete