Cheth.com

Contact | About | Home

Code is a grossly misleading term. It implies that programs must be incomprehensibly constructed by secret guilds of cryptographers, that the goal of programming is maximum obfuscation through inscrutable cleverness, and that the programmer's tricks of the trade are much like the old magicians “code” — explanations shall never be revealed.

The truth is code must be readable. Over time, the major cost of code is not the development effort, it is the amount of time spent on maintenance. Consequently, creating code that isn't unduly "coded" is always the wiser choice.

The following example is a short, object-oriented report that analyzes new account activity. The report runs a parameterized SQL query, then hands off the report formatting chores to a standardized view.

<?php /********************************** * lookupRegistrationAttempts.php *---------------------------- * Purpose: Report Recent Registration Attempts on Drupal site *---------------------------- * 1.0.0 (cheth) 2015-Apr-16 initial implementation * 1.0.1 (cheth) 2015-Apr-26 allow reporting for various time periods. **********************************/ /************************** * standard initialization * **************************/ require_once('../config.php'); /********************* * initialize classes * *********************/ require_once(INCLUDE_PATH . 'models/MySQLReportModel.php'); $rep = new MySQLReporter; /*************************** * get runtime param values * ***************************/ $myValues = $rep->getParamValues(); // parse and sanitize POST vars $myTimestamp = $myValues->start_date; /******************** * set report params * ********************/ $rep->setTitle("Registration Attempts"); /************ * SQL query * ************/ $sql = " SELECT SQL_CALC_FOUND_ROWS COUNT(*) AS counter ,accesslog.hostname ,MIN(FROM_UNIXTIME(accesslog.timestamp)) AS earliest ,MAX(FROM_UNIXTIME(accesslog.timestamp)) AS latest ,users.name FROM accesslog LEFT OUTER JOIN users on users.id = accesslog.user_id WHERE timestamp >= :myTimestamp GROUP BY hostname HAVING registration_attempts > 0 ORDER BY registration_attempts DESC ,counter DESC "; $params[] = array(':myTimeStamp', $dashed_title, PDO::PARAM_STR, 'value'); /****************** * generate report * ******************/ $rep->PrepareReport($sql, $params); require_once(INCLUDE_PATH . 'views/registrations_view.php'); /* eof */
The major cost of code is maintenance.
Good code eschews obfuscation.
Well documented code saves money.