Jozsef Hocza

My Adventures To Spaghetti Code Land

For the next one or two week, I have to work on a legacy code that I’ve created many years ago.
When I looked at the code it was like: Jesus, god, anything else but not this!

If you do not want to burn your eyes out, feel free to scroll thru the code really fast. :)

<?php

for($i=0;$i<$rarray['arrayinfo']['rownumber'];$i++){
    $rarray6 = $GSql->Select("training_schedule_days","Count(*)","DayType=4 AND ScheduleOid=".$rarray[$i][0]);
    $prepayments_number = $rarray6[0][0];
    $rarray2 = $GSql->Select("trainings","Name,OKJID","Oid=".$rarray[$i][1]);
    $rarray3 = $GSql->Select("training_clients","Count(*)","ScheduleOid=".$rarray[$i][0]." AND ClientStatusOid=-1");
    $stood_back = $rarray3[0][0];
    $rarray3 = $GSql->Select("training_clients","Count(*)","ScheduleOid=".$rarray[$i][0]." AND ClientStatusOid>-1");
    $rarray4 = $GSql->Select("training_clients","Count(*)","ScheduleOid=".$rarray[$i][0]." AND ClientStatusOid>2");
    $rarray8 = $GSql->Select("training_clients","Count(*)","ScheduleOid=".$rarray[$i][0]." AND ClientStatusOid>3");
    $rarray5 = $GSql->Select("training_schedule_days","Date","DayType=2 AND ScheduleOid=".$rarray[$i][0]);
    $rarray6 = $GSql->Select("training_schedule_days","Date","DayType=3 AND ScheduleOid=".$rarray[$i][0]);
    $rarray7 = $GSql->Select("training_schedule_days","Date","DayType=17 AND ScheduleOid=".$rarray[$i][0]);
    $tajstart = $rarray5[0][0];
    $tajend = date("Y-m-d",strtotime($tajstart." + 7 days"));
    $today = date("Y-m-d");
    if($today >= $tajstart && $today <= $tajend && !empty($rarray5[0][0])){
        $stilus = 'style="background-color:#FDFFB8;"';
    }else{
        $stilus = '';
    }?>

    /* html stuff here */
}

That was the time when I was “trying to do some OOP code…” Hah, OOP. :) How fool I was.
Look at my “GSql” abomination that you saw previously. I am pretty sure If I did not lost you already, you are going to unsubscribe after this “Object”. :-D

<?php

class TMySql 
{
    var $FHost;
    var $FUser;
    var $FPw;
    var $FDatabase;
    var $FTable;
    var $FFields;
    var $FConnection;
    
    function TMySql($AHost, $AUser, $APw, $ADatabase) 
    {
        $this->FHost = $AHost;
        $this->FUser = $AUser;
        $this->FPw = $APw;
        $this->FDatabase = $ADatabase;
    }

    function Connect() 
    {
        $this->FConnection = mysql_connect($this->FHost, $this->FUser, $this->FPw);
        
        if (!$this->FConnection)
        {
            die('Could not connect: ' . mysql_error());
        }
        
        mysql_select_db($this->FDatabase, $this->FConnection);
    }
    
    function Disconnect()
    {
        mysql_close($this->FConnection);
    }
    
    function SetQuery($ATable, $AFields) 
    {
        $this->FTable=$ATable;
        $this->FFields=$AFields;
    }    

