• RSS
  • Print this article!
  • Digg
  • del.icio.us
  • DZone
  • Facebook
  • Mixx
  • Google Bookmarks
  • Design Float
  • Reddit
  • StumbleUpon
  • Technorati
  • Live
  • TwitThis
Home > Development Best Practices > Reasons Why You Should Use a Function

Reasons Why You Should Use a Function

June 11th, 2009

When is a good time to use a function or a method? Here is a list of reasons when you should use them.

Reduce Complexity

This is probably the most important of all reasons to use a function.  You can create a function and forget about its inner workings.  This creates an abstraction of all the code within it by hiding the information every time you use the function. Not to mention it helps with code size, maintainability, and correctness.

Example:

1
 

//Complex ———————–
if ($val >= 32 || $val < 10){

for($i =0; $i < 10; $i++){
echo “name”. $i
}
}else{
echo “no name”;
}

//Simple ———————–
echo getName($val);

?>

Make Code Easier To Read

Putting a piece of code within a well named function is one of the best ways to document your code. Instead of reading multiple lines of code you can simply read a single descriptive line of code.

Example:

1
 

// Difficult to read…it takes time to
//figure out what is going on.
$names = array(’Bob’,'Ted’,'Bill’);
for ($i = 0; $i < count($names); $i++){
echo $names[$i];
}

//Self explanitory code.
DisplayNames();

?>

Avoid Duplicate Code

If you find that you have several instances of the same code spread through out, it is probably a good indication you should use a function in its place.

Example:

1
 

function getUserName(){
$firstname = getFirstName();
$lastname = getLastName();
$name = $firstname . ” ” . $lastname;
return $name;
}

// Functions prevents having to use duplicate code.
echo “Welcome “. getUserName() . “!”;
echo “What kind of name is “. getUserName() . “?”;

?>

Hide Sequences

It is usually a good idea to hide the order in which things happen.  If a program has a set of processes it has to go through, it’s best that each process doesn’t have to rely on any others to work.  Placing each process in it’s own function will help keep things organized.

Example:

1
2
3
4
5
6
/*
* Splitting up the process into functions,
* each process doesn't know what the other
* one is doing. This allows you to make changes
* to the process with little to no trouble.
*/

$userName = getUsername();
$validUsername = checkUsername($userName);
updateDatabase($validUsername);

?>

Improve Portability

Use functions to help portability by isolating code into nice little packages.

Example:

1
2
3
function add ($a, $b){
return $a + $b;
}

// First lame program
echo add(1,7);

//Function works just as well in any program…as lame as it might be. ;)
for ($i = 0; $i
echo $;
}
?>

Simplify complicated Boolean Tests

If you have an overly complicated Boolean test, placing the test within a function can help make your condition statements a lot easier to read, and use.

Example:

1
 

//Complicated
if ((2*5+3/7)/2 > 30){
echo “wtf?”;
}

//Simplified
if (isValidNumber()){
echo “Ahh much better.”;
}

?>

Improve Performance

Instead of running around trying to optimize your code in several places, functions allow you to place your code into one place.  This way you can optimize your code in one place and it will take effect in several areas, in turn making your life easier.

  • RSS
  • Print this article!
  • Digg
  • del.icio.us
  • DZone
  • Facebook
  • Mixx
  • Google Bookmarks
  • Design Float
  • Reddit
  • StumbleUpon
  • Technorati
  • Live
  • TwitThis
  1. EllisGL
    June 11th, 2009 at 23:34 | #1

    Actually, functions and classes slow down execution of code, class more so. If you want the performance, only use functions for recursion. Of course for the best performance, create extensions for everything you are doing!

  2. June 12th, 2009 at 07:54 | #2

    Good point, I meant for ease of improving performance functions help. It’s more practical to recode a routine with a more efficient algorithm and have it update where ever the function is being used, than having to search out every piece of code and update it. A single optimization benefits the whole code. But you are right, use function sparingly, just be smart about it if it is going to save you time.

  3. August 17th, 2009 at 21:05 | #3

    If you are using enough functions and classes to slow down your code noticeably then you are doing something wrong. That said, I would never do the add() function… it lends itself to writing things like:

    add(add(add(add(add(1,2),3),4),5),6)

    which is far less readable than

    1 + 2 + 3 + 4 + 5 + 6

  4. August 26th, 2009 at 05:42 | #4

    SeanJA :
    If you are using enough functions and classes to slow down your code noticeably then you are doing something wrong…

    I completely agree with your point Sean. I would imagine that this blog is generally written with the type of user in mind that creates and administers small/medium size web applications. In these cases, the myraid benefits of clean, portable code far outstrip any concerns that ‘the result came back in 12ms rather than 8ms’. The users won’t notice the difference; but the designer certainly will when he/she comes to code their next project…

    Obviously enterprise-scale applications have a whole different set of rules; but thankfully I get to dodge that bullet!

Comments are closed.