Tuesday, February 10, 2015

Juxtaposed Between a Mess and Clean Code

On the one hand, I'm learning so much about how to write clean code; while on the other, I inherited a complete mess. At the same time, I'm working on another legacy code base that was the number one mess. If anything, this juxtaposition is reinforcing the lessons I'm entrenching myself in.

Let this example suffice to illustrate the mess I'm washing through:

if(($_GET['job'] == 'placeorder') || ($_GET['job'] == 'cancelorder'))
$form .= '<div '.lookup_data($data, get_user(...),...,).' class="'.get_class_for_job($_GET['job']).'"'.....more form stuff....
$tmp_result = $db.mysqli_query('select '.$tbl_order_columns_array.explode().' from tbl_orders where '.check_for_id($_GET['order_id']....db result handling to follow
$form .= '<script type=\"text/javascript\"> function checksomething('.$_GET['order_id'].'....more escaped javascript followed by more php, html, SQL and comments that state the obvious. And the typical length of files is on the order of thousands of lines with each statement on one line. Some lines go on for hundreds of characters.

The whole program is like a run-on sentence with the structure being dictated by the layout of the page. There are some base helper functions that do some work. However, the generic nature of those interfaces make for too much logic for them to be easily understood. I'm encountering if statements that are hundreds of lines long followed by an else or perhaps elseifs; nested ifs on the order of five or more; loops that contain hundreds of lines of code. The bottom line is that all of this makes for really poor readability. When code is difficult to read, it is difficult to comprehend. When it is difficult to comprehend, it is difficult to support. When it is difficult to support, service level declines, defect rates go up, support eats up more time which means less time to spend on other commitments, which means defect rates go up... Enter the viscous cycle!

No comments:

Post a Comment