As a follow up to my earlier node Is there an ideal module size or subroutine size?, I offer my thoughts on refactoring my own web application code.
Setup: My CGI::Application web app consists of three modules, Users, Projects and Files. Recently, I had to add functionality to allow for the creation of multiple users, attached to one or more projects, or not attached at all. I also had to add similar functionality when creating a project: it should be possible to add a pile of user names (we're using E-Mail addresses) as new members of this project.
Development: Realizing that there would be some duplicated work, I added the functionality to the Create Project run mode, got it working, then hacked the same code (cargo cult, I know) into Create User, and get that to work.
Refactoring: Once the code was all working, I printed out the two modules, spread them out over a large counter top, and started marking them up. I highlighted the package statements and each of the method names and drew boxes around the duplicated hunks of code. I spent a long time looking at repeated patterns in the code, separating the run modes from the utility routines.
I moved the utility routines into a seperate Util module, ending up with Users::Util and Projects::Util, and fixed the various problems -- missing modules in the two Util modules, extraneous modules in the main modules.
Having done a lot of work on the Projects and Users modules, I then printed out the Files module and found that I could do a lot of similar work there, and ended up creating a Files::Util module. I also found that there were a few routines that would be used in several of the modules, and created a top level Util module that all of the modules used.
Testing: It's hard to test your own code, but I try. I have Log::Log4perl turned to DEBUG for all of my modules, and I tail the log file in one window, and tail httpd-ssl-error in another window, then bash away using all of the features that I can think of. If I fix an error, I go back and test all of the functionality around that error.
Conclusion: You sure get some weird looks from co-workers when you stand over twenty odd pages of code, printed two-up, for several days running. But I'm now very happy with the structure of the code, and I look forward to a code review. And the size of my modules went down by a total of 388 lines, but that was made up by an increase in the Util modules with a total of 492 lines. Still, it's worth it -- the code's better organized, with run modes in the main modules and utility routines in the Util sub-modules. And my average module size wen from about 900 lines to about 800 lines -- that's OK. At least perltidy is happy with my code, and I have tests for some of the utility modules.
Oh, did I forget to mention the 1800 lines of Template::Toolkit web pages? Eh, never mind.
In reply to My code refactoring journey by talexb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |