A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Reasons to split up a very long subroutine into several more subroutines

  • Repetition
    • If the same or very similar code is being called several times, it should be put into a subroutine to avoid code duplication.
    • This is the most obvious reason.
  • Clarity
    • You should be able to read the code like English
    • Reading the code in a top level subroutine or script should give an overview of what it does, without going into detail
    • Each subroutine should be able to be easily described in one or two sentences
    • Comments are not sufficient for this, as you would still have to scan through many pages to gain an understanding. 
    • This reason is controversial; Some people say repetition is the only valid reason (see 'dangers' below)
  • Testing
    • You should be able to test each conceptual part of the system separately.
    • It should be obvious to which part of the system a unit test applies.


Dangers of the second two reasons:
- Too much abstraction, difficult to understand and follow code flow
- Code is slow because of overhead of creating and managing subroutines