PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Jul 03, 2020 3:18 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sat Aug 06, 2011 9:41 am 
Offline
Forum Newbie

Joined: Sun Jul 31, 2011 10:18 pm
Posts: 10
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:

Syntax: [ Download ] [ Hide ]
public function getAmazonExpense($category,$type) {
    switch ($category) {
        case 'media':
           return 'something';
           break;
       case 'gardening':
           return 'something';
           break;
       default:
           return 'something else';
}


or this:

Syntax: [ Download ] [ Hide ]
public function getAmazonMediaExpense($type) {
     return 'something';
}

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


Top
 Profile  
 
PostPosted: Sat Aug 06, 2011 10:19 pm 
Offline
DevNet Master
User avatar

Joined: Thu Mar 15, 2007 6:28 pm
Posts: 2765
Location: Redding, California
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 (). It covers stuff like this in-depth.


Top
 Profile  
 
PostPosted: Sun Aug 07, 2011 9:49 am 
Offline
Forum Newbie

Joined: Sun Jul 31, 2011 10:18 pm
Posts: 10
Thanks!


Top
 Profile  
 
PostPosted: Mon Aug 29, 2011 11:36 pm 
Offline
Forum Commoner

Joined: Sat Aug 27, 2011 11:16 pm
Posts: 33
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


Top
 Profile  
 
PostPosted: Mon Sep 05, 2011 3:34 pm 
Offline
Forum Newbie

Joined: Tue Aug 23, 2011 2:15 pm
Posts: 8
An alternative approach would be to consider an interface:

Syntax: [ Download ] [ Hide ]
<?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;

?>
 


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group