Sunday, 2 December 2012

Idiot C# GUI Strategy

I've had a couple of complaints about my views in the last post... And folks, I appreciate you have your views, these however are mine... Sorry...

Now, I've also had complaints that I have been knocking other peoples ideas about code and system design, certainly I have been, and I have made out that I ALWAYS get things right... I don't folks, this is one of the first things GOOD programmers will admit and great programmers know.  I consider myself good, not because of my technical ability being better than others but simply because I know I don't always have the answer and if I do have an answer someone else might have a better one.

And this is one of those times, I have been recently programming in C++, this means passing parameters and pointers and working in certain ways.  And then I've in the last couple of days gone back to making a GUI in C#...

And I came up with the data, I came up with a bunch of clever looking GUI (forms) and I have each one linked to a type of data class... And I had one instance of the root data class in the main program, but I for reasons I can't remember... Didn't decide to pass the reference to the root data around and manipulate it in each form at each step...

No, I decided it was better to keep that one root instance safe and pass dozens of delegates up into each higher user facing form to allow that form to manipulate down to the data.

The end result was a teetering  tottering, staggeringly poor implementation, unable to evolve, unable to be clearly documented, with a bloat of each layer needing to pass the values in to the preceding delegates, or set them into the succeeding layers... In short it was a bloody mess, like someone had taken a 9mm to a telephone junction box, wires everywhere but these wires were delegates.

I have of course this evening, in the last 45 minutes in fact, removed nearly ALL of this code.

And replaced it with the old standby, best practice  or passing the actual reference to the root data into each sub layer, and to allow each sub layer to see the Parent control, by passing it as the parent...

Now, from each layer, I can manipulate the data in root directly, finding and altering whatever I like in each control and form.  And I can call back down the layers to say roll back a page, or call to reset some value...

All in all, simple, clean, easier... And... I did not see that solution first time.  Everyone does it folks... No programmer is infallible, not even me.

No comments:

Post a Comment