ArticleslgStudy

computer science

Method cascading

Method cascading is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Method cascading rather than just read about it. In short: In object-oriented programming, method cascading is syntax which allows multiple methods to be called on the same object. This is particularly applied in fluent interfaces.

Key takeaways

  • Method cascading belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Method cascading to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Method cascading from memory before moving on to harder problems.

Reference excerpt

In object-oriented programming, method cascading is syntax which allows multiple methods to be called on the same object. This is particularly applied in fluent interfaces. For example, in Dart, the cascade:

is equivalent to the individual calls:

Method cascading is much less common than method chaining – 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, below.

Application Cascading is syntactic sugar that eliminates the need to list the object repeatedly. This is particularly used in fluent interfaces, 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:

or using a temporary variable:

cascading allows the expression to be written once and used repeatedly:

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 evaluates this expression to the right object.

Chaining The following chain (in C++):

is equivalent to the simple form:

Cascading The following cascade (in Dart):

is equivalent to the simple form:

Cascading can be implemented in terms of chaining by having the methods return the target object (receiver, this, 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.

Smalltalk Method chains and cascades were both introduced in Smalltalk; 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:

Compare with separate statements, terminated with a period, also using a variable for abbreviation:

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 sending a yourself message that just returns the receiver. 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:

Note that yourself is not special in Smalltalk: rather yourself is just defined in the standard library, e.g. on class Object or somewhere similar:

This is in contrast to Smalltalk's self and super (and thisContext) which are defined within the language itself, and so can only be replaced or altered via a change to the compiler.

Visual Basic Visual Basic uses the With statement to enable an arbitrary number of method calls or property accesses on the same object:

With..End With blocks in Visual Basic can be nested:

Dart Among newer languages, Dart 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, 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:

is equivalent to:

Lisp and dialects The builtin doto macro can be used in Clojure to call multiple methods on the same object without binding it to a variable, which is mostly useful to operate on mutable objects from the host language.

Similar macros are available or can be easily defined in various other lisps like Common Lisp.

References

External links Dart "Method Cascades in Dart", Gilad Bracha, February 17, 2012 Milestone 1 Language Changes, "Cascades", Bob Nystrom, July 2012 (updated March 2013)

Worked examples

Example 1 — a first encounter with Method cascading

Start with the simplest possible case. Write down what Method cascading claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Method cascading before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Method cascading ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Method cascading

In research
Method cascading appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Method cascading in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Method cascading is common in secondary-school and first-year university syllabi. It links to neighbouring topics Method (computer programming), so understanding it makes those chapters shorter.
In everyday life
Look for Method cascading outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Method cascading” →

How to study Method cascading in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Method cascading means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Method cascading out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Method cascading in simple terms?

In object-oriented programming, method cascading is syntax which allows multiple methods to be called on the same object. This is particularly applied in fluent interfaces.

Why does Method cascading matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Method cascading?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Method cascading.

Tags

  • Method (computer programming)

Keep exploring