Command chainingEven if you're still stuck using a non-dynamic language (Java, C#, etc), you can steal a trick or two from the other side. Command chaining is an easy practice that can really simplify your code. It's easy. Put return this; before the end of each method in your class. Just say no to void! Of course, you wouldn't do it on functions. They're supposed to return data. But following this practice for 'command' methods makes perfect sense, and allows calling code to be much cleaner. Compare:
Element para = new Element("p"); para.set("class","song"); para.append("Hello World"); para.appendTo(document);to
new Element("p") .set("class","song") .append("Hello World") .appendTo(document);or
new Element("p") .set("class","song") .append("Hello World") .appendTo(document);#2 and #3 are far more readable. If you want to draw attention to the commands (say they aren't transparently obvious, or need inline comments), #3 is a better choice. If you want to focus on the surrounding logic, #2 cuts line usage down tremendously. Both remove repetition and reduce re-factoring costs. As a bonus, you don't have to clutter the scope with another arbitrarily named variable; you've created and finished with the object all in one statement. Like all coding practices, there is a short adjustment period while you get used to reading and writing this style; however, I'm confident you'll find it much faster to read and write code without that superfluous, repetitive text. DRY. Just say no to void. Be nice to your callers.