Up Yours Truly

A fairy tale about software development

Posts Tagged ‘Genious

Guaranteed Execution

leave a comment »

Dear Reader. Today is the opening of the Best Practices section and I am proud to introduce to you the first Design Pattern that was recently discovered by one of my beloved colleagues. Without further ado, here it is:

Pattern Name and Classification:
Guaranteed Execution

Discovered by: Willy Benton

Level of Complexity: Intermediate

Intent: Provides a fail safe way of executing a code block with 100% certainty.

Also Known As: Surefire Block, 100% Execution, Execute It For Real

Motivation (Forces): Imagine you have a code block that plays a very important role in your application. You wouldn’t want that block to be accidentally skipped by the compiler or interpreter, or by CPU in run time. You have to take precautions. Guaranteed Execution is the best practice in such occasions.

Applicability: Can be used in any programming language which supports If / Else constraints. Although, we suggest extensive use of Guaranteed Execution design pattern in PHP, Perl and Ruby to make sure your mission critical applications will work as expected.

Structure:

Guaranteed Execution Design Pattern

Participants:

Compiler / Interpreter / CPU – a thing that processes the code (the author is not quite sure which one is it).
Critical Code Block – a block of code which MUST be executed by any means necessary.
The Lure – If / Else statement that attracts the attention of Compiler / Interpreter / CPU therefore lures it into execution of the Critical Code Block.

Collaboration: Either compiler, interpreter or CPU is doing it’s usual daily job – computing and stuff. One of them notices The Lure – an If / Else statement, and says “Shit, this must be important, I’d better go execute it”. If / Else statement traps the Compiler / Interpreter / CPU and forces it to execute the Critical Code Block. Job well done!

Consequences: Your mission critical application will work as expected. No trade offs or side effects whatsoever as If / Else statements work really fast (“I am sure If / Else statements work as fast as your eye blinks” – W. Benton).

Implementation: The implementation is straightforward. It should get clear after reading the Sample Code.

Sample Code:


<!-- the usual HTML here -->

<?php

//Compiler / Interpreter / CPU hidden here somewhere

$a = 1;
$b = 2;

//The Lure
if (!is_empty($a + $b / 10032)) {
    //Critical Code Block
    $application_safe = true;
    $critical = "executed";
} else {
    //Critical Code Block
    $application_safe = true;
    $critical = "executed";
}
?>

<!-- back to the usual HTML here -->

Known Uses: Number of mission critical applications of SHIT Inc. that cannot be disclosed.

Related Patterns: Soon to come (wait for the updates).

Advertisements

Written by Ken Benchmark Jr.

January 27, 2008 at 9:16 am