«

»

Jun 25

Wrapper functions make programming easier.

One of the things I have come to learn with any programming language is that more often that not you will be making changes to code and a lot of the time this will be in multiple places.

One of the Code Smells for Refactoring is repeated code and one method to fix this is generally putting the code in a function so you can call it, in turn this makes the repeated code easier to maintain. however it’s not just repeated code that you want to put inside functions but library functions you use repeatedly aswell.

Earlier versions of PHP used to use mysql_ functions. later mysqli_ was used and repeated times people were told to stop using mysql_ as it was depreciated and will one day no longer exists in builds of PHP.

Now imagine a huge ass project with alot of pages and alot of work with a database. you built it with mysql_ when it wasn’t depreciated but when you upgraded PHP it failed. now you have to go though every page, every line of code, replacing your mysql_ functions with mysqli_ functions, and also in some cases like with the query functions you will have to change the argument order. this is quite the task when you have hundreds of thousands of lines of code.

by wrapping your mysql_ functions in your own functions with near identical arguments when you need to change to mysqli_ your not looking through your entire directory of code making changes, you’ll just me making changes to one part and with our query example you can swap the argument order for the mysqli_ function even easier. bellow illustrates this

function wrap_sqlQuery($query, $link)
{
    mysql_query($query, $link);
}

function wrap_sqlQuery($query, $link)
{
    mysqli_query($link, $query);
}

as you can see if you had wrap_sqlQuery in 100 places and you went from mysql_ to mysqli_ you only need to make the change to one line rather than 100.

this also helps when you changes libraries or technologies. using our database query examples imagine if you want to replace your MySQLi library with PDO. remember that PDO has no procedural syntax like what the previous 2 libraries did

function wrap_sqlQuery($query, $link)
{
    mysqli_query($link, $query);
}

function wrap_sqlQuery($query, $link)
{
    $link->query($query);
}

as you can see same function, same argument lists, different code. the amount of code you might need to change will be alot less because you are making changes to your functions which will already have been called. you just need to make sure that the output of the function remains the same but then you just add more code to the function to create the same kind of format.

now while you could go crazy and wrapper every function there is a good way to tell if you should wrapper stuff. if you can choose between 2 or more different libraries or methods to do the exact same thing, that’s when it’s a good idea to consider whether or not to wrapper the function. also if you read about something like what happened with PHP with the change from mysql_ to mysqli_ you should see whether or not the older syntax is going to be removed sometime in the future and if so start working on wrappering the functions then. sure it’ll be a lot of work however in the end you’ll be prepared if it happens again

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>