As a programmer, I have always wondered whether it is preferable to write (a) short modular functions that are each stored in their own script (i.e., file) or (b) long comprehensive scripts that contain all of their relevant functions.

For example, if I code a data analysis stream that includes a variety of operations performed on the same data (e.g., data cleaning, feature selection, clustering, supervised learning, etc.), should I write a single file even if it runs for hundreds of lines?

I have heard many opinions on the matter and I wonder what this community thinks. I understand that the answer may depend on the type of project, but, if this is the case, then it would be great to hear your thoughts on what factors one should consider in deciding between options (a) and (b).

I am currently coding in Python and I've wondered whether Python programmers have a preference to multiple functions in the same file or one file per function, but I imagine that this question is broader and applies to all programming languages.

closed as primarily opinion-based by gnat, JeffO, GlenH7, Martijn Pieters, Kilian Foth Feb 19 '14 at 8:36

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.


Things that belong together should be kept together. Don't have me open another file just to find some _internal_helper_function() that can't be reused in another context. This re-usability is another important point: If the code you've written could be useful in future projects, structure it as a reusable library. Separate it both logically (dependencies) and physically (files, directories) from the project-specific code.

Most importantly, the code should be easy to read. Shorter files with only a few related definitions each tend to be easier to understand. Beyond 1K lines, you'll have a hard time navigating the source code without search tools, in which case you should refactor.

There is no universal recipe how to find a good balance between the factors reuseability, locality, and ease of understanding. Don't decide for one extreme or the other, rather choose a middle path that does justice to the specifics of the project, and the preferences of the involved programmers.


I would say it really much is dependent on how the language itself is structured:

In Mathematica, you tend to end up with quite large packages, that may contain several K lines. In java, you don't have much choice, the file to functionality is determined by the objects.

If you get annoyed by having too many files, or a too big file, it is an indication that something should change...


The first problem its that your are talking about "functions" and "files" in a object oriented language, the first you need to do is thinking in terms of classes and their relationships if you want to build good OO python code.

And speaking about classes and length, normally a good class its supposed to adhere to the Single Responsibility Principle, one class has one responsibility and one reason to change. Normally a good designed codebase is composed of a lot of small classes with very clear and simple responsibilities, more than 200 or 300 lines in a language like python its a red alert IMMO (an alert to take a second look at the code, not a rule to follow blindly!).

Not the answer you're looking for? Browse other questions tagged or ask your own question.