In my experience, the question how do I combine two methods is almost always the wrong question.

What is the real benefit you are trying to achieve? A lot of very bright people have discovered that making something reusable is more than twice as hard as making a single-purpose version. That's why many people don't worry about generalizing without 3 examples.

If you are going to need several more of these kinds of methods, then maybe, you have a good reason for generalizing. Even then, how do I combine is not my first question.

I normally prefer questions like:

The second question is the easiest. The third is a variation of the second. But, the first is usually the most powerful (for me).

I begin by breaking out one of these pieces into a new subroutine. I build testing code for it, to make certain it works the way I want it to. Then I modify the routines to use this chunk. In the process of building this new routine, I often find that parameter names and my variable names become more generic to fit with the utility nature of the method.

Then, I replace the code in one of the original routines with a call to the new method, running our tests to verify I haven't broken anything. (You do have tests for these methods, right? If not, you need those before you start any kind of refactoring effort.) Then I can replace functionality in the next routine, and so on.

Then, I repeat with the next chunk of code I want to break out of these methods. I apply this algorithm repeatedly until the usage of the routines becomes obvious or There's nothing more to move.

Sometimes, I will now leave the original routines in place as a simple template for running the newly refactored code. Sometimes, I will remove the old methods and replace the places they were called with calls to the new code.

I often find that just giving names to the internal functionality causes it to be reused elsewhere. This is a benefit above and beyond the desire to combine two methods. I can also build new variations of these methods by combining the common code with whatever unique data or pre/post-processing is needed.

I know this isn't a direct answer to your question, but, in my experience, it's more likely to get you to better code in the end.

G. Wade

In reply to Re: how can I combine into one method by gwadej
in thread how can I combine into one method by lomSpace

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.