Is it bad practice to make a setter return “this”?

It’s not bad practice. It’s an increasingly common practice. Most languages don’t require you to deal with the returned object if you don’t want to so it doesn’t change “normal” setter usage syntax but allows you to chain setters together.

This is commonly called a builder pattern or a fluent interface.

It’s also common in the Java API:

String s = new StringBuilder().append("testing ").append(1)
  .append(" 2 ").append(3).toString();

Leave a Comment