views:

72

answers:

5

I do PHP coding a lot in my company and personal work. Usually my files get bigger, sometimes more than 2000-3000 lines long. Then, they get difficult to manage.

My Question: What should be (is) the standard length of a PHP code file in terms of lines-of-code. At what length do you guys split it up?

Note: No Object Oriented programming (I don't use classes). Please answer accordingly.


Clarification of not using classes:

  1. I do use functions a lot.
  2. I don't use classes because the code is legacy. I have to maintain that and add new features.
  3. I was a C programmer before. So, going OO is somewhat tough for me. Like learning whole new way of doing things.
+2  A: 

Maybe you should start using classes then.

BTW, I definitely split the PHP code files at 1000 lines of code.

shamittomar
1000. Hmm that appears a nice number.
WhatIsOpenID
+3  A: 

There is no good standard length. Some files grow bigger, some smaller.

A good guiding principle from Object Oriented Programming is separating tasks and concerns into classes, and splitting those classes into separate files.

That is the most logical separation, and allows using PHP 5's Autoloading. The basic principles may be worth adopting even if you don't want to get into serious OOP.

Related questions:

Pekka
Autoloading <<-- that's something new. Thanks.
WhatIsOpenID
@whatis check this question for a use of Autoloading with functions. http://stackoverflow.com/questions/2457653/php-import-functions
Pekka
Nice link. Thanks again. I already +1'd you.
WhatIsOpenID
+3  A: 

Code should not be split according to number of lines of code, it should be split according to functionality. Parts of your code that handle, say, templating, should go in different files (and possibly directories) than parts that handle, say, authentication. If you have a file that's thousands of lines long, it's almost certainly doing way too much and needs to be split up, if not refactored entirely.

Jordan
But what if they are related and yet the code file becomes too large. My project has 14 files and 4 of them are more than 2000 lines long. Content in them is related.
WhatIsOpenID
+1  A: 

Use classes and OO programming. I have been to an workshop once "make love to your code" that stated to avoid functions that are longer as the space on your monitor (you should not scroll to look at the whole function)

Mark Baijens
+1  A: 

Even quite large code files can be reasonably easy to manage if you organise them well. You should keep your functions short, keep related functions together, and name them well.

You will also find it easier to manage if you use an IDE with a function lookup table - I use Netbeans, and on the left hand side it gives me a panel with quick links to all the functions in my current file. It also gives me the ability to click on a line where a function is called and jump to the declaration (anwhere in the project).

On the other hand, if you have code files several thousand lines long which consist of a single function, then yes, the odds are it will be very hard to manage, an no amount of IDE cleverness will help.

Spudley