Boilerplate (text)

From Wikipedia, the free encyclopedia

Jump to: navigation, search

Boilerplate is any text that is or can be reused in new contexts or applications without being changed much from the original. Many computer programmers often use the term boilerplate code. A legal boilerplate is a standard provision in a contract.

The term dates back to the early 1900s, referring to the thick, tough steel sheets used to build steam boilers. From the 1890s onwards, printing plates of text for widespread reproduction such as advertisements or syndicated columns were cast or stamped in steel (instead of the much softer and less durable lead alloys used otherwise) ready for the printing press and distributed to newspapers around the United States. They came to be known as 'boilerplates'. Until the 1950s, thousands of newspapers received and used this kind of boilerplate from the nation's largest supplier, the Western Newspaper Union.

Some companies also sent out press releases as boilerplate so that they had to be printed as written. The modern equivalent is the press release boilerplate, or "boiler," a paragraph or two that describes the company and its products.

The word has also come into use for pre-created form letters on the Internet for things such as issues to be broached by a politician based on an issue ad, requesting a cable network be added to a system by a cable or satellite operator, or a pre-written complaint about something such as a program, book, or video game opposed by a group which created the letter, along with online petitions. Usually the greeting and the body of the letter have been pre-written, requiring the person requesting the action to only type or sign their name at the end.

Contents

[edit] Boilerplate language

The term boilerplate is adopted by lawyers to describe those parts of a contract that are considered "standard language", although it is good practice to always read the boilerplates in any contract.

[edit] Boilerplate code

In computer programming, boilerplate is the term used to describe sections of code that have to be included in many places with little or no alteration. It is more often used when referring to languages which are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs. The need for boilerplate can be reduced through high-level mechanisms such as Metaprogramming (which has the computer automatically write the needed boilerplate text) and Convention over Configuration (which provides good defaults values, reducing the need to specify program details in every project).

[edit] Preambles

One form of boilerplate consists of declarations which, while not part of the program logic or the language's essential syntax, are added to the start of a source file as a matter of custom. The following Perl example demonstrates boilerplate:

#!/usr/bin/perl
use warnings;
use strict;

The first line is a shebang, which identifies the file as a Perl script that can be executed directly on the command line. The other two are pragmas turning on warnings and strict mode, which are mandated by good Perl programming style.

Preambles often include imports, that is declarations of which libraries are being used. For example the following lines are very frequently included in C programs:

#include <stdio.h>
#include <stdlib.h>

This tells the compiler to include the files stdio.h and stdlib.h, which contain declarations of the types of commonly used library functions, making them available for use in the rest of the source file.

[edit] Dynamic allocation

A frequent source of boilerplate comes from the need for manual handling of dynamically allocated data structures. This is pervasive in low level languages such as C, which provide no automatic means of deallocating memory (such as garbage collection). For example, the following code defines a C function for appending two strings, then uses it twice to append three strings. (In C, strings are represented simply as pointers to arrays of characters terminated by NULL characters; these need to be allocated and deallocated manually.)

char *append(char *left, char *right)
{
    /* Allocate enough memory for the two strings, plus terminating NULL */
    char *result = malloc(strlen(left) + strlen(right) + 1);
    /* Copy the first string into the array */
    strcpy(result, first);
    /* Copy the second string onto the end */
    strcat(result, second);
    return result;
}
 
...
 
char *tmp = append(first, second);
char *composite = append(tmp, third);
free(tmp);

Here the boilerplate is the call to malloc() to allocate the new string, the temporary variable tmp, and the call to free(). While the first is abstracted away in the function call, the result of appending the first two strings needs to be deallocated after it is used, which means the program must keep track of it explicitly. If the language provides garbage collection, the latter part of the above code could be rewritten as follows, eliminating this boilerplate:

char *composite = append(first, append(second, third));

The result of the inner call is used in the outer call and then thrown away (it becomes "garbage"). The garbage collector "catches" this inner result and automatically deallocates it when (or after) the outer call returns. Without garbage collection, there is nothing to "catch" the inner result and it is never deallocated: a memory leak.

[edit] See also

[edit] External links

Personal tools
Languages