一位同事从未听说过这个词,我也无法给出一个确切的定义。对我来说,这一直是一个“我看到什么就知道什么”的例子。

附带问题,这个词是谁发明的?


“样板代码”是任何看似重复的代码,它们一次又一次地出现,以获得一些看起来应该简单得多的结果。

这是一个主观的定义。

这个术语来源于报纸行业的“标准版”:wiki


它是可以被许多应用程序/上下文使用的代码,只需很少或没有更改。

样板纸(Boilerplate)起源于20世纪初的钢铁工业。


从维基百科:

在计算机编程中,样板文件是用来描述必须包含在许多地方且很少或没有更改的代码段的术语。它更常用于被认为是冗长的语言,即程序员必须编写大量代码来完成最少的工作。

所以基本上,你可以把样板代码看作是一种编程语言所需要的文本,在你用这种语言编写的程序中经常用到。

现代语言正在尝试减少它,但旧的语言也有特定的类型检查器(例如OCaml有一个类型推断器,它允许您避免在像Java这样更冗长的语言中作为样板代码的太多声明)


Joshua Bloch有一个关于API设计的演讲,涵盖了糟糕的API如何使样板代码成为必要。(参考样板文件的第46分钟,今天听这个)


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).


在词源上,术语boilerplate: from http://www.takeourword.com/Issue009.html…

Interestingly, the term arose from the newspaper business. Columns and other pieces that were syndicated were sent out to subscribing newspapers in the form of a mat (i.e. a matrix). Once received, boiling lead was poured into this mat to create the plate used to print the piece, hence the name boilerplate. As the article printed on a boilerplate could not be altered, the term came to be used by attorneys to refer to the portions of a contract which did not change through repeated uses in different applications, and finally to language in general which did not change in any document that was used repeatedly for different occasions.

编程中的样板文件由什么组成?正如其他人可能已经指出的那样,它 只是一大块代码,一遍又一遍地复制,很少或 在这个过程中没有对它做任何改变。


您可以将其称为“片段”,或者更准确地称为“片段的集合”。 我认为这个术语是由新闻界和印刷业创造出来的,在那里他们使用实际的“板块”,然后再次将它们用作大块。 在现代互联网上,这是一个正在进行的(恕我冒犯)趋势的一部分,使用花哨的术语来描述简单的东西,以便看起来更时尚和复杂。参见响应性=适应性/流动性。


样板文件是优秀程序员所避免的:重复。


样板代码是指可以反复使用的一段代码。另一方面,任何人都可以说它是一段可重用的代码。

这个词实际上来自钢铁行业。

对于一点历史,根据维基百科:

在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; } }


来自whatis.techtarget.com:

In information technology, a boilerplate is a unit of writing that can be reused over and over without change. By extension, the idea is sometimes applied to reusable programming as in "boilerplate code." The term derives from steel manufacturing, where boilerplate is steel rolled into large plates for use in steam boilers. The implication is either that boilerplate writing has been time-tested and strong as "steel," or possibly that it has been rolled out into something strong enough for repeated reuse.

编程之外:

样板可以与特定类型的模板进行比较 被认为是一个填空样板。一些典型的 样板文件通常包括:任务陈述、安全警告 使用的安装程序、版权声明和责任 免责声明。

以我作为程序员的经验来看,合适的样板代码通常是一堆你开始的代码,这些代码不太大,也不太复杂,不能称为框架。

HTML5 Boilerplate就是一个典型的例子。


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.

同义词 蓝图是样板 模板是样板 页脚是一个样板 用于多种用途的设计模式是一个样板 邮件的签名是一份样板文件


软件开发中的样板对不同的人有不同的含义,但通常是指反复使用的代码块。

在MEAN栈开发中,这个术语指的是通过使用模板来生成代码。它比从头开始手动编码整个应用程序更容易,它提供了代码块的一致性和更少的错误,因为它是干净的,经过测试和验证的代码,而且它是开源的,所以它不断更新或修复,因此它在使用框架或代码生成器时节省了大量时间。有关MEAN stack的更多信息,请点击这里。


样板文件是一种写作单元,可以不作任何更改地反复使用。通过扩展,这种思想有时应用于可重用编程,如“样板代码”


简而言之,样板代码是需要包含在应用程序中的重复代码,程序/框架几乎没有改变,并且对应用程序的逻辑没有任何贡献。当您编写伪代码时,您可以删除样板代码。建议使用合适的Editor来生成样板代码。

在HTML中,接口中的样板代码。

<!DOCTYPE html>   
<html>   
   <head>   
      <title></title>   
   </head>   
   <body> </body>   
</html>

在c#语言中,属性的样板代码。

class Price
{   
  private string _price;   
  public string Price
  {   
     get {return _price;}   
     set {_price= value;}   
  }   
}  

重复的代码只会增加不必要的输入层来完成工作。

这些是必要的吗?

在大多数情况下,这些都是结构和装饰性的标记。它提供了代码、函数和语言的一致性。

因此,对于开发人员来说,它有点多余,因为我们已经见过它很多次了,但它确实向非开发人员和编译器传达了一些实际意义。