Hi Chris,
Kevin Ollivier wrote:
code I see tends to do #1, you're better off going with #2 to get in the habit of using MVC, IMHO. Once you move your data into a custom dialog, you can only use that dialog with that data. While that may be the intention right now, as your code grows and expands, it's not uncommon to want to reuse UI code in numerous places, and it's much easier if you just design it to be reusable from the start. At least IMHO
hmmm -- I wonder---
no matter what you do, you need to define an API of some sort.
Option #1 defines the API for the Dialog as a given Data Object being passed in. If you want to use the dialog with some other model, it will need to pass in that Data object -- but, thanks to duck typing, all it needs to do is pass in a data object that behaves in the same way.
Yes, but that's true of #2 too.
Option #2 defines the API of the dialog as a set of getters and setters (though I'd probably use properties..). In order to use that Dialog with another model, you need the new model to call all the same getters and setters -- is that really any easier?
Easier? Not really. The easiest route is to just stuff everything into the constructor for the dialog subclass, which is why that approach tends to be popular. I'm more arguing what is the most robust approach.
Another point -- I think it's far more likely that you'll want to use the same model with a different View than vice-versa anyway -- though maybe that's just my use-cases.
Which is actually a pretty good reason not to design your model's API around what a particular view needs, don't you think?
Here's the thing. If you design your data model to simply provide any data it has to any client, and you design your view to simply display whatever data is given, then you don't have to refactor when you need a new view for your data. You just write a new controller.
I think Option #1 involves less code, so that gives it an edge -- but maybe not.
When we're speaking in hypotheticals, it's hard to know for sure. I doubt it would be a significant amount of extra code though, as we're largely talking about where we put the code to load the view's contents and perhaps record any changes to it.
In any case, I personally stopped caring about writing the least amount of code possible a long time ago - most programmers can type a line of code pretty fast, and can read reasonably fast as well, so I don't really think that's a major performance bottleneck when writing an app. I think it's far more important that my code is easily readable, straightforward, and resilient to change, rather than concise or elegant. To that end, for the most part I've found that decoupling usually brings benefits, especially when done from the start.
For example, I actually wrote a lot of code taking the approach of #1 up until probably a couple years ago, and I found that it encourages some bad habits regarding tying model and view, which led to some pretty nasty bugs, and also made writing isolated tests / bug reductions difficult because often my view would need a "fake" data object just so I could test that it loads/runs, and sometimes that data object would contain pointers to other objects, which would also need to be faked, etc. This then discourages one from doing test-driven development because it's more work to setup such tests and setting up a fake environment looks pretty hacky/ugly, and so you just say screw it and do "manual testing", e.g. run real world tests, which tends to stress common cases and ignore corner cases, and also are limited by how many testers you have and how much time you/they can spend testing. Not to mention it's almost impossible to keep track of what exactly was and was not tested.
Also, by keeping the view truly separate, I could also make the code reusable for any wxPython user, which could help others and maybe get some good bug reports, whereas if my app's data model is tied into the core of the class, that wouldn't be possible without refactoring or again requiring every app to create some special data model to use the class. Easiest and best aren't often the same, and in fact, I'd say are often in conflict with one another. As with lots of things in life, you can either do the hard work up front, or put it off until later when it will be twice as hard. (e.g. my refactoring of my first major Python app) In my experience, not doing the hard work has rarely ever been an option unless you're writing some quick, one-off or throwaway code.
Regards,
Kevin
···
On Jan 9, 2009, at 9:39 AM, Christopher Barker wrote:
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users