Index

Overview


Method cascading is a great way to simplify code.

Java has good support for it, but it seems under-used in Java code.

Let's take a peek.

Here is an example of our "regular" code approach.
private void runDemo() throws IOException  {
	Report farmCommodityReport = new Report();
	farmCommodityReport.showLine("Farm Report");
	farmCommodityReport.newline();
	farmCommodityReport.showLine("Commodities:");
	farmCommodityReport.newline();
	farmCommodityReport.showLines(commodityNames());
	farmCommodityReport.newline();
	farmCommodityReport.showLine("Trends:");
	farmCommodityReport.newline();
	farmCommodityReport.showLine("Corn up $0.05");
	farmCommodityReport.showLine("Soybeans down -$0.20");
	farmCommodityReport.showLine("Wheat unchanged");
	farmCommodityReport.writeAndView("FarmReport.txt");
}
Here the code is doing the same thing
as the previous example.

But we use method cascading to lighten up our code.
private void runDemoWithMethodCascading() throws IOException  {
	Report farmCommodityReport = new Report();
	farmCommodityReport
		.showLine("Farm Report")
		.newline()
		.showLine("Commodities:")
		.newline()
		.showLines(commodityNames())
		.newline()
		.showLine("Trends:")
		.newline()
		.showLine("Corn up $0.05")
		.showLine("Soybeans down -$0.20")
		.showLine("Wheat unchanged")
		.writeAndView("FarmReport.txt");
}

Side-by-Side Comparison


Left: Regular code
Right: Code with method cascading
private void runDemo() throws IOException  {
	Report farmCommodityReport = new Report();
	farmCommodityReport.showLine("Farm Report");
	farmCommodityReport.newline();
	farmCommodityReport.showLine("Commodities:");
	farmCommodityReport.newline();
	farmCommodityReport.showLines(commodityNames());
	farmCommodityReport.newline();
	farmCommodityReport.showLine("Trends:");
	farmCommodityReport.newline();
	farmCommodityReport.showLine("Corn up $0.05");
	farmCommodityReport.showLine("Soybeans down -$0.20");
	farmCommodityReport.showLine("Wheat unchanged");
	farmCommodityReport.writeAndView("FarmReport.txt");
}
private void runDemoWithMethodCascading() throws IOException  {
	Report farmCommodityReport = new Report();
	farmCommodityReport
		.showLine("Farm Report")
		.newline()
		.showLine("Commodities:")
		.newline()
		.showLines(commodityNames())
		.newline()
		.showLine("Trends:")
		.newline()
		.showLine("Corn up $0.05")
		.showLine("Soybeans down -$0.20")
		.showLine("Wheat unchanged")
		.writeAndView("FarmReport.txt");
}


Code to Support Method Cascading


Returning 'this'
This is the most common way to support support cascading.

Just two steps:

  • Set the return type to the class you are coding in
  • Return "this"

Three examples (methods) are shown that support support cascading. In each of these three we do the two steps.
public class Report {
	private String contents;

	public Report show(Object o)  {
		basicShow(o);
		return this;
	}

	public Report showLine(Object o)  {
		basicShow(o);
		newline();
		return this;
	}

	public Report showLines(List<?> lines)  {
		for (Object each: lines)
			showLine(each);
		return this;
	}

Returning new Object of Correct Type
This is another approach to support method cascading. We construct a new object which is the same type as "this" object (i.e. type "Report").

Just two steps:

  • Set the return type to the class you are coding in
  • Construct a new object of that type.
Report Class

public class Report {
	private String contents;

	public 
1
Report copyReport()
{ return
2
new Report(this); }



Example Code (ZIP File)


The code for the examples is here...