5.2. Declaration Versus Execution

 <  Day Day Up  >  

Example 5-5 with arrays rather than separate methods illustrates the difference between using declarative-style and executable-style code to express an operation. The base_rental_period_per_category array is an example of declarative style. If you want to change the base period for a category, you do not change the internal operations of a method, just the values in a table. Because the values are in a table, this is often referred to as table-driven code . I use the term declarative-style programming to encompass any form of coding in which the design permits expected changes to be made in anything other than executable code. This includes table-driven code and configurable code.

With tables in source-code files, the code will have to be recompiled if the values change. Instead of hardcoding these values into the source, you could read in the values from a configuration file. The implementation of the configuration file could employ comma-delimited files, XML, or a database. The configuration data acts in a declarative-style mode. It contains information on what needs to be done, without specifying how it is done.

With a configuration file, the source program code does not change, even if a new CDCategory is added. We could add hundreds of new categories without an additional line of code. The configuration file would contain additional data on the new categories. [*]

[*] If Sam was going to be adding new categories or altering data for existing categories frequently, a separate CDReleaseConfiguration class should handle these functions.

DECLARATION OVER EXECUTION

Declarative-style programming can provide flexibility without code changes.


 <  Day Day Up  >  


Prefactoring
Prefactoring: Extreme Abstraction, Extreme Separation, Extreme Readability
ISBN: 0596008740
EAN: 2147483647
Year: 2005
Pages: 175
Authors: Ken Pugh

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net