Pages

Method Chaining


Method chaining is a very handy technique for invoking multiple methods at once. Not only does it improve the readability of the code, but it also reduces the amount of code needed when interacting with a class or an instance of a class.

Method Chaining is a programming style supported by many object-oriented languages. This style is more popular in Smalltalk than in Java and is considered by some people to be less readable and more difficult to debug than the more accepted Java style. However it is very convenient in most cases.

Example of Method Chaining

SessionFactory session = new Configuration();
              .addResource(""Message.hbm.xml)
              .setProperties(System.getProperties())
              .buildSessionFactory();



But if you do use this coding style, it is better to write each method invocation on a different line. Otherwise, it might be difficult to step through the code in your debuuger.

No comments:

Post a Comment