Code Kata 4 Part 3, DOH!

Man, this sucks. All that work and I finally read part three of Code Kata 4. In short, Code Kata 4 Part 2 is Code Kata 4 Part 3 and this post should actually be Code Kata 4 Part 2. I have to say this is more than a little demoralizing but the show must go on. It is true in show business and in programming. I will pretend that yesterday never happened and develop a second program as I should have. Tomorrow, we will do a postmortem. Today, everything is still alive.

Let’s start with the button click since it is literally all there is to do.

Wait a damn minute. What do you mean that you did today’s yesterday and yesterday’s today?

I assumed that I was going to have data set. Then I assumed that we were looking at re-usability of the code. I didn’t assume that this was a step by step re-usability task. I create a nuclear weapon for a bug rather than shoe. On the bright-side both killed the bug.

So today is Code Kata 4 Part 2 and not Code Kata 4 Part 3 like the title says?

Exactly, so yeah…

btnProcessFootball

private void btnProcessFootball_Click(object sender, EventArgs e)
       {
           string buffer;
           List<string> line = new List<string>();
           List<string> cleanLine = new List<string>();
           string allowed;
           string scored;
           int smallest = 1000;
 
           using (StreamReader reader = new StreamReader(txtSoccerPath.Text))
           {
               while (reader.Peek() != -1)
               {
                   line = new List<string>();
                   cleanLine = new List<string>();
 
                   buffer = reader.ReadLine().Trim();
 
                   line.AddRange(buffer.Trim().Split(' '));                
 
                   foreach (string segment in line)
                   {
                       if (segment.Trim() != string.Empty)
                       {
                           if (!segment.Contains(".") && !segment.Contains("-"))
                           {
                               cleanLine.Add(segment.Trim());
                           }
                       }
                   }
 
                   if (cleanLine.Count > 1)
                   {
                       scored = cleanLine[5];
                       allowed = cleanLine[6];
                       lbResults.Items.Add(scored + "\t" + allowed);
 
                       if(scored != "F")
                       {
                           if (int.Parse(scored) - int.Parse(allowed) < smallest)
                           {
                               smallest = int.Parse(scored) - int.Parse(allowed);
                           }
                       }
                   }
               }
               lbResults.Items.Add(smallest);
           }
       }

So here is the simple version of the code. We do everything the same as the weather.dat except for the post clean line segment count is greater than 1. In that section, we get the scored and allowed. Again, I have my throw away line to give me more context but should not technically be in the results.

Finally I have the scored value check to make sure we miss the header row so I can just parse the scored and allowed into integers without concern. I can do this because I saw the data and know there are no abnormalities in this context, except for the header itself. Then to the output goes the final answer after everything has looped through.

After that marathon slug through yesterday, today’s seemed like a breeze. Tomorrow, I will look at maybe doing code Kata 4 part 3 for real as the author intended.

To end this on a positive note, let’s see the GUI with the output ready. The second Process Soccer button is the click event caller.

Leave a Reply

Your email address will not be published.