Code Kata 4 Part 1

If you followed yesterday’s posts, then you likely know that today we start the first challenge of Code Kata 4. In this challenge we are take some data which is stored in a dat file, and outputting the first three columns. So let’s open up Visual Studio and make a little interface for it so it can be pretty. It’s time to get into Code Kata 4 Part 1.

So we have a simple little interface. It took 5 minutes to get all set up. It has a textbox for the weather.dat file’s location, a button to process the file and a listbox to display the results.

I have not read the full kata as I am following the rules. I have just read part 1. So here we will have the day of the month, the high temperature and the lower temperature. Easy Peezy,….um…I don’t think I am allowed to finish that one so um….squeezy cheezy?! That was close.

All humor aside, here is the final product.

Enough with the pretty things, show me the code!

My grouchy comrade, states there is more to the challenge then making things pretty.

Let dig into it.

The heart and soul of this challenge is contained so far in single button. There just is not much to isolate out at this stage. So let’s look at the button click event.

private void btnProcess_Click(object sender, EventArgs e) 
{ 
   string buffer; 
   List<string> line = new List<string>(); 
   List<string> cleanLine = new List<string>(); 
   string day; 
   string maxTemp; 
   string minTemp; 
   
   using (StreamReader reader = new StreamReader(txtPath.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) 
            { 
               cleanLine.Add(segment.Trim()); 
            } 
         } 

         if(cleanLine.Count > 1) 
         { 
            day = cleanLine[0]; 
            maxTemp = cleanLine[1]; 
            minTemp = cleanLine[2]; 
            lbResults.Items.Add(day + "\t" + maxTemp + "\t" + minTemp); 
         } 
      } 
   } 
}

Tear down Code Kata 4 Part 1

So first things, first, I am know I need to tear down a clear text file. So I whip out the StreamReader object. As a result, the buffer string makes it easier to deal with reader lines. Since we need to split the string into many pieces, I need a list of strings to store those pieces. Finally, the list of things has some noise so I created a list of non noisy pieces. Finally I have the day, maxTemp and minTemp variables to store, day, maximum and minimum temperatures.

With all the declarations out of the way, it is time for the StreamReader. I take the path from the GUI as the path to the weather.dat file. Then I open the file up and start moving through the file, line by line.

The reader.Peek will check the next line for being empty and if true then return -1 so we know to end the while loop.

So in the while loop, we know the file has another line to process. I set up new instantiations of line and cleanLine lists. This clears out the lines from any previous data. With everything setup, we can now start processing the line.

We read the line into the buffer and trim off any blank spaces. With that line we split the line into its pieces. This gets stored in the line list and the split function breakup up the space delimited list. Now we have an issue with the space delimited lines. The columns are not always the same number of spaces between things. That is why I have to do another layer of cleaning.

The foreach loop starts the next level of cleansing. So we go through all the parts of the split contained in the line list, we check if the trimmed line segment, is an empty string. If it is not then we add it to the clean list.

Now that we have the cleaned list, we make sure there are multiple parts to avoid a blank line situation which would manifest as a single segment.

Finally we take the first three columns, which contain the data we want and store it in the variable. I decided to do the variables so that what is going on will be more visible. After all that we dump it into the listbox lbResults.

Final Output

So there we go, we handled the * on day 26 and everything is ordered nicely. Mission accomplished.

What do I feel about Code Kata 4 Part 1?

I really wish, the data was a bit cleaner so I could eliminate the second cleaning step. There is almost certainly a better way to handle this problem but this is my 30 minute solution. I don’t hate it but I don’t love it. I could see removing the day, maxTemp and minTemp variables. That would strip out 9 lines or so. I might do this as this function is physically lengthier than I would prefer. We could also eliminate the buffer variable since we don’t actually use it for anything productive. So there is definitely some tightening of the code that could happen upon refactor. That said none of the logic changes.

I am excited to see what tomorrow brings on Code Kata 4. It is better than Code Kata 3.

Leave a Reply

Your email address will not be published.