In
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
, method cascading is
syntax
In linguistics, syntax () is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure ( constituency) ...
which allows multiple
methods
Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to:
*Scien ...
to be called on the same object. This is particularly applied in
fluent interface
In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric Ev ...
s.
For example, in
Dart
Dart or DART may refer to:
* Dart, the equipment in the game of darts
Arts, entertainment and media
* Dart (comics), an Image Comics superhero
* Dart, a character from ''G.I. Joe''
* Dart, a ''Thomas & Friends'' railway engine character
* Dar ...
, the cascade:
a..b()
..c();
is equivalent to the individual calls:
a.b();
a.c();
Method cascading is much less common than
method chaining
Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement ...
– it is found only in a handful of object-oriented languages, while chaining is very common. A form of cascading can be implemented using chaining, but this restricts the interface; see
comparison with method chaining
Comparison or comparing is the act of evaluating two or more things by determining the relevant, comparable characteristics of each thing, and then determining which characteristics of each are similar to the other, which are different, and t ...
, below.
Application
Cascading is
syntactic sugar
In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...
that eliminates the need to list the object repeatedly. This is particularly used in
fluent interface
In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric Ev ...
s, which feature many method calls on a single object.
This is particularly useful if the object is the value of a lengthy expression, as it eliminates the need to either list the expression repeatedly or use a temporary variable. For example, instead of either listing an expression repeatedly:
a.b().c();
a.b().d();
or using a temporary variable:
n = a.b();
n.c();
n.d();
cascading allows the expression to be written once and used repeatedly:
a.b()..c()
..d();
Comparison with method chaining
Given a method call
a.b()
, after executing the call, method cascading evaluates this expression to the ''left'' object
a
(with its new value, if mutated), while
method chaining
Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement ...
evaluates this expression to the ''right'' object.
;Chaining
The following chain (in C++):
a.b().c();
is equivalent to the simple form:
b = a.b();
b.c();
;Cascading
The following cascade (in Dart):
a..b()
..c();
is equivalent to the simple form:
a.b();
a.c();
Cascading can be implemented in terms of chaining by having the methods return the target object (receiver,
this
This may refer to:
* ''This'', the singular proximal demonstrative pronoun
Places
* This, or ''Thinis'', an ancient city in Upper Egypt
* This, Ardennes, a commune in France
People with the surname
* Hervé This, French culinary chemist Arts, e ...
,
self
). However, this requires that the method be implemented this way already – or the original object be wrapped in another object that does this – and that the method not return some other, potentially useful value (or nothing if that would be more appropriate, as in setters). In fluent interfaces this often means that setters return ''this'' instead of nothing.
Languages
Pascal
Within the component statement of the with statement, the components (fields) of the record variable specified by the with clause can be denoted by their field identifier only, i.e. without preceding them with the denotation of the entire record variable. The with clause effectively opens the scope containing the field identifiers of the specified record variable, so that the field identifiers may occur as variable identifiers.
with date do
if month = 12 then
begin month := 1; year := year + 1 end
else month := month + 1
if date.month = 12 then
begin date.month := 1; date.year := date.year + 1 end
else date.month := date.month + 1
Smalltalk
Method chains and cascades were both introduced in
Smalltalk
Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Ka ...
; most subsequent object-oriented languages have implemented chains, but few have implemented cascades. In Smalltalk the semicolon operator can be used to send different messages to the same object:
self listPane parent
color: Color black;
height: 17;
width: 11
Compare with separate statements, terminated with a period, also using a variable for abbreviation:
, parent,
parent := self listPane parent.
parent color: Color black.
parent height: 17.
parent width: 11.
One subtlety is that the value of a method call ("message") in a cascade is still the ordinary value of the message, ''not'' the receiver. This is a problem when you do want the value of the receiver, for example when building up a complex value. This can be worked around by using the special
yourself
method that simply returns the receiver:
Object>>yourself
^self
For example, the "add an object to a collection" method (
Collection>>add: anObject
) returns the object that was added, not the collection. Thus to use this in a cascade in an assignment statement, the cascade must end with
yourself
, otherwise the value will just be the last element added, not the collection itself:
all := OrderedCollection new
add: 5;
add: 7;
yourself.
Visual Basic
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to:
* Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET
* Visual Basic (cl ...
uses th
With
statementto enable an arbitrary number of method calls or property accesses on the same object:
With ExpressionThatReturnsAnObject
.SomeFunction(42)
.Property = value
End With
With..End With
blocks in Visual Basic can be nested:
With ExpressionThatReturnsAnObject
.SomeFunction(42)
.Property = value
With .SubObject
.SubProperty = otherValue
.AnotherMethod(42)
End With
End With
Dart
Among newer languages,
Dart
Dart or DART may refer to:
* Dart, the equipment in the game of darts
Arts, entertainment and media
* Dart (comics), an Image Comics superhero
* Dart, a character from ''G.I. Joe''
* Dart, a ''Thomas & Friends'' railway engine character
* Dar ...
implements cascades, using a double-dot
..
"cascaded method invocation operation". Unlike Smalltalk, in Dart the value of a cascaded method invocation is the receiver (base object), not the value of the (uncascaded) method invocation, and thus there is no need for
yourself
. Dart uses
properties
Property is the ownership of land, resources, improvements or other tangible objects, or intellectual property.
Property may also refer to:
Mathematics
* Property (mathematics)
Philosophy and science
* Property (philosophy), in philosophy and ...
, and thus rather than using method syntax for getters and setters (
foo.getBar(); foo.setBar(b);
), it uses field value/assignment syntax (
foo.bar; foo.bar = b;
), and cascades work with assignments:
a..string = 'Hello world!'
..done = true;
is equivalent to:
a.string = 'Hello world!';
a.done = true;
References
*
{{refend
External links
;Dart
*
Method Cascades in Dart, Gilad Bracha, February 17, 2012
*
Milestone 1 Language Changes''
Cascades, Bob Nystrom, July 2012 (updated March 2013)
Cascading