一位同事从未听说过这个词,我也无法给出一个确切的定义。对我来说,这一直是一个“我看到什么就知道什么”的例子。
附带问题,这个词是谁发明的?
一位同事从未听说过这个词,我也无法给出一个确切的定义。对我来说,这一直是一个“我看到什么就知道什么”的例子。
附带问题,这个词是谁发明的?
当前回答
样板代码是指可以反复使用的一段代码。另一方面,任何人都可以说它是一段可重用的代码。
这个词实际上来自钢铁行业。
对于一点历史,根据维基百科:
在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; } }
其他回答
从维基百科:
在计算机编程中,样板文件是用来描述必须包含在许多地方且很少或没有更改的代码段的术语。它更常用于被认为是冗长的语言,即程序员必须编写大量代码来完成最少的工作。
所以基本上,你可以把样板代码看作是一种编程语言所需要的文本,在你用这种语言编写的程序中经常用到。
现代语言正在尝试减少它,但旧的语言也有特定的类型检查器(例如OCaml有一个类型推断器,它允许您避免在像Java这样更冗长的语言中作为样板代码的太多声明)
它是可以被许多应用程序/上下文使用的代码,只需很少或没有更改。
样板纸(Boilerplate)起源于20世纪初的钢铁工业。
“样板代码”是任何看似重复的代码,它们一次又一次地出现,以获得一些看起来应该简单得多的结果。
这是一个主观的定义。
这个术语来源于报纸行业的“标准版”:wiki
简而言之,样板代码是需要包含在应用程序中的重复代码,程序/框架几乎没有改变,并且对应用程序的逻辑没有任何贡献。当您编写伪代码时,您可以删除样板代码。建议使用合适的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;}
}
}
Joshua Bloch有一个关于API设计的演讲,涵盖了糟糕的API如何使样板代码成为必要。(参考样板文件的第46分钟,今天听这个)