DEV Community

Theofanis Despoudis
Theofanis Despoudis

Posted on

This is the feeling of working with legacy code.

A long time ago when I started my career as a developer I had both the unfortunate and fortunate experience of working with existing code - that is code that was already in place and I inherited.

The thing now is that this code could be awesome, clean and tidy or could be in the other end; messy, spaghetti style that is hard to understand and to work with.

As you may already have guessed, I inherited the latter... and I was so desperate that I copied some part of that to study it at home.

Years, later when I was reorganizing some of my files I found those pieces of code, and today I will give you a small sample of it for the future generation to have a taste of it.

Here it is then. (a little bit reformed). Enjoy!



<?php
// Search page
$mt_ttle = 'Search Results - ';
if(!$_GET['q']) $q = ''; else $q = furl($_GET['q']);
if(!$_GET['flt']) $flt = ''; else $flt = furl($_GET['flt']);
$qLen = strlen($q);
$con = '<div class="plainHdr pgHeader searchb">' . $srch_rslts . '</div>';

if ($qLen <= 3){
    if ($qLen == 0) $err = 'No Keyword was entered.'; else $err = 'Keyword is too short.';
    $con .= '
    <div class="grid3_2col">
        <h3 class="nr mb20">' . $err . '</h3>
        <div class="paraBig">Please make sure you enter a keyword which is longer than 3 characters.<br />Try again in the Search field above.<br /><br /></div>
        <div class="nr mt20">PROBLEM SEARCHING?<br />If you continue having this message please <a href="' . url() . '/report_a_problem">Report this Problem</a> and<br />we will get back to you the soonest possible.</div>
    </div>' . $signup_quick_links;

} else {

    $sExp = preg_split('/\s+/',$q);
    $secure_keywords = array();
    foreach ($sExp as $key=>$keyword){
        if (strlen($keyword) >= 3){ // cut off words that are less than 3 chars
            $secure_keywords[] = $keyword;
        }
    }
    $kwords = count($secure_keywords);
    foreach ($secure_keywords as $key=>$keyword){
        $keyword = sql_proof($keyword);
        $srchqr .= "`en_title` LIKE '%$keyword%' OR `gr_title` LIKE '%$keyword%' OR `keywords` LIKE '%$keyword%'";
        $blgqr .= "`blog_title` LIKE '%$keyword%' OR `description` LIKE '%$keyword%'";
        $prfqr .= "`pfTitle` LIKE '%$keyword%' OR `pfDesc` LIKE '%$keyword%'";
        if ($key != ($kwords - 1)){
            $srchqr .= " AND "; $blgqr .= " AND "; $prfqr .= " AND ";
        }
    }

    if ($flt != 'blog' && $flt != 'feature'){
        if ($flt == 'cat') {if($_GET['val']){$val = furl($_GET['val']); $prfqr = $prfqr . " AND `pfCategory`='$val'";}}
        $sqlCommand = "SELECT * FROM `profiles` WHERE $prfqr"; // ---------- : Profiles
        $query = mysqli_query($myConnection, $sqlCommand) or die ();
        $prfrows = mysqli_num_rows($query); $sdcats = array();
        if ($prfrows >= 1){
            while ($row = mysqli_fetch_array($query)){
                $pfTitle = $row['pfTitle']; $link_code = $row['link_code']; $pfDesc = $row['pfDesc']; $pfCategory = $row['pfCategory'];
                $srslts .= '<div><b><a href="' . url() . '/business/'.$link_code . '">'.$pfTitle . '</a></b><br />
                <span>' . ucwords(str_replace('_',' ',$pfCategory)) . '</span><br />' . $pfDesc . '</div>';
                $sdcats[] = $pfCategory;
            }
            asort($sdcats);
            $counts = array_count_values($sdcats);
            foreach($counts as $cuVal => $cuCount){
                $sqlCommand = "SELECT `en_title`, `gr_title` FROM `categories` WHERE `link_code`='$cuVal' LIMIT 1";
                $query = mysqli_query($myConnection, $sqlCommand) or die ();
                $row = mysqli_fetch_array($query); $caTtl = $row[$lang . '_title'];
                $sd_panel .= '<a class="sdl" href="' . curPageURL() . '&flt=cat&val=' . $cuVal . '">' . $caTtl . ' <span>(' . $cuCount . ')</span></a>';
            }
        }
    }

    if ($flt != 'cat' && $flt != 'feature'){
        $sqlCommand = "SELECT * FROM `blog` WHERE $blgqr"; // ---------- : Blogs
        $query = mysqli_query($myConnection, $sqlCommand) or die ();
        $blgrows = mysqli_num_rows($query);
        if ($blgrows >= 1){
            while ($row = mysqli_fetch_array($query)){
                $blg_title = $row['blog_title']; $link_code = $row['link_code']; $description = $row['description'];
                $srslts .= '<div><b><a href="' . url() . '/blog/'.$link_code . '">'.$blg_title . '</a></b><br /><span>Article</span><br />' . $description . '</div>';
            }
            $sd_panel .= '<a class="sdl" href="' . curPageURL() . '&flt=blog">Blog Posts <span>(' . $blgrows . ')</span></a>';
        }
    }

    if ($flt != 'cat' && $flt != 'blog'){
        $sqlCommand = "SELECT * FROM `categories` WHERE $srchqr"; // ---------- : Features
        $query = mysqli_query($myConnection, $sqlCommand) or die ();
        $schrows = mysqli_num_rows($query);
        if ($schrows >= 1){
            while ($row = mysqli_fetch_array($query)){
                $catTitle = $row[$lang . '_title']; $link_code = $row['link_code']; $catDescr = $row[$lang . '_descr'];
                $srslts .= '<div><b><a href="' . url() . '/' . $link_code . '">' . $catTitle . '</a></b><br /><span>Feature</span><br />' . $catDescr . '</div>';
            }
            $sd_panel .= '<a class="sdl" href="' . curPageURL() . '&flt=feature">Features <span>(' . $schrows . ')</span></a>';
        }
    }

    $numrows = $schrows + $prfrows + $blgrows;

    if ($flt != ''){
        $sd_panel = '<a class="sdl" href="' . url() . '/search?q=' . $q . '">Show All Results</a>';
    }

    if ($numrows >= 1) {
        if ($numrows > 1) $rescap = 's'; 
        if ($flt == '') $sfhd = '<div class="nr mb10 sdtls"><b>' . $fltr_ct . '</b></div>';
        $con .= '<div class="nr midHd mb20">' . $numrows . ' Result' . $rescap . ' Found for <b>“' . $q . '”</b></div>
        <div class="sdclmn">' . $sfhd . '<div class="nr bt1">' . $sd_panel . '</div></div>
        <div class="rgtpnl"><div class="nr mb10 sdtls"><b>Results</b></div><div class="nr bt1 scrlts">' . $srslts . '</div></div>';

    } else {
        $con .= '<h3 class="nr mb20">No Results Found</h3>' . $signup_quick_links;
    }
}
?>


Enter fullscreen mode Exit fullscreen mode

Top comments (15)

Collapse
 
sudiukil profile image
Quentin Sonrel

<?php

I stopped there, I believe you 😂

Collapse
 
thebouv profile image
Anthony Bouvier

New and shiny code today is the legacy code of tomorrow.

Collapse
 
mfp22 profile image
Mike Pearson

True, but that code sucked back then too.

Collapse
 
alainvanhout profile image
Alain Van Hout

I once came across a developer that was able to write 'instant legacy' code.

Collapse
 
drewknab profile image
Drew Knab

Yep, this.

I once inherited a PHP application in a single index.php that echo'd four or five different HTML pages depending on a post back to itself and whether it was post or get and then the contents of the post or get.

All of the business logic looked exactly like this.

Collapse
 
okolbay profile image
andrew

I once wrote an application like this. I guess we all have dark pages of our history )

Collapse
 
aksel profile image
aksel

He goes on to say that code becomes "legacy" because it works, and no one wants to change something that works, and his goal anytime he's writing software is to make it so good that it'll one day become legacy.

That's a pretty cool way of seeing things. Of course, it wouldn't always work on a large scale, where the requirements can change much later on. But when working on something isolated, some specific functionality, I think it's a good mantra.

Collapse
 
catriname profile image
catrina • Edited

i'd have to say this actually looks clean? you have to take into context the time frame it was created and what was available then. "front end" development didn't exist in the slightest.

i inherited asp (classic) legacy code and grew through php that looks much like this and couldn't go past certain versions because of the "zend" restrictions/legacy connections. it is the reality of being a developer.

thanks for posting something so raw. it really gives people a grasp of what code is actually out there!

Collapse
 
theodesp profile image
Theofanis Despoudis

It was in 2013, at least we had Angular.js

Collapse
 
31547 profile image
31547 • Edited

this HURTS. what hurts worse is that when i first got into web development and backend development by screwing around on php5 when i was 13, i did exactly this.

hurr, whats an API??? MVC??? :thinking:

Collapse
 
scottishross profile image
Ross Henderson

I know the feeling! I inhereted a HTML4 site that was based on a Wiki Site. Had to strip that all out and bring it into HTML5!

Collapse
 
joshcheek profile image
Josh Cheek

Always fun to see the Gilded Rose in production! ❤️

Collapse
 
theodesp profile image
Theofanis Despoudis

In that case, it's more than a stripped Corpse flower. The gilded rose can be refactored easier.

Collapse
 
joshcheek profile image
Josh Cheek

Gilded Rose is as logically as gnarly as this, the reason this is harder to refactor is b/c it's (presumably) untested and it depends on web requests and a database.

Collapse
 
thebouv profile image
Anthony Bouvier

And just like people, it rots.

:D