Unit Testing for Life

My grand C# project as of late has been a WPF Powerball simulator. I once hacked it up with WinForms and got everything to work by smearing public modifiers all over the place and ignoring good practice, but now that it really matters, I have taken the more professional approach.

At first, things were working great. The code behind was executing flawlessly, and the last thing to do was implement the payout logic. That’s when it occurred to me that my code behind was getting sloppy, and I wanted to pull out the logic into a separate class file. The moment I did, all functionality broke, as I no longer had access to the Window instance. Thus began a two day journey to learning bindings and MVVM, and I cannot honestly say there are any truly good tutorials out there on it.

Just a few minutes ago, I finally got the Powerball values to start populating. You see, I’ve learned so very much in the past two days, but what I learned actually had little to do with WPF. Sure, there has been a lot of confusion about the ICommand interface and how in the world to get events to work as bindings, but I learned that a { get; set; } declaration is required even on public properties when you want to access them from outside, that one should be really careful not to mistake an object for one of its properties, and that using constructors properly is very important for getting things to work they way you want. This is OOP in the trenches.

My greatest mistake has been trying to convert my application all at once. Without finding simple ways to understand bindings, I found myself flinging solutions at the whole thing, and it turns out what primarily kept my application from working was a mistaken understanding of how to pass values between the model and viewmodel. I suspect that one of the best ways to learn is to bite off one piece at a time, similar to unit testing. If you can get that basic piece to work, they you are far better equipped to to solve the next larger piece, and so on until you can isolate bugs to the newest features and their implications to other elements of the application. I’ve never thought about actually approaching learning from this perspective.

Next steps are to get those commands to work, but after a very long day, it’s time for a movie.