it happens that there are sometimes very complex changes you want to make to your code. these might involve changes to dozens of existing classes, and to the heirs of some of those, too.
how do we approach this kind of situation?
recall that managing mental scope is the very heart of programming well. but any such change is sure to blow all hope of narrow mental scope right out of the water.
we can only manage mental scope if we apply our change in steps, where each step is itself of small enough scope to be manageable.
to make this work, we need to keep our program running and running green the entire time we’re applying steps. think of “running green” as the definition of a step boundary. if we’re running green, we’re between steps. if we’re not, we’re inside one. we want to convert our huge change to a series of small green-to-green steps.
formal refactoring is definitionally green-to-green. refactoring is changing the design w/o changing the function. so any of the standard formal refactorings we need to apply just automatically qualify.
in addition, any actual re-writing we need in there has to meet the same criteria.
here’s what i do. in fact, i’m doing it as we speak, or stalling artfully in any case.
check out head. randomly go nuts making your crazy complex change. DO NOT COMMIT OR PUSH. press on for a while.
what you’re going to see is what we call “blood in the gutters”. that’s an IDE-user’s term. it means all the red-bar lining the code screen. if you don’t use an IDE – you ought to – think of it as one of those 10,000 error outputs one gets when one breaks the entire free world. but a great many of those errors are repeat errors. there’s a pattern. find that pattern and you’ve discovered a step.
an easy example: the complex change will mean that all subclasses of X must now implement a method differently than they did before. when you boldly re-derive, you get blood in the gutters from the fact that none of those classes does it.
you just found a step, a bunch of them, in fact.
the meta-step: make all soon-to-be-X classes implement the new method. individual steps, make class 1 implement new method, and so on.
you can press on for a while, usually, discovering these step dependencies. make post-its of them.
if you’re a noob, the hardest thing in the world is throwing away the code you just typed. if you’re a pro, you do it every day. do it.
now you have some or all of the precursor steps on your post-its. you’ve empirically determined how to make your big change small. this backwards-working seems hard at first. but the thing that makes it hard is over-valuing the text you typed and undervaluing your mind.
i type fast. it’s the 21st century. all geeks type fast. as we’ve said over and again: typing is not the bottleneck.
the change i’m making today is huge. it will involve lots and lots of differences. the commit would be many hundreds of lines of code. but i did my thing of pushing the huge change for a while, noting the small changes it requires. and now i’ve got some of what i need.
note that if a change is big enough, pushing it like this won’t get you all of what you need, only part. that’s okay. since whatever steps i discover this way are green-to-green, i’m perfectly happy that they only take me part of the way. once they’re in and committed, i’ll repeat, trying the now-smaller huge change again, and seeing what steps fall out of that effort.
eventually, the huge change remainder will become a small change. and i make it, and we’re done.
notice: because i am insisting on green-to-green steps, i really CAN DO NO WRONG.
any green-to-green code change is “okay”. it might not be right, it might not be best, it might not be needed. but it’s okay. when you’re still skill-building, your version of “huge” will be smaller than when you’re a master. but the approach still works. try it.