Method Chaining

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)

Webopedia Staff
Webopedia Staff
Since 1995, more than 100 tech experts and researchers have kept Webopedia’s definitions, articles, and study guides up to date. For more information on current editorial staff, please visit our About page.
Get the Free Newsletter
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.

Related Articles

Embedded Analytics

Embedded analytics brings self-service business intelligence to everyday application users.

HRIS

Human resources information system (HRIS) solutions help businesses manage multiple facets of their workforce operations. They provide a central platform for human resources professionals...

Complete List of Cybersecurity Acronyms

Cybersecurity news and best practices are full of acronyms and abbreviations. Without understanding what each one means, it's difficult to comprehend the significance of...

Human Resources Management System

A Human Resources Management System (HRMS) is a software application that supports many functions of a company's Human Resources department, including benefits administration, payroll,...

ScalaHosting

ScalaHosting is a leading managed hosting provider that offers secure, scalable, and affordable...

HRIS

Human resources information system (HRIS) solutions help businesses manage multiple facets of their...

Best Managed Service Providers...

In today's business world, managed services are more critical than ever. They can...