Page 1 of 1

Use a multiple functions or just one with more arguments?

Posted: Sat Aug 06, 2011 9:41 am
by adbertram
In my minimal amount of PHP development experience, this is a question I've asked myself numerous times. Is it "better" to use multiple functions to perform a task or simply use a single function but with more arguments?

Take this most recent scenario as an example. I'm developing an application that will be for people that sell on Amazon. One of the purposes of this application is to calculate total expenses a seller may incur if listing a particular item on Amazon such as Amazon fees, shipping supplies, etc. Would it be better to do something like this:

Code: Select all

public function getAmazonExpense($category,$type) {
    switch ($category) {
        case 'media':
           return 'something';
           break;
       case 'gardening':
           return 'something';
           break;
       default:
           return 'something else';
}
or this:

Code: Select all

public function getAmazonMediaExpense($type) {
     return 'something';
}

public function getAmazonGardeningExpense($type) {
      return 'something';
}

Re: Use a multiple functions or just one with more arguments

Posted: Sat Aug 06, 2011 10:19 pm
by Jonah Bron
Use separate methods. It's best to lean toward the side of a smaller method than a larger one. On reason is that the purpose of the method is explained in the name if it's small. If the method is larger, it becomes more difficult to use, more difficult to understand, more difficult to maintain, and requires more documentation.

I highly recommend Clean Code by Robert C. Martin (ISBN-0132350882). It covers stuff like this in-depth.

Re: Use a multiple functions or just one with more arguments

Posted: Sun Aug 07, 2011 9:49 am
by adbertram
Thanks!

Re: Use a multiple functions or just one with more arguments

Posted: Mon Aug 29, 2011 11:36 pm
by KCAstroTech
I've often pondered similar questions in my apps and have naturally broken them down into smaller more meaningful functions. Like you said this makes them less confusing and easier to manage. Also, I try and take any large chunks of code that I repeat more than twice in two different functions and create a new function specifically for that task. That way the code is lighter and should easily provide a logical structure to follow.

Also, thanks for the book recommendation! :D

Re: Use a multiple functions or just one with more arguments

Posted: Mon Sep 05, 2011 3:34 pm
by jonesi
An alternative approach would be to consider an interface:

Code: Select all

<?php 

interface Expense {
	public function getCost();
}

class fees implements Expense {
	public function getCost() {
		// Code to calculate fees goes here.
		$fees = 100;
		return $fees;	
	}
}

class shipping implements Expense {
	public function getCost() {
		// Code to calculate shipping goes here.
		$shipping = 9.99;
		return $shipping;	
	}
}

$expensesList = array(new fees, new shipping);
$total = 0;
foreach ($expensesList as $item) {
	$total = $total + $item->getCost();
}
print $total;

?>