Skip to content

Cleaning up old code, and learning PHP – Week 6

Following on from last week’s unit testing for eprints, this week I took on the gargantuan 120 line function. It sucked up almost an entire day all by itself, but eventually I had each and every condition and result tested. The next step was, of course, to break it down into readable maintainable chunks of code. I took sections, some of which were originally marked out as separate sections in comments, and put each one it its own function. I also fixed a broken function called is_valid_date that had been returning true without carrying out any checks. Instead of using complicated regex and lots of if statements one after the other, as this function had been before it was given up on and prepended with “return 1;”, I decided to use a Dates library and let that attempt to parse the string being passed in. If it raised any errors I returned false, the string isn’t a valid date, else I returned true. This cut the function down to less than half its original length. It was great being able to put some of the ideas I read about in my first couple of weeks here into practice.

I also got to contribute towards the development of ThisIsMalware, a system I’d been a part of the requirement capture for a few weeks ago. This got me programming in PHP, an entirely new experience for me. After having tried my hand at Perl, it didn’t seem too complicated on a small scale, but I did find that in a larger web application I started to lose track of the overall structure of the system.

Posted in Perl, PHP, Programming.

Tagged with .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

or, reply to this post via trackback.