一位同事从未听说过这个词,我也无法给出一个确切的定义。对我来说,这一直是一个“我看到什么就知道什么”的例子。
附带问题,这个词是谁发明的?
一位同事从未听说过这个词,我也无法给出一个确切的定义。对我来说,这一直是一个“我看到什么就知道什么”的例子。
附带问题,这个词是谁发明的?
当前回答
样板代码是指可以反复使用的一段代码。另一方面,任何人都可以说它是一段可重用的代码。
这个词实际上来自钢铁行业。
对于一点历史,根据维基百科:
在19世纪90年代,样板版实际上是铸造或冲压成金属,准备印刷,并分发给美国各地的报纸。直到20世纪50年代,成千上万的报纸从美国最大的供应商西部报业联盟那里收到并使用了这种样板文件。有些公司还以样板文件的形式发布新闻稿,这样它们就必须按书面形式打印出来。
根据维基百科:
In object-oriented programs, classes are often provided with methods for getting and setting instance variables. The definitions of these methods can frequently be regarded as boilerplate. Although the code will vary from one class to another, it is sufficiently stereotypical in structure that it would be better generated automatically than written by hand. For example, in the following Java class representing a pet, almost all the code is boilerplate except for the declarations of Pet, name and owner: public class Pet { private PetName name; private Person owner; public Pet(PetName name, Person owner) { this.name = name; this.owner = owner; } public PetName getName() { return name; } public void setName(PetName name) { this.name = name; } public Person getOwner() { return owner; } public void setOwner(Person owner) { this.owner = owner; } }
其他回答
Boilerplate definition is becoming more global in many other programming languages nowadays. It comes from OOP and hybrid languages that have become OOP and were before procedual have now the same goal to keep repeating the code you build with a model/template/class/object hence why they adapt this term. You make a template and the only things you do for each instance of a template are the parameters to individualize an object this part is what we call boilerplate. You simply re-use the code you made a template of, just with different parameters.
同义词 蓝图是样板 模板是样板 页脚是一个样板 用于多种用途的设计模式是一个样板 邮件的签名是一份样板文件
In practical terms, boilerplate code is the stuff you cut-n-paste all over the place. Often it'll be things like a module header, plus some standard/required declarations (every module must declare a logger, every module must declare variables for its name and revision, etc.) On my current project, we're writing message handlers and they all have the same structure (read a message, validate it, process it) and to eliminate dependencies among the handlers we didn't want to have them all inherit from a base class, so we came up with a boilerplate skeleton. It declared all the routine variables, the standard methods, exception handling framework — all a developer had to do was add the code specific to the message being handled. It would have been quick & easy to use, but then we found out we were getting our message definitions in a spreadsheet (which used a boilerplate format), so we wound up just writing a code generator to emit 90% of the code (including the unit tests).
Joshua Bloch有一个关于API设计的演讲,涵盖了糟糕的API如何使样板代码成为必要。(参考样板文件的第46分钟,今天听这个)
从维基百科:
在计算机编程中,样板文件是用来描述必须包含在许多地方且很少或没有更改的代码段的术语。它更常用于被认为是冗长的语言,即程序员必须编写大量代码来完成最少的工作。
所以基本上,你可以把样板代码看作是一种编程语言所需要的文本,在你用这种语言编写的程序中经常用到。
现代语言正在尝试减少它,但旧的语言也有特定的类型检查器(例如OCaml有一个类型推断器,它允许您避免在像Java这样更冗长的语言中作为样板代码的太多声明)
您可以将其称为“片段”,或者更准确地称为“片段的集合”。 我认为这个术语是由新闻界和印刷业创造出来的,在那里他们使用实际的“板块”,然后再次将它们用作大块。 在现代互联网上,这是一个正在进行的(恕我冒犯)趋势的一部分,使用花哨的术语来描述简单的东西,以便看起来更时尚和复杂。参见响应性=适应性/流动性。