函数与重复代码 [英] functions vs repeated code

查看:83
本文介绍了函数与重复代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些PHP代码来使用FPDF库创建PDF。我基本上使用相同的4行代码来打印文档的每一行。我想知道哪个更有效率,反复重复这4条线,还是会让它变得更好?我很好奇,因为它感觉像一个函数会有更大的开销,因为函数只有4行。




我质疑的代码如下所示:

  $ 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();


解决方案

这应该回答它:
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

http://www.codinghorror。 com / blog / 2007/03 / curlys-law-do-one-thing.html


卷曲定律事情是,
反映在现代软件开发的几个核心原则
中:


  • Don '重复你自己



    如果你有多种表达同一事物的方式,在某点
    ,两个或三个不同的
    表示最有可能会相互跌落
    。即使
    他们没有,你保证
    自己保持
    并行的头痛,只要发生
    的变化。并且会发生变化。如果您的
    需要灵活且可维护的
    软件,请勿重复购买

  • 只有一次



    每个行为声明都应该出现一次,并且只发生一次
    。当重构
    代码时,这是主要目标之一,即使不是主要目标,也是
    。设计目标是消除
    重复的行为声明,
    通常通过合并它们或者用
    a统一抽象来替换
    多个类似的实现。


  • 单点真相

    重复会导致不一致,并且代码微妙地
    被破坏,因为当您需要更改
    时,您只更改了一些
    的重复项。通常,这也意味着
    您没有正确地想通过
    组织您的代码。任何
    的时间你都会看到重复的代码,这是一个
    的危险信号。复杂性是成本;
    不支付两次。




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();

解决方案

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.

这篇关于函数与重复代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