0

I have 34 different methods that take 1 parameter of my ViewModel class, but each of them only uses two or three properies on the class. I think I'd rather just pass those properties to these methods instead of the entire object. Is that a way to easily refactor that? I tried to just select all the text inside my function and do Refactor > Extract Method hoping it would be smart enough to see that only 2 properties of the object were being used and make the parameter list just those 2 properties, but it just wanted to take the whole object again.

Maybe I need to explain what I'm doing...

I'm trying to write an application that will generate text for a wiki page. Based on the values entered into the UI it will either generate text for a template and text for using that template, or it will just generate the text for the page if a template isn't necessary. There are 17 different parts to it so there are 17 functions that, when combined, create the text of the template page, and there are 17 functions that combine to make the actual page if a template isn't needed. I did it this way so I can test smaller pieces of it at a time instead of testing the entire output of one massive function.

So currently all 34 of those methods take 1 parameter, the whole object that's built from the UI. To me that doesn't make sense any more. A function that generates 1 section of the entire page will never need to know about the data that's used in a different section, and if anyone does try to use it there then I know something's wrong.

6
  • 2
    Why do you want to do this? It's typically considered good practice to send the entire object. Then, if a method needs one more property of the object in the future, there's nothing to change in the call -- the method already has what it needs. And it's cleaner to send one object.
    – Bob Horn
    Sep 22, 2012 at 19:31
  • Bob, voted your comment up as being similar to mine. ;) Sep 22, 2012 at 19:32
  • @JohnLockwood Ha, and I upvoted your answer. I actually hope that there isn't a way to accomplish this. :)
    – Bob Horn
    Sep 22, 2012 at 19:34
  • 1
    @Nick Having 34 methods taking the same parameter from a ViewModel smells like a bad design. I think you should better sit and refactor the design of.
    – Erre Efe
    Sep 22, 2012 at 19:38
  • 1
    I'm with Bob and John, about the only refactor I'd consider would be passing an interface instead of a concrete class. There are all sorts of nice patterns you can make use of if you leave it as it is, that will not be possible if you go to discrete primitive arguments as well. Sep 22, 2012 at 20:00

2 Answers 2

7

Is there a reason not to pass the object? Objects are passed by reference and it's quite efficient, plus if your implementation needs more properties, it's easy to modify the function than to maintain the function declaration and go after all the callers. I'd be inclined to leave it alone.

But to answer your specific question, I don't know of a way to refactor it in Visual Studio. That's another part of why I'd be inclined to leave it alone if it's working. :)

1
  • I updated the question. Currently all 34 of those methods take 1 parameter: the whole object that's built from the UI. To me that doesn't make sense any more. A function that generates 1 section of the entire page will never need to know about the data that's used in a different section, and if anyone does try to use it there then I know something's wrong.
    – Nick
    Sep 22, 2012 at 20:15
0

Maybe Resharper could help you as it provides a lot of different refactoring functionalities. You could try to download a trial which works for 30 days. Unfortunately currently there is no EAP program open as they're releasing v7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.