Desktop Applications and the big ball of mud

I confess, I am normally a web developer, and one of my first lessons was to decouple user interface and business logic, that is: My ASPX Page is not actually doing much except passing data back and forth to some back end classes. It's not necessarily MVC as the "View" may do some validation and some other stuff that belongs into the model or controller, but usually it's clean.

A few weeks ago I started a WinForms application, which is my first foray into a real WinForms application (I have written some before, but those were very simple tools, not full-blown applications.) I have to admit that not much planning went into SWiki. I kinda knew what I wanted, but at the same time I didn't because I had not used it yet. SWiki 0.8 was finished in about 6 hours and I started using it to create some real documentation. And by using it, I saw what did not work and what additional features are needed, which I put in a Road Map and implemented in the order I deemed appropriate. I've released 4 updated versions and I am about to release 0.13, and it just struck me.

The thing became a big ball of mud. Really, look at the code for the Main Form. I have the User Interface extremely tightly coupled to the database. In fact, if I want to programatically select another wiki page to display, I call the SelectPageInTree() function, which Selects a Page in the TreeView. This triggers the AfterSelect Event of the TreeView, which will change the Page. This is completely stupid. Even worse: I am currently trying to fix the Re-Parenting feature so that you can parent pages to the root page again. Unfortunately, there is no good way to do it without either making the UI illogical or having tons of side effects and more global variables.

Now, this does not mean that it's beyond repair. SWiki is a rather small project and I can easily fix that with a day or two of work. But really, I have to go back to redesign and rebuild the UI from the ground up, now that I know which features I need. That's not a complete rewrite, but I think Jeff Atwood is right when he said "Prepare to write your application three times". SWiki 0.8 was already the second version (the first one was more a proof-of-concept/experiment, but still), so I guess that is my third time now 🙂

As said, in a web application I found it rather natural to have Logic and UI separated, mainly because of the stateless nature and because I "rebuild" the UI on every request anyway. In a WinForms application, that is not the case, and I think it's a lot easier to accidentially make your user interface part of the core application instead of just using it for Input/Output of data. I'm going to read about the MVP and MVVM patterns a bit I guess. MVVM is what is commonly used in MVC and WPF/Silverlight applications, so I do not know if it's applicable to WinForms.

So yeah, still plenty of work to do until SWiki 1.0 (I am also thinking of renaming it because Swiki is already the name of another Wiki), but overall I am quite happy with how the current 0.13 alpha works, and at the end of the day, that's what matters more than perfect code. I'm currently holding off the 0.13 release and merging it with the 0.14 version which had the UI Overhaul planned anyway.