1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP developers : How do you handle bad codes?

Discussion in 'PHP' started by atik, Mar 16, 2013.

  1. #1
    I have been using PHP for over 5 years now. I mainly do freelancing projects. Its quite common to find php projects which are very poorly written, unstructured code, mix of design & logic. You already know how painful it is to extend or modify such type of projects. The small projects might be handled with some extra afford, but the complexity of managing such poor projects increase exponentially with the increasing size of the project. Especially medium to large projects if written poorly without any coding convention or guideline are unmanageable. I personally find these types of projects boring and in many cases I found myself low productive with those projects. I was wondering how you feel about those projects? How you keep yourself motivated with such projects?
     
    Solved! View solution.
    atik, Mar 16, 2013 IP
  2. jason_at_snip

    jason_at_snip Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    Yes, you're talking about "legacy code". I've dealt with the same thing quite a bit myself. I would recommend reading Michael Feathers' Working Effectively With Legacy Code. The author defines legacy code as "code without tests", which I find to be an interesting definition. Basically, the general idea as I understand it is to establish a beachhead with a couple trivial tests and work your way outward from there. He says each positive change can feel like a pointless drop in the bucket, but if you're persistent over time, you do start seeing a significant difference.

    I've been applying Michael Feathers' techniques, to a certain extent, to one of my projects over the last couple months and I definitely feel a little better about my code now.
     
    jason_at_snip, Mar 16, 2013 IP
  3. #3
    Back in the '80's I was working closely with a developer who'd been writing cobol and fortran apps about as long as I'd been alive at that point... and I'm going to tell you exactly what he told me. Something I tell people about their websites EVERY day.

    Sometimes it's easier, faster, and far, far less work, to throw it all out and start over from scratch, than it is to try and work with broken poorly written code.

    It sounds counterintuitive... It should be more work, but to even work with their code you have to understand what it does first -- Once you understand what it does, integrating your code to it is often not feasible, inefficient, and more work than rewriting the whole thing. One BIG mistake you can make though is making changes before you understand ALL of it... and what's the best way to understand all of it? Rewrite it.

    Take a client I had not so long ago (yeah, retired, right...) where they wanted a new website, but had a contact form that fed into a fancy contact management system; they wanted me to preserve that form... When I got to it... The markup was such garbage I couldn't leave it be, it was a standalone .php and I needed it integrated into the 'one index to rule them all' system, and it was ridiculously large code... for example, they had this function:

    function Decode($strValueIn) {
    //Do not modify this function!
    $intX = 0;
    $intY = 0;
    $Temp = "";
    $Mod = "";
    $intMod =0;
    $intTemp = 0;
    $ValueOut = "";
    $intY = 1;
    $strValueOut="";
    for ($intX=1; $intX<=((strlen($strValueIn)/2)/2); $intX=$intX+1) {
        $strTemp="";
        $strMod="";
        if (($intX % 2)==0) {
          $strMod=substr(substr($strValueIn,$intY-1,4),0,2);
          $strTemp=substr(substr($strValueIn,$intY-1,4),strlen(substr($strValueIn,$intY-1,4))-(2));
        } else {
          $strMod=substr(substr($strValueIn,$intY-1,4),strlen(substr($strValueIn,$intY-1,4))-(2));
          $strTemp=substr(substr($strValueIn,$intY-1,4),0,2);
        }
        $intMod=hexdec($strMod);
        $intTemp=hexdec($strTemp);
        $intTemp=$intTemp-$intMod;
        $strValueOut=$strValueOut.chr($intTemp);
        $intY=$intY+4;
      }
      return $strValueOut;
    }
    Code (markup):
    Which is massive garbage... I did a quick rewrite of it dropping all the unused variables, re-arranging the logic into something more efficient

    function myDecode($inString) {
    	$result='';
    	$t=0;
    	while ($t<strlen($inString)) {
    		$top=hexdec(substr($inString,$t,2));
    		$t+=2;
    		$bottom=hexdec(substr($inString,$t,2));
    		$t+=2;
    		$result.=chr(($t%8)==0 ? $bottom-$top : $top-$bottom);
    	}
    	return $result;
    }
    Code (markup):
    Functionally identical -- but taking the time to rewrite it was a mistake! WHY? Because I didn't look at what was calling it first -- again, understand it before you rewrite. What was using it?

    It was being used to encode/decode the users login password, mysql password, mysql username and mysql hostname AND SEND THEM AS HIDDEN INPUTS IN THE FORM!!! Whiskey tango foxtrot! Security? WHAT'S THAT?!? It would then decode those value on the submit (with an entire separate copy of the same routine, shared libraries, WHAT'S THAT?!?) and use them to connect to the DB... But the handler was the SAME FILE so the values for mysql at least WERE ALREADY IN IT! NOT that this handler actually used mysql for ANYTHING!

    The more I tugged on things, the worse it got -- it had this massive over-commented uselessly commented train wreck of code I couldn't make any sense out of until I looked at the output -- where all it was doing is outputting the result from the form as XML to be sent attached as a e-mail to the contact software!

    There's a reason I kneejerk every time I see idiotic BS like:
    /* start decode function */
    function decode($inString) {

    Just trying to reskin it was dreadful because it wasn't just outdated invalid garbage markup, FINDING the markup in the massive train wreck of code? GOOD LUCK WITH THAT!

    The original contact form and standalone handler came in at over 200k for one php file, most of it idiotic garbage on how NOT to do a contact form or build a XML result file. My rewrite was just a hair over 4k. Which one is easier to integrate with new code? Which is easier to update? Which is easier to skin? Which approach would have taken me less time, trying to make that 200k monstrosity sit up and play nice, or figure out "This takes a form and turns it into XML to be e-mailed" and write my own 4k handler for it? (well, ok, 8k if you include the 4k contact.template.php)

    Really, if it's garbage, tell the client it's garbage and fix it charging for the time... if it's REALLY garbage weigh the advantages of trying to 'fix it' or even just try and do whatever it is you are in there to do, against how long it would take for you to just write the same thing from scratch. Quite often, throwing it out and starting over is faster, less effort, because it lets you discard all that broken bloated legacy code and do things RIGHT.
     
    Last edited: Mar 16, 2013
    deathshadow, Mar 16, 2013 IP
  4. atik

    atik Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    26
    #4
    So true, I feel the same way. Though its not always the case client agrees with new solution instead of his 'existing junk' solution at the beginning, but eventually they were charged enough by the end of the project which could gave them better projects in less time if they wanted to start from scratch.
     
    atik, Mar 16, 2013 IP
  5. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #5
    As others said, look at the code and think if its good for the future to rewrite certain parts. I've done alot of rewrites of sites as the orginal code looked like garbage and then spend a week to make it readable. Most times it worked for me as i got more jobs/things to do with the code, but sometimes its not paying off and it was a waste of my time..

    With huge projects i'm calculating the rewrite in the 'total' price but when the buyer wants a small fix i make the small fix and that's it.
     
    EricBruggema, Mar 17, 2013 IP
  6. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #6
    I surgically splice it code and re-write it in ways of my own habit and identification purposes.
    Even if the code looks pretty good I'll edit and drop my own notes // Note example.
    I also sometimes use a style that might not be perfect in pagination or for other programmers to read.
    That started while working on a few higher level ecommerce sites that I noticed had been covered with code snippets from every coder they had hired prior to me. The programmers were sticking all sorts of activation code snippets in there ..espeically at email scripts. (example) every email sent to the site owner was also sending a duplicate to the programmer company. Some of them were quite interesting the way codes were stashed or put into handlers and wouldn't have been found by a glance. Some activating in several stages of If and /else if and pulling handlers that were labeled/ named as things unrelated. Calling functions with names that has passive names unrelated except one I found that I still think is funny how they named the function 'snoop'. Then when checking background on the former programmers, they were also working for a similar company that did web design and program work FOR those types of companies to get their program code in and swipe their emails. I was even able to get them to send me advertisements via email to false emails I set up into the former database. (they were activating code whena user name was stored it would also send them an email of their shipping/email etc) They were automating it so they were able to send email advertisements to names like Ben Dover, Hugh G. Rection, Hugh Jass etc. I had to rewrite all the code from scratch through that particular site. They had tried a keypair script for nGnupg as well. Suprising what some will hide in code so you might always want to scan through code for that purpose. That was back in the day when I tried to retalliate by using hyperlink of Google Adword and put it in link keyword of "Does anybody know what the heck this is?!..I'll pay $100 if you can tell me!" That was the text link, the link was their adword link. Not sure if it was 100% successful. But I DID get to send them lots of BS user members because I saved the code and would activate thousands of usernames when ever I was bored around 2:30pm time of day and needed a quick chuckle.
     
    ezprint2008, Mar 17, 2013 IP