views:

459

answers:

6

After asking organising my Python project and then calling from a parent file in Python it's occurring to me that it'll be so much easier to put all my code in one file (data will be read in externally).

I've always thought that this was bad project organisation but it seems to be the easiest way to deal with the problems I'm thinking I will face. Have I simply gotten the wrong end of the stick with file count or have I not seen some great guide on large (for me) projects?

+13  A: 

If you are planning to use any kind of SCM then you are going to be screwed. Having one file is a guaranteed way to have lots of collisions and merges that will be painstaking to deal with over time.

Stick to conventions and break apart your files. If nothing more than to save the guy who will one day have to maintain your code...

Josh
+1 - I concur with the SCM point. Its a trade off or a long term investment here. Software projects are not legacy of a single programmer. Take a shortcut now and you will have to pay later. So better take a hard decision and some pain now. Its anyway to adding to your code organization skills.
JV
Um, any decent SCM should be able to comfortably deal with changes in the same file, especially when there is only person who's going to be editing it. Even the oldest one in existence I know -- RCS -- works well with a single file.
ShreevatsaR
"there is only person who's going to be editing it" will that be the case going forward too? Isn't that a sizable assumption to make? unless this is college project which will end when submitted.
JV
It's a hobby project, I don't see it as likely that there'll ever be more than one person involved.
Teifion
+1 for mentioning SCM
Andrew Hare
no, a godfile is terrible on many grounds, SCM being a big one and hobby-projects not excusing that - don't practice bad habits just because they're convenient
annakata
+4  A: 

If your code is going to work together all the time anyway, and isn't useful separately, there's nothing wrong with keeping everything in one file. I can think of at least popular package (BeautifulSoup) that does this. Sure makes installation easier.

Of course, if it seems, down the road, that you could use part of your code with another project, or if maintainance starts to be an issue, then worry about organizing your project differently.

It seems to me from the questions you've been asking lately that you're worrying about all of this a bit prematurely. Often, for me, these sorts of issues are better tackled a little later on in the solution. Especially for smaller projects, my goal is to get a solution that is correct, and then optimal.

Triptych
I am coming to the end of a project which weighs in at 11k lines of code and was so easy only because I organised it really well to start with. That was in PHP, I figured I should do the same here so as to help make this easier :)
Teifion
emerge, which is the main program of the Gentoo Linux package manager, is one single file consisting of ~7000 lines of Python code.
Federico Ramponi
+2  A: 

It's always a now verses then argument. If you're under the gun to get it done, do it. Source control will be a problem later, as with many things there's no black and white answer. You need to be responsible to both your deadline and the long term maintenance of the code.

Dan Williams
Good summary phrase! The practices that make it easy to get a quick first cut working are often not those that support long term maintenance and reusability.
joel.neely
+2  A: 

If that's the best way to organise it, you're probably doing something wrong.

If it's more than just a toy program or a simple script, then you should break it up into separate files, etc. It's the only sane way of doing it. When your project gets big enough that you need someone else helping on it, then it will make the SCM a whole bunch easier.

Additionally, sooner or later you are going to need to add a separate utility to your project, that is going to need some common code/structures. It's far easier to do this if you have separate source files than if you have just one big one.

CodeSlave
+2  A: 

Since Calling from a parent file in Python indicates serious design problems, I'd say that you have two choices.

  1. Don't have a library module try to call back to main. You'll have to rewrite things to fix this.

    [An imported component calling the main program is an improper dependency. And Python doesn't support it because it's a poor design.]

  2. Put it all in one file until you figure out a better design with proper one-way dependencies. Then you'll have to rewrite it to fix the dependency problems.

A module (a single file) should be a logical piece of related code. Not everything. Not a single class definition. There's a middle ground of modularity.

Additionally, there should be a proper one-way dependency graph from main program to components (which do NOT depend on the main program) to utility libraries and what-not (that do not know about the components OR the main program.

Circular (or mutual) dependencies often indicate a design problem. Callbacks are one way out of the problem. Another way is to decompose the circular elements to get a proper one-way graph.

S.Lott
+2  A: 

Looking at your earlier questions I would say all code in one file would be a good intermediate state on the way to a complete refactoring of your project. To do this you'll need a regression test suite to make sure you don't break the project while refactoring it.

Once all your code is in one file, I suggest iterating on the following:

  1. Identify a small group of interdependent classes.

  2. Pull those classes into a separate file.

  3. Add unit tests for the new separate file.

  4. Retest the entire project.

Depending on the size of your project, it shouldn't take too many iterations for you to reach something reasonable.

Norman Ramsey