Method Chaining Definition & Meaning

Method chaining is the practice of calling different methods in a single line in object-oriented programming languages. Each method returns an object, meaning the calls can be chained together in a single line without requiring variables to store the results. Method chaining is used to eliminate an extra variable for each intermediate step, making it a simpler process and avoiding repetition. The developer no longer has to name the variable and keep it in mind. 

Method chaining can be used as a convenient way to build value types, especially for a builder pattern. It can be used to implement fluent interfaces, which is an object-oriented API that extensively relies on method chaining. Fluent interfaces provide developers with a mechanism for calling methods on classes that are easily readable and can be used to build small domain-specific languages. It also allows for operation on the object through methods without breaking the flow over and over by repeating the object. 

A drawback of using method chaining is that it takes away the ability to return any other value from methods since it must return the object the method was invoked on. 

Examples of method chaining 

A popular and simple example is iostream (Standard Input/Output Stream) in the high-level programming language C++. <<  returns the left objects, which allows for chaining. 

What started as:

a << b << c;

is now:  

a << b;

a << c;

In JavaScript, the built-in methods of Array are used: 

somethings

  .filter(x => x.count > 10)

  .sort((a, b) => a.count – b.count)

  .map(x => x.name)

Top Articles

The Complete List of Text Abbreviations & Acronyms

From A3 to ZZZ we list 1,559 text message and online chat abbreviations to help you translate and understand today's texting lingo. Includes Top...

How to Create a Website Shortcut on Your Desktop

This Webopedia guide will show you how to create a desktop shortcut to a website using Firefox, Chrome or Internet Explorer (IE). Creating a desktop...

Windows Operating System History & Versions

The Windows operating system (Windows OS) refers to a family of operating systems developed by Microsoft Corporation. We look at the history of Windows...

Hotmail [Outlook] Email Accounts

By Vangie Beal Hotmail was one of the first public webmail services that could be accessed from any web browser. Since 2011, Hotmail, in terms...

IT Observability Definition &...

IT observability is the theory that a system's internal state should be understood...

Data Corruption Definition &...

Data corruption is the process of data becoming unreadable or invalid. It typically...

Subschema Definition & Meaning

A subschema is a database view that filters or organizes all data to...