Chapter 13. Composed Method and SLAP

SLAP stands for the Single Level of Abstraction Principle. While the concept comes from Kent Beck’s Smalltalk Best Practice Patterns (Prentice Hall), my friend Glenn Vanderburg is the one who captured its essence with this great acronym.
Before I talk about SLAP, though, I must talk about the composed method pattern discussed in Beck’s book. Composed method mandates that all public methods read like an outline of the steps to perform. The actual steps are implemented as private methods. Composed method is a way of factoring your code to keep it cohesive and make it easier to spot candidates for code reuse. The best way to understand composed method is to see it in action.
Composed Method in Action
Composed method encourages factoring (or refactoring) code into small, cohesive, readable chunks. For projects on which I’m the tech lead, our rule of thumb is to allow no method to exceed 15 lines of code in Java or C#. For dynamic languages such as Groovy or Ruby, the rule is five lines of code.
What benefit does this provide? Consider the following noncomposed method code, from a small e-commerce site:
publicvoidpopulate()throwsException { Connection c =null;try{ Class.forName(DRIVER_CLASS); c = DriverManager.getConnection(DB_URL, USER, PASSWORD); Statement stmt = c.createStatement(); ResultSet rs = stmt.executeQuery(SQL_SELECT_PARTS);while(rs.next()) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access