[Grok-dev] Re: is automatic context detection harmful?

Martijn Faassen faassen at startifact.com
Tue Aug 28 05:48:55 EDT 2007

Brandon Craig Rhodes wrote:
> This would mean declaring Models in one module and Views in another
> module - exactly what Grok makes abnormal!

I don't think Grok in any way makes this pattern *abnormal*. I also 
don't agree with established Zope 3 culture that splitting things up 
that way is necessarily a *good thing*. Good application design involves 
more than following some rules. It involves examining the rules and 
reconsidering them once every while.

Grok makes it possible to start small. It encourages placing everything 
in one place. It makes the code readable: ah, there's this model, which 
has these views, by just reading one file. It allows package structures 
to be flat, not nested.

If your application grows, or you need to extend your application, or 
you introduce interfaces, you can always refactor and move your views 
somewhere else should this be necessary.

> I find, as I write my first tutorial (yes, it's coming, but I am a
> slow writer!), that I am very tempted to simply use grok.context()
> everywhere, because it just seems to make everything simpler and more
> readable.  When grok.context() is always used, there is just One Way
> to do things, and whether someone choses to place Models and Views in
> the same module, or split them apart into several modules, the actual
> code will read the same in each case.  There are no exceptions to
> learn, and - in my opinion! - code becomes far more self-documenting
> and obvious because you can always "see" the context of every View
> without learning magic rules.

Feel free to use explicit grok.context() everywhere in your tutorial. 
There is no problem with this.

I don't think the grok.context() rule is so hard to understand. Almost 
all the grok directives have a default. If you leave it out, the default 
behavior will kick in. The rules for the default behavior aren't too 
hard to internalize, and if you do something that Grok can't figure out, 
Grok will clearly tell you.

 > It seems better to me that everyone go ahead
> and throw grok.context() calls into their code and tutorials for 0.12
> than for us to reach 1.0 with two entirely different ways for Views to
> find their Model, especially since the "magic way" encourages poor
> application design and makes a more normal Zope-3-style organization
> of code look suspicious to the newcomer.

Again, I strongly disagree with the characterization of placing view 
classes and modules in the same module as "poor application design".

I tend to organize my grok code into separate modules per module. I 
don't see any reason to go in and add 20 grok.context() lines 
everywhere, one for each of my views.



More information about the Grok-dev mailing list