Most important bits of refactoring complete!

August 29, 2008 § Leave a comment

Finally, after what seems like an eternity, I have completed renaming a load of functions to use a nicer more Perl-ish form. It’s taken 3 attempts, but I’ve finally got there and hopefully I haven’t broken anything. I’ll just take a moment to explain how I’ve done the refactoring, so hopefully some veteran coders can tell me how I should have done it 😉

I worked through modules to decide which functions to rename – let’s start in MusicBrainz::Server::Artist for example. I want to rename “sub GetResolution” and “sub SetResolution” into just “sub resolution.” Firstly, I did:

grep -r ‘sub [GS]etResolution’ .

This showed only the getter and setter I was interested in. If it showed more complicated functions (i.e. sub GetResolutionInReverse) then I would rename those first. Next, I would do the bulk of the renaming:

find lib -name ‘*.pm’ | xargs perl -pi -e ‘s/[GS]etResolution/resolution/g’

And finally, I went back into Artist.pm and rewrote sub resolution to look like:

sub resolution
{
    my ($self, $new_resolution) = @_;

    if (defined $new_resolution) { $self->{resolution} = $new_resolution; }
    return $self->{resolution};
}

I did this over just about every function that my facades are using, so I can now merge this refactoring and make a new branch to delete those facades (never liked ’em in the first place!). And just to give an idea how much code this touched:

musicbrainz@doughnut ~/TemplateToolkit/lib $ git diff --shortstat master..HEAD
120 files changed, 2549 insertions(+), 2584 deletions(-)

And one final statistic…

musicbrainz@doughnut ~/TemplateToolkit/lib $ grep -r 'sub [GS]et' . | wc -l
255

255 functions still have Get or Set in front of them – at some point these need to be changed! But this will likely happen after facades are removed – one step at a time.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Most important bits of refactoring complete! at Cycles.

meta

%d bloggers like this: