ConstructiCat Logo
CodeBust.
Browse section ▾

Boat Anchor.

A boat anchor is code or hardware kept in a system although it no longer serves any purpose, confusing maintainers and slowing builds.

In software engineering, a boat anchor (or boatanchor) is a piece of code — or, less often, hardware — that is kept in a system although it no longer serves any purpose. The name is a metaphor for something obsolete, useless and cumbersome, whose only productive use is to be thrown into the water as a boat mooring. The term was first applied to old, heavy radio and computer equipment; terms such as brick, doorstop and paperweight are similar.

In software

The term boat anchor has been extended to software code that is left in a system's codebase, typically in case it is needed later. This is an example of an anti-pattern and therefore can cause many problems for people attempting to maintain the program that contains the obsolete code. The key problem comes from the fact that programmers will have a hard time differentiating between obsolete code which doesn't do anything and working code which does.

For example, a programmer may be looking into a bug with the program's input handling system, so they search through the code looking for code that links into the input handling API. Obviously if the programmer comes across obsolete input handling code they may well start editing and debugging it, wasting valuable time before they realise that the code that they're working with is never executed and therefore not part of the problem they're trying to solve. Other problems include longer compile times and the risk that programmers may accidentally link working code into the defunct code, inadvertently resurrecting it.

A recommended solution for dealing with boat anchors in source code is to remove them from the code base and to place them in a separate location so that they can be referred to if necessary, but will not be compiled or be mistaken as "working" code. (For example, deleting them, knowing they are stored in the project's source control)

Example

Fax delivery was switched off years ago, but the gateway field and the method that used it are still in the class. Every reader has to work out that sendByFax is never called, and the class still drags in LegacyFaxGateway as a dependency.

class InvoiceService {
    // fax delivery was switched off in 2019
    private final LegacyFaxGateway faxGateway = new LegacyFaxGateway();

    String send(Invoice invoice) {
        return EmailSender.send(invoice);
    }

    // kept "just in case"; nothing calls it
    String sendByFax(Invoice invoice) {
        return faxGateway.transmit(invoice.toFaxPage());
    }
}

The unused code is deleted rather than commented out; if it is ever needed again, it can be recovered from version control (see also Dead Code).

class InvoiceService {
    String send(Invoice invoice) {
        return EmailSender.send(invoice);
    }
}

Adapted from the Wikipedia article “Boat anchor (metaphor)” (revision 1258854358), available under the CC BY-SA 4.0 license; the text has been edited and examples added.