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?
Moderator: General Moderators
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>";
}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;
}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;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.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.
Fixed it for you.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
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>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>Code: Select all
<h1>Result</h1>
<p><strong>{$x} {$op} {$y} = {$result}</strong></p>Code: Select all
Result
{$x} {$op} {$y} = {$result}Code: Select all
<span style="color: red">Result
{$x} {$op} {$y} = {$result}</span>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.It seems to me that the idea of a Class as a controller makes it a God class.