    function FetchArray($AWhere) 
    {
        $query = "SELECT $this->FFields";
        $query = "$query FROM $this->FTable";
        
        if($AWhere!="0")
        {
            $query = "$query WHERE $AWhere";
        }
        
        $result = mysql_query($query);
        $fields_exp = explode(',', $this->FFields);
        $i = count($fields_exp);
        
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
            print "<br>";
            
            for($j=0;$j<=$i;$j++)
            {
                $mezo = $fields_exp[$j];
                print "$row[$mezo] ";
            }
        }
    }
    
    function Select($ATable, $AFields, $AWhere="",$ADebug=0)
    {    
        $query = "SELECT $AFields";
        $query = "$query FROM $ATable";
        
        if(!empty($AWhere))
        {
            $query = "$query WHERE $AWhere";
        }
        
        if($ADebug==1) print "<font color=red><b>KVERI</b></font>: $query";
        $result = mysql_query($query);
        $fields_exp = explode(',', $AFields);
        $i = count($fields_exp);
        $k=0;
        /*
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
        {
            for($j=0;$j<$i;$j++)
            {
                $mezo = $fields_exp[$j];
                $rarray[$mezo][$k] = $row[$mezo];
            }
            $k++;
        }
        */
        
        while ($row = mysql_fetch_array($result, MYSQL_NUM))
        {
            for($j=0;$j<$i;$j++)
            {
                $rarray[$k][$j] = $row[$j];
            }
            $k++;
        }
        
        $i = 0;
        $rarray['arrayinfo']['rownumber'] = $k;
        $rarray['arrayinfo']['cellnumber'] = count($fields_exp);
        return $rarray;
    }
    
    function NameById($ATable, $AValue)
    {
        $query = "SELECT Name";
        $query = "$query FROM $ATable WHERE Oid=$AValue";
        $result = mysql_query($query);
        $row = mysql_fetch_row($result);
        return $row[0];
        
    }
    
    function Update($ATable, $AFields, $AValues, $AWhere,$ADebug=0)
    {
        $fields_exp = explode(',', $AFields);
        $values_exp = explode(',', $AValues);
        $query = "UPDATE $ATable SET";
        $i = 0;
        
        if(count($fields_exp)!=count($values_exp))
        {
            print "<br><b>Helytelen kit�lt�s. Valamelyik mez� nem lett kit�ltve.</b><br>";
            return 0;
        }
            while(isset($fields_exp[$i]))
            {
            
                if($i!=0)
                {
                    $query = "$query,";
                }
            
                $query = "$query $fields_exp[$i] = $values_exp[$i]";
                $i++;
            }
        
        $query = "$query WHERE $AWhere";
        
        if($ADebug==1) print "Update KVERI: $query";
        mysql_query($query);
    }
    
    function Delete($ATable, $AWhere,$ADebug=0)
    {
        $query = "DELETE FROM $ATable WHERE $AWhere";
        if($ADebug==1) print "DELETE KVERI: $query";
        mysql_query($query);
    }
    
    function Insert($ATable, $AFields, $AValues,$ADebug=0)
    {
        
        if(count($fields_exp)!=count($values_exp))
        {
            print "<br><b>Nem helyes a kit�lt�s. Valamelyik mez� nem lett kit�ltve.</b><br>";
            return 0;
        }
        
        $query = "INSERT INTO $ATable ($AFields) VALUES($AValues)";
        if($ADebug==1) print "KVERI: $query";
        mysql_query($query);
    }
    
    function FetchDropDown($AName, $ATable, $AField1, $AField2) 
    {
        $query = "SELECT $AField1, $AField2";
        $query = "$query FROM $ATable";
        $result = mysql_query($query);
    
        print "<SELECT NAME=$AName>"; // Ennek meg nem lett n�v adva
        
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
                print "<OPTION VALUE=$row[$AField1]>$row[$AField2]";
        }
        print "</SELECT>";
    }
    
    function Log($ALog,$APublic)
    {
        $date = date("Y-m-d H:i:s");
        $this->Insert("tlog","Date,Log,Public","'$date','$ALog',$APublic");
    }
    
    function insert_array($table, $insert_values,$ADebug=0) 
    {
        foreach($insert_values as $key=>$value) { 
            $keys[] = $key; 
            $insertvalues[] = '\''.$value.'\''; 
        } 
        $keys = implode(',', $keys); 
        $insertvalues = implode(',', $insertvalues); 

        $sql = "INSERT INTO $table ($keys) VALUES ($insertvalues)";
        if($ADebug==1) print "<font color=red><b>KVERI</b></font>: $sql";  
        mysql_query($sql);
        //$this->sqlordie($sql); 
    }
    function update_array($table, $keyColumnName, $id, $update_values,$ADebug=0)
    {
        foreach($update_values as $key=>$value) { 

            $sets[] = $key.'=\''.$value.'\''; 

        } 
        $sets = implode(',', $sets); 

        $sql = "UPDATE $table SET $sets WHERE $keyColumnName = '$id'";
        if($ADebug==1) print "<font color=red><b>KVERI</b></font>: $sql";
        mysql_query($sql);
        //$this->sqlordie($sql); 
    }
}

The interesting part, that back in those years other developers were using my classes like this. Some sites/apps still using them… How I know that? Two developers have contacted me when they upgraded to PHP7 because their code got broken… :)

the bright side

I realized that It is not something you should be sad about. If you look back at your code and you say: “What the piece of …. is that?” it means one thing. You have improved.

In this big world there are developers who stuck on their level and still look at their spaghetti code like some masterpiece. They have stuck in the past. They stopped evolving. They just stay bad. They think they know everything. Even if they knew everything yesterday, the world did not stop with them.

So if you can admit, that your previous code is bad, it means you got better. Nowadays I look back at a 6 months old project and I say: “Well… damn…. that was a pretty dumb solution…”

So, be happy that you were bad. :)

I also hope that today’s code you write will be also bad for your future self.

Do not forget: There is always room for improvement. Once you think that you cannot evolve any further, you just became those poor guys, living in the past.


Share this:

SUBSCRIBE
Subscribe to my e-mail list.
You can unsubscribe anytime.