in reply to Re^5: replace conditionals with polymorphism
in thread replace conditionals with polymorphism
Tilly, that is one beautiful piece of code, but here's the irony - you could have made it even more general purpose by using OOP. Not, of course, by getting rid of the dispatch table (heaven forbid!), but rather by turning the configuration data into the data portion of an object and the definitions of dispatch routines into methods.
As a result, you would have a framework for multi-syntax parser that could process any mini-language that had those four tokens (open tag, close tag, open constant tag, escape code). Viewed as a whole, your code is a great example of the Type B OOP (multiple related type dependent functions). Furthermore, the idea of using a dispatch table to handle tokens is arguably a form of logical programming. The dispatch table defines a set of "rules". The rules play themselves out until they have nothing left to say.
Which all goes to the essential point of your post on why you like programming with functions. That point was the quote from Tom Christiansen to the effect that one who isn't comfortable with all the paradigms: imperative, functional, objective and logical is like a chef that knows how to boil, but not bake.
And bringing things back to video studied by the original poster, your code, OOPified or not, highlights one of the essential problems with that video (and much of OOP education) - the failure to teach which of the many kinds of relationships between data and function, function and function should be encapsulated in an object. Often the obvious ones (choosing which function to process a token) are not the important ones (defining the collection of functions for a mini-language). The fact that you need different functions for different tokens isn't like to change over time. But the definition of the function for each token easily could and probably needs to change in a coordinated manner with other rules for processing tokens.
Thanks for a good read,
Best, beth
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: replace conditionals with polymorphism
by tilly (Archbishop) on Feb 10, 2009 at 15:58 UTC | |
by ELISHEVA (Prior) on Feb 10, 2009 at 19:50 UTC | |
by tilly (Archbishop) on Feb 11, 2009 at 19:02 UTC | |
by ELISHEVA (Prior) on Feb 11, 2009 at 21:04 UTC | |
by tilly (Archbishop) on Feb 11, 2009 at 21:42 UTC |