views:

564

answers:

7

hi guys, i'm currently working on a perl web app LAMP style and recently stumbled upon this death maze of code left by some previous developer. He left so many magic numbers and weird logic that it's gives me a headache everytime i had to go through it.

I'm learning unit testing right now so i want to find some useful tool to refactor the code and write unit test along the way to make sure that i don't accidentally break anything.

Any recommendation for any good refactoring tool for LAMP perl? Preferably free :)

Thanks.

+5  A: 

Probably, volunteers on Refactor :my => 'code' can help you. Anyway, it's free to ask :)

jetxee
That looks an interesting site. +1 for bringing it to our attention!
draegtun
A: 

vim! (Or any other text editor)

There really isn't a magic tool to refractor your code, there is tools around to (for example) rename variables/functions, but there is no way it can magically fix horrible code structure or weird logic.

dbr
well i was looking along the lines of tools that can help me identify bad code smells for refactor, etc. :)
melaos
+12  A: 

I think Eclipse / EPIC has "some" Perl refactoring tools... but don't quote me on that ;-)

Might also be worth checking out Komodo. However the full version isn't free like Eclipse / EPIC. You could try their opensource version Komodo Edit but I guess it won't have all the features?

I've not used either above or any other refactoring tools... I get by with vi/vim & TextMate editors and what they provide (or what I've so far found in each of these editors!).

Making unit tests is a good start. Also have a look perltidy / Perl::Tidy & Perl::Critic which may help peer thru the mess and find some of those "code smells".

/I3az/

draegtun
Kurt W. Leucht
+5  A: 

While it's not really refactoring in any great depth, this PerlMonks node describes a couple of Vim mappings for deobfuscating perl code using B::Deparse.

Their examples:

You can type _d when your cursor is over this line in normal mode:

--$|&&s|\n|-|;

... and it will be replaced with:

s/\n/-/ if --$|;

And this line:

$foo and $bar or $baz = 1;

... would be replaced with:

$baz = 1 unless $foo and $bar;
Adam Bellaire
+6  A: 

Perl's dynamic nature means that it is very hard to create refactoring tools for it.

However with regards to testing you should be able to put together some regression tests to help you on your way. This works by starting with the code as it is now and capturing its current output. This might be running the CGI script from the commandline and saving the resultant HTML to file.

Once you've captured this you can change the code and as you go check to see that the HTML has not changed, which means that the code has not broken. When changes do occur you can then either find the bug, or change the test to accept the new HTML as correct.

This can be a bit of a chore to set up but will make your life easier in the long run. You should try to automate these tests to make the easy to run. Checkout Test::WWW::Mechanize and Test::LongString as well as all the others.

EvdB
+1  A: 

Eclipse with the EPIC plug-in does have some refactoring support. Not as sophisticated as IntelliJ's refactoring for Java tool. Not 100% sure it would help with your problem though. They way I've used it is to highlight blocks of code and move them into functions / methods. In your case you're probably going to want to do a lot of search/replace on those magic numbers...

I assume you're going to use Test::More to write your unit tests. Some of the other EPIC tools could help with that (eg "todo" tags).

You could also use Test::WWW::Mechanize and Test::WWW::Selenium -- would be useful in your case to have selenium tests defined to make sure you don't break any end-user functionality. EPIC doesn't have anything in particular to help with that though but it should be possible to use it to step-through the code when debugging.

Good luck. :-)

Hissohathair