Page 1 of 2

is less more or is more more?

Posted: Tue Apr 17, 2007 2:53 pm
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?

Posted: Tue Apr 17, 2007 2:57 pm
by Jenk
small, concise objects and methods.

Posted: Tue Apr 17, 2007 3:13 pm
by patrikG
less is more. Always. Quality over quantity, dark over milk chocolate. Small, concise methods trump god-classes in every way.

Posted: Tue Apr 17, 2007 3:18 pm
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.

Posted: Tue Apr 17, 2007 3:49 pm
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?

Posted: Tue Apr 17, 2007 3:52 pm
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.

Posted: Tue Apr 17, 2007 4:50 pm
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

Posted: Tue Apr 17, 2007 5:00 pm
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.

Posted: Tue Apr 17, 2007 7:32 pm
by RobertGonzalez
Ninja, that was an excellently worded explanation of reusability in code.

Posted: Tue Apr 17, 2007 8:44 pm
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!

Posted: Tue Apr 17, 2007 8:54 pm
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. :)

Posted: Tue Apr 17, 2007 9:02 pm
by Kieran Huggins
patrikG wrote:...dark over milk chocolate...
8O

I have found my nemesis!

Posted: Fri Apr 20, 2007 11:59 am
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.

Posted: Fri Apr 20, 2007 3:05 pm
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

Posted: Fri Apr 20, 2007 3:23 pm
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: