I think my fear of refactoring goes up exponentially with the number of functions and modules I'm dealing with. Usually, in my case, however, I'll write my first pass as One Big Long Linear Glob With Lots of Amoeboic Branches, so the fear doesn't show up until I feel the need to refactor a _second_ time. :) That fear induces a healthy sense of responsibility to refactor correctly the first time... ;-]

I think the most important thing to remember when refactoring is that you should always re-design to make your code fit the structure of the problem you are solving, not just to re-use code or shrink the size of the code. It's important NOT to mix refactoring and optimization, in my opinion! Doing that is a recipe for disaster. Current machines are fast enough that there's rarely a need to get tricky any more.

I spend some time thinking about structure, order, and flow in code, once I've seen my first pass run a few times. I deal a lot with embedded systems and system-level coding with sockets and daemons and data i/o, so once I've gotten it running, I know where my tight spots are going to be. I map out the subsystems and the transactions that are necessary, and examine my message protocols for completeness, robustness, and flexibility. Within each subsystem (or daemon, or object), there's also usually a set of states which can be identified, and rules for transitions. Once I've gotten to this level of detail, I look at all the places where I can replace code with data structures used repeatably. Be careful to do this in ways that make sense to someone looking at the big picture of your project, and name things appropriately, both the data bits and the code functions that read them.

Finally, and this is especially important with object-oriented programs, I distinguish between the 'initialization' and 'running' phases of the code. It's a natural tendency in OOP to create objects when you need them, but it's usually much smarter to build what you need once, prepare them completely beforehand, and then cut them loose. Doing this leads to much more predictable run times and far fewer surprises once the program is set free into The Real World. :D

Don Wilde
"There's more than one level to any answer."

In reply to Re: On the fear of re-factoring code by samizdat
in thread On the fear of re-factoring code by deorth

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.