Procedural versus declarative
- By Ellen Gottesdiener
- July 25, 2001
Procedural logic has control flow built into the code. Declarative rules, alternatively, remove the control flow and sequencing -- the how's -- from the program.
Consider the procedural logic for calculating a total sale in a book store. The program will test the book type for a variety of types (sale, hardcover, mass market and the like), then store an interim cost in memory based on the book type. It then tests for any customer discounts (such as volume, student or preferred card) and applies the discount to the interim value. Next, the program totals customer sales and gives a message to the sales clerk that if the customer buys one more book, she is eligible for a free one. The procedural logic continues, prescribing tests and actions in a sequence, including updating database entries, logging activities and notifying end users. Languages such as C, Cobol, C++ and Pascal are procedural.
Declarative statements do not care about how the rules are enforced. Languages used in rule-based systems, such as Prolog, express rules in a declarative fashion. They represent 'what is true.' Declarative statements require one-third to one-sixth the number of statements to represent rules than procedural logic.
-- Ellen Gottesdiener