is less more or is more more?

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

User avatar
snowrhythm
Forum Commoner
Posts: 75
Joined: Thu May 04, 2006 1:14 pm
Location: North Bay Area, CA

is less more or is more more?

Post by snowrhythm »

these might sound like a total n00b questions, but regardless, i'm going to ask them. :D

in my applications is it better to have several, one-task functions, or a few long, multipurpose functions?

which is better programming practice? and is there a performance boost or hit for either one?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

small, concise objects and methods.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

less is more. Always. Quality over quantity, dark over milk chocolate. Small, concise methods trump god-classes in every way.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yup, definately one-task functions. The reason being that a function (or class or method) is much more reusable if it only does one thing. While being reusable may not immediately strike you as important, it is. For example, take these (not particularly useful) functions:

Code: Select all

function multiply($x, $y)
{
    $result = $x * $y;
    echo "<h1>Result</h1>\n";
    echo "<p><strong>" . $x . " * " . $y . " = " . $result . "</strong></p>";
}
function add($x, $y)
{
    $result = $x + $y;
    echo "<h1>Result</h1>\n";
    echo "<p><strong>" . $x . " + " . $y . " = " . $result . "</strong></p>";
}
function subtract($x, $y)
{
    $result = $x - $y;
    echo "<h1>Result</h1>\n";
    echo "<p><strong>" . $x . " - " . $y . " = " . $result . "</strong></p>";
}
function divide($x, $y)
{
    $result = $x / $y;
    echo "<h1>Result</h1>\n";
    echo "<p><strong>" . $x . " / " . $y . " = " . $result . "</strong></p>";
}
Now let's say somebody asks you to make your functions output no html. Since your functions do not just do their task and their task only, they cannot be reused now because they have already been implemented into your system as-is and changing them would change how your system now displays calculations. DARN! If you had written them like this, there would be no problem:

Code: Select all

function multiply($x, $y)
{
    return $x * $y;
}
function add($x, $y)
{
    return $x + $y;
}
function subtract($x, $y)
{
    return $x - $y;
}
function divide($x, $y)
{
    return $x / $y;
}
These functions could be used to display output however you like:

Code: Select all

$x = 4;
$y = 14;
$result = add($x, $y);
echo "<h1>Result</h1>\n";
echo "<p><strong>" . $x . " / " . $y . " = " . $result . "</strong></p>";
 // or ...
echo "Result\n";
echo $x . " / " . $y . " = " . $result;
Admittedly this is probably not the best example, but hopefully you get what I mean.
User avatar
snowrhythm
Forum Commoner
Posts: 75
Joined: Thu May 04, 2006 1:14 pm
Location: North Bay Area, CA

Post by snowrhythm »

wow, thanks for the replies. i totally see how that works better, and with the program i'm
creating right now reusable functions are going to play a big part in keeping things tidy.

what about the performance issue? is there an advantage to the smaller function system
as far as performance goes?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

The idea of small, reusable, concise objects or function allows the programmer to use only whats needed. If you are witting a "god" class it will almost certainly be including stuff you aren't using, therefore reducing performance.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

you really shouldn't worry too much about performance yet anyway... optimize only once your application shows that it needs optimization.

http://en.wikipedia.org/wiki/Optimizati ... o_optimize

EDIT: phpbb won't link that correctly, so you're going to have to copy and paste it
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

The Ninja Space Goat wrote:you really shouldn't worry too much about performance yet anyway... optimize only once your application shows that it needs optimization.
Indeed. Even if you have your site hosted on the IKEA version of a hosting company, you'll have a server that handles many similar sites anyway. Stick to trial and error and if something starts creaking look into why & fix it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ninja, that was an excellently worded explanation of reusability in code.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

Everah wrote:Ninja, that was an excellently worded explanation of reusability in code.
Agree, that was the perfect example to demonstrate the point. Nice one!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The Ninja Space Goat wrote:http://en.wikipedia.org/wiki/Optimizati ... o_optimize

