Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to split code into files/modules? [closed]

To date I had been developing only small Python scripts. They were not longer than 500 lines per each. Now I'm going to write something bigger - I think it will have about 1000 lines. Is it good idea to handle it in one file or is it good time to organize code in subdirectories? I found some advices on how to modularize code, but I can't find any information about when to do that (or rather when it isn't waste of time).

like image 690
ciembor Avatar asked May 27 '12 06:05

ciembor


People also ask

Why do we split codes?

To prevent the requirement of downloading ginormous files, scripts can be split into multiple smaller files. Then features required at page load can be downloaded immediately with additional scripts being lazy loaded after the page or application is interactive, thus improving performance.

Why is Python divided into modules?

Most programs are more than a few lines long. This means that they have to split into multiple small pieces for them to be manageable. Python programs can be split into modules.


1 Answers

I usually do it under these circumstances:

  • You could run parts of the application on thir own and running them would be useful (so they could be reused)
  • A part of the application is abstract and the rest is concrete (The abstract parts could be reused)
  • I want to divide it into 'plugins'
  • A single script would get insanely large (then I divide e.g. by class or put the unittests into a separate file).

In general I try to go for reusability. If I cannot divide it into reusable parts I don't divide except it would get too large.

like image 54
katzenversteher Avatar answered Nov 15 '22 14:11

katzenversteher