Extract some part of the code into it's own module/library from Application Code when :
- Another Application needs it.
- You need to encapsulate it's complexity from the rest of the code such as in the case of cut-and-pasted , boiler plate, legacy code.
- You don't feel the cost of it's maintenance as a separate library is justified.
- You want to make more aggressive optimization.
- The library is getting specialized and hacked to fit to the Application code until it seems natural to just let it be a direct "member" of the Application code.