views:

187

answers:

8

Hey everyone,

I am writing some PHP code to create PDFs using the FPDF library. And I basically use the same 4 lines of code to print every line of the document. I was wondering which is more efficient, repeating these 4 lines over and over, or would making it into a function be better? I'm curious because it feels like a function would have a larger overhead becuse the function would only be 4 lines long.

The code I am questioning looks like this:

$pdf->checkIfPageBreakNeeded($lineheight * 2, true);
$text = ' label';
$pdf->MultiCell(0, $lineheight, $text, 1, 'L', 1);
$text = $valueFromForm;
$pdf->MultiCell(0, $lineheight, $text, 1, 'L');
$pdf->Ln();
+2  A: 

A function is certainly preferable, especially if you have to go back later to make a change.

dave
+15  A: 

This should answer it: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself and http://www.codinghorror.com/blog/2007/03/curlys-law-do-one-thing.html

Curly's Law, Do One Thing, is reflected in several core principles of modern software development:

  • Don't Repeat Yourself

    If you have more than one way to express the same thing, at some point the two or three different representations will most likely fall out of step with each other. Even if they don't, you're guaranteeing yourself the headache of maintaining them in parallel whenever a change occurs. And change will occur. Don't repeat yourself is important if you want flexible and maintainable software.

  • Once and Only Once

    Each and every declaration of behavior should occur once, and only once. This is one of the main goals, if not the main goal, when refactoring code. The design goal is to eliminate duplicated declarations of behavior, typically by merging them or replacing multiple similar implementations with a unifying abstraction.

  • Single Point of Truth

    Repetition leads to inconsistency and code that is subtly broken, because you changed only some repetitions when you needed to change all of them. Often, it also means that you haven't properly thought through the organization of your code. Any time you see duplicate code, that's a danger sign. Complexity is a cost; don't pay it twice.

Christian Smorra
+11  A: 

Rather than asking yourself which is more efficient you should instead ask yourself which is more maintainable.

Writing a function is far more maintainable.

evolve
+8  A: 

I'm curious because it feels like a function would have a larger overhead becuse the function would only be 4 lines long.

This is where spaghetti comes from.

Defininely encapsulate it into a function and call it. The overhead that you fear is the worst kind of premature optimization.

DRY - Don't Repeat Yourself.

duffymo
+2  A: 

Don't worry about overhead; worry about yourself, a year in the future, trying to debug this.

In the light of the above, Don't Repeat Yourself and make a tiny function.

Piskvor
+3  A: 

Make it a function. Function call overhead is pretty small these days. In general you'll be able to save far more time by finding better high-level algorithms than fiddling with such low-level details. And making and keeping it correct is far easier with such a function. For what shall it profit a man, if he shall gain a little speed, and lose his program's correctness?

Paul E.
+1 for the paraphrase, v.appropriate :)
Piskvor
A: 

In addition to all the valuable answers about the far more important topic of maintainability; I'd like to add a little something on the question of overhead.

I don't understand why you fear that a four line function would have a greater overhead.

  1. In a compiled language, a good compiler would probably be able to inline it anyway, if appropriate.

  2. In an interpreted language (such as PHP) the interpreter has to parse all of this repeated code each time it is encountered, at runtime. To me, that suggests that repetition might carry an even greater overhead than a function call.

  3. Worrying about function call overhead here is ghastly premature optimisation. In matters like this, the only way to really know which is faster, is to profile it.

Make it work, make it right, make it fast. In that order.

Paul Butcher
A: 

The overhead is actually very small and wont be causing a big difference in your application. Would u rather these small overhead, but have a easier program to maintain, or u want to save the mere millisecond but take hours to correct small changes which are repeated.

If you ask me or other developer out there, we definitely want the 1st option. So go on with the function. U may not be maintaining the code today, but when u do, u will hate yourself for trying to save that mere milliseconds

C_Rance