Use a multiple functions or just one with more arguments?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
adbertram
Forum Newbie
Posts: 10
Joined: Sun Jul 31, 2011 10:18 pm

Use a multiple functions or just one with more arguments?

Post 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';
}
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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.
adbertram
Forum Newbie
Posts: 10
Joined: Sun Jul 31, 2011 10:18 pm

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

Post by adbertram »

Thanks!
KCAstroTech
Forum Commoner
Posts: 33
Joined: Sat Aug 27, 2011 11:16 pm

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

Post 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
jonesi
Forum Newbie
Posts: 8
Joined: Tue Aug 23, 2011 2:15 pm

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

Post 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;

?>
Post Reply