EDIT: phpbb won't link that correctly, so you're going to have to copy and paste it
Fixed it for you. :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

patrikG wrote:...dark over milk chocolate...
8O

I have found my nemesis!
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

It's funny how everybody is talking about God classes. It seems to me that the idea of a Class as a controller makes it a God class.

I'm sorry. That's not on topic. I'm just brain dumping.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

newbie question ahead:

if i have the same function list as Ninja has above (in a file) and then I import it in and have the following php code in a separate file:

Code: Select all

<?php
include ('./includes/mathf.php');
if (isset($_POST['submitted'])){
$x = $_POST['x'];
$y = $_POST['y'];

if (isset($_POST['math']))
{
	switch($_POST['math'])
	{	
		case 'add' :
			$result = add($x, $y);
			echo "<h1>Result</h1>\n";
			echo "<p><strong>" . $x . " + " . $y . " = " . $result . "</strong></p>";
			exit();
		case 'subtract' :
			$result = subtract($x, $y);
			echo "<h1>Result</h1>\n";
			echo "<p><strong>" . $x . " - " . $y . " = " . $result . "</strong></p>";
			exit();
		case 'multiply' :
			$result = multiply($x, $y);
			echo "<h1>Result</h1>\n";
			echo "<p><strong>" . $x . " * " . $y . " = " . $result . "</strong></p>";
			exit();
		case 'divide' :
			$result = divide($x, $y);
			echo "<h1>Result</h1>\n";
			echo "<p><strong>" . $x . " / " . $y . " = " . $result . "</strong></p>";
			exit();
	}
}
}else{
?>
<html>
<body>
<form action="calc.php" method="post">
<fieldset>
<p>Number 1 (x):  <input type="text" name="x" size="3" />
<select name="math">
<option value="add">Add</option>
<option value="subtract">Subract</option>
<option value="multiply">Multiply</option>
<option value="divide">Divide</option>
</select>
Number 2 (y):  <input type="text" name="y" size="3" />
<input type="submit" name="sumbit" value="Do Math!" />
<input type="hidden" name="submitted" value="TRUE" />
</fieldset>
</form>
<?php
}
?>
</body>
</html>
Would it be better to have the echo statements in each case or should i have that at the bottom and dynamically change the operator symbol?

Wayne
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Wayne, what I'd do is have a template (a whole seperate php page) to display the output, so something like this:

Code: Select all

<?php
include ('./includes/mathf.php');
if (isset($_POST['submitted'])){
$x = $_POST['x'];
$y = $_POST['y'];

$tmplt = new Template;

if (isset($_POST['math']))
{
        switch($_POST['math'])
        {
                case 'add' :
                        $tmplt->result = add($x, $y);
                        $tmplt->op = "+";
                        break;

                case 'subtract' :
                        $tmplt->result = subtract($x, $y);
                        $tmplt->op = "-";
                        break;

                case 'multiply' :
                        $tmplt->result = multiply($x, $y);
                        $tmplt->op = "*";
                        break;

                case 'divide' :
                        $tmplt->result = divide($x, $y);
                        $tmplt->op = "/";
                        break;
        }

        $tmplt->x = $x;
        $tmplt->y = $y;
        $tmplt->render('display.tpl.php');

}
}else{
        $tmplt->render('form.tpl.php');
}
?>
</body>
</html>
display.tpl.php

Code: Select all

<h1>Result</h1>
<p><strong>{$x} {$op} {$y} = {$result}</strong></p>
that way if I want to change how it renders, I simple change the template file:

display_no_html.tpl.php

Code: Select all

Result
{$x} {$op} {$y} = {$result}
display_red.tpl.php

Code: Select all

<span style="color: red">Result
{$x} {$op} {$y} = {$result}</span>
It seems to me that the idea of a Class as a controller makes it a God class.
Eh, sort of, but I think that in the case of a controller, it's ok. Even though the class SEEMS to be doing a lot more than one task, when it really comes down to it, the class has ONE function, and that's to be a controller. :lol:
Post Reply