Local database reading edition

Refactoring Technique : Extract Method

Reconstructed from database records, not a captured original forum layout. Dates and display names are retained as recorded; timezone and real-world identity are not inferred. Historical advice, contact details and offers are not current. Original markup is shown as inert text.

Forum index · Object Oriented Design

Opening post

Asha · 2001-02-27 21:15:29

Extract Method is my first choice of doing Refactoring. When I see a method that is too long or doing a sequence of operations that needs comment to understand the purpose.I then fragment the code into its own method. The benefits could be <ul> <li>It increases the chances that other methods can use a method when it is finely grained. </li> <li>When you see the higher level methods ,it does the logic like a sequenece of comments.</li> <li>Overriding is easier</li> <li> It improves clarity</li> </ul> for example : <pre id=code><font face=courier size=2 id=code> public void validateRequest(HttpServeletRequest req,...){ isValidMerchantId(...); isSecureKeyValid(...); isCertificateValid(...); ... } </font id=code></pre id=code> Instead of dumming everytthing in one method ,i can fragment the method like this.This increases the flexibility of updating the validation to different implementation logic. I could easily update something in isSecureKeyValid() without affecting the container method. We can say this as method level abstraction. The key for this refactoring technique is the semantic distance between the method name and the method body.If extracing improves clarity then we can go for this. In the above example validateRequest(...) method name is having quite a bit semantic distance from the validating a Certificate. validateRequest is more generic so it is good to extract the inner logic and give a good name for that. Like isCertificateValid(...); One more common place I use extract method is &quot;printing object state&quot;. This would be helpful to debug the object state when you invoke some operations on that.Write a method called displayAll() and print all the members of the object. call this method in any of the business methods(operations).e.g <pre id=code><font face=courier size=2 id=code> Class Account{ private PK acct_id; private Money balance; private long acct_no; public Money someBusinessMethod(...){ displayAll(); // logic here } public boolean moreBusinessMethod(){ displayAll(); //logic here } public void display(){ YourLogger.print(acct_id); YourLogger.print(balance.toString()); YourLogger.print(acct_no); } } </font id=code></pre id=code> Asha Balasubramanyan http://www.ammu.org Edited by - asha on February 27 2001 9:27:33 PM

Source notes

MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 53