Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

design issue refactoring the code.Please suggest.

 
aruna sydu
Ranch Hand
Posts: 98
MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,


I am supposed to refactor a Java class containing 1000 lines of of code in a single Method.
I know its a bad Design to hold 1000 line in a single Method.So please help me out figuring How well to refactor!
Comming to the responsibilities of the Method.

1.There is an Object HugeObject which contains 21 Objects of type java.util.Set and 20 variables of type string all with its Getter and Setter
2.The method Retrives each of the Set Object from the HugeObject.
3.Calls the Helper class to find whether Reconilation logic applies the Objects
4.If reconilation
logic to Reconile the HugeObject
Else.
5.Calls Helper class to Verify the contents of the immediate Objects of HugeObject.


This is the sample code for 1 child object.

6.The Process repeats for the all the Objects.

The Method is holding 2 responsibilities
1.Calling the Helper class to verification
2.Reconcilation of the Object.


Please suggest me a right way to refactory the above.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aruna,
Moving to our Refactoring forum since this is not about the SCEA. If you meant to post in a different certification forum, just reply with which one.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aruna,
The first thing you can do is look for any duplicated logic. You said the process repeats for each child object. Is the code the same? If so, you can put it in a method and call it 21 times. If not, are any snippets of it the same.
 
aruna sydu
Ranch Hand
Posts: 98
MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne Boyarsky,

Thanks for moving my post to this Forum.Actually not every sure where to Post.
All the 21 Objects are different ,So should I Hold 21 methods.There is a common Logic in the 21 Objects ,so I can move it to a different method and call from the 21 Objects code.
Thanks for pointing the duplicated logic point.
Can you please point out any other things i need to consider while refactoring.?


Thanks,
Aruna.
 
Imran Perwez
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you have a bit of a monster here.

Michael Feathers book "Working Effectively with Legacy Code" has a chapter dedicated to working with large methods. You might want to see if you can get a copy of this.

The basic idea is to apply safe refactorings in the first instance, ones that you know wont break the code. Then get the code under test (you dont want to be breaking stuff as you work). Once, you have the tests in place you can start breaking down the dependencies further and encapsulating bits of common logic as you go along...

You want to look for opportunities to apply design patterns to remove duplicate code. E.g. Template Method.
 
aruna sydu
Ranch Hand
Posts: 98
MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Imran Perwez,

Thanku for the new insight You provide me.That would help,However I do not hold Michael Feathers book "Working Effectively with Legacy Code" would try to get one!!

Any new ideas on how to approach this kind of gaint methods would help.


Regards,
Aruna.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic