VersiĆ³n en castellano

Wednesday, 8 February 2012

Describing a web page (Describing vs Coding II)

The most common concept in web development...is a web page.
Is possible to describe a web page?
A simple try: lets try to describe a web page where you can see info of a certain artist, along with artwork he uploaded to our site:


<?php
       $webpages["viewGallery"]=array(
             
             "PERMISSIONS"=>array("ANONYMOUS"),
             "RENDERER"   =>"HTML",
             "HTML"       =>array(
                     "HEADER" => "default_header.html",
                     "FOOTER" => "default_footer.html",
                     "RESOURCES"=>array(
                                "CSS"     =>array("viewGallery.css"),
                                "SCRIPTS" =>array("viewGallery.js")
                               )
             ),             
             "PARAMS"=>array(
                              "id"=>array(
                                           "MODEL"    =>"Artist",
                                           "FIELD"    =>"artist_id"
                                           "REQUIRED" =>true,
                                           "SOURCE"   =>"GET"
                                          )
                             ),
             "ROLE"  =>"VIEW",
             "MODEL" =>"Artist",
             "VIEWS"=>array(
                            "ROLE"    =>"COMPOSITEVIEW",
                            "LAYOUT"  =>"STACK",
                            "SUBVIEWS"=>array(
                                array(
                                      "ROLE"    =>"VIEW",
                                      "OBJECT"  =>"Artist",
                                      "VIEW"    =>"Bio"
                                      "PARAMS"  =>array("artist_id"=>"id")
                                     ),
                                 array(
                                       "ROLE"   =>"LIST",
                                       "OBJECT" =>"Artwork",
                                       "VIEW"   =>"artworkList",
                                       "PARAMS" =>array("artist_id"=>"id")
                                     )
                            )
                          ),
       "CACHE"=>array(
                             "TYPE"            =>"TIMED",
                             "INVALIDATE_AFTER"=>30
                      )
        );
   

This array may look complex, but i invite you to read it.Again, bear in mind that this is just a "sketch" of what i want my page to be.It's not related to any particular framework.It just tries to express concepts.

But, what i consider interesting, is that many things that usually are coded, here are described.
Most pages will have a header, and a footer.Many, will require permissions.Lots will use parameters, and will have to sanitize them.Will include external resources. Will include one or some other pages.Other things that may include..headers (encodings,etc), tags...May require a certain "state" to be set.May set a "state".. Things that are easily described, but that usually, are just scattered around in code (if not forgotten).

When you are writing the above description, you're focusing in what you need to create the page.Not in how you'll create it.This is key.
And, as there isn't any kind of formal structure, you can add whatever keys/values that may fit your needings.

If somebody has to maintain my code, and reads the description array, he'll have lots of information that will help him to understand the actual code.Of course, as you may begin to notice, most of that code, could / should interpret that array, or will be generated from it.

And,if someday i want to switch from one framework, or language, even programming paradigm, having lots of information about my system as *data*, instead written in code, will ease the transition.

And this is the main concept; i don't know if there are frameworks using similar ideas, but, that's already a contradiction;my idea is: freely describe your system as data, without any dependency , syntax o requirements a certain framework may impose.
I've written an ORM that i (later) found to be pretty similar to Django for python.But, the fact that model objects definitions are arrays without any dependency on that ORM, is what makes both completely different in philosophy, and it's the philosophy (describe your system, don't just code it) what i consider important.

The challenge is to find the right way to describe a web page may have, from models to db-mapping, views, pages,permissions, in a way that it's, at the same time, intuitive, extensive (covers as much of the functionality as possible), inter-related (so form definitions rely on model definitions), and external dependency-free.

No comments:

Post a Comment