0

This question is mainly talking about PHP, but I think it can be applicable to other languages as well. This is a question about code organization.

I have a friend who insists the best approach regarding organization to PHP programming is to have one file/class with all functions ever written inside it. He says it's simpler like this because he can just do a find on function myFunctionName in order to find a function. I've tried breaking off from this approach and using multiple code files/classes to organize related code like I was taught in school. But I find it takes longer to find functions I'm looking for doing it like this. Is there a point when a project becomes big enough that multiple files becomes better than one file? When is this point?

What's a good argument for separating functions into different files?

2 Answers2

1

Very good question! It is very similar to asking "why use classes?" and the answer is encapsulation.

First I would like to negate the "it makes it easier to search" argument, an IDE makes a big difference! Even VB6 offers a find in files option (for the project or a folder location). All IDEs and most text editors have this functionality which is commonly bound to CTRL+SHIFT+F. Take a look at PHPStorm.

Additional benefits are:

  • With large projects if you cannot remember the name of something: it's much easier to navigate an organized file system hierarchy than scroll a huge PHP file.

  • It is also way easier to merge changes with other developers if you were working on separate files! You'll notice other developers are using multiple files - trust me: it's not just because they were told to in school.

  • Some PHP frameworks require you to work in multiple files.

  • But probably the biggest benefit is that it encourages you to at least think about the SRP (Single Responsibility Principle). This is where each class (or even a file in some languages) has a very specific job to do and does only that job. When you get into a habit of this, your code becomes much easier to read -- especially when you return to it after a long hiatus. When you name your file (and class) correctly, you immediately get a sense of purpose for the code and this helps you stay on track.

There's nothing wrong with learning in a single file or even starting a project there -- in fact, many languages (eg. Python, TypeScript) encourage fewer files. But once you feel like you're starting to outgrow this, start reading about Patterns and good naming conventions and you'll realize you can code-share between projects which is a lot easier with files than lines of code.

-2

Usually, what we split into separate files is classes, not functions.

If your code is not object-oriented, so it consists of a multitude of free-standing functions, (as opposed to a bunch of classes where each function belongs to a specific class,) then it might actually be better to leave all functions in a single source file.

This way, you are clearly indicating to the unsuspecting viewer that this is a terrible lengthy mess of sixties-style function-call-function code that they better steer clear from and not even try to read or comprehend.

Dividing the functions into separate files might create the illusion of a software design, while in fact there is none.

Mike Nakis
  • 32,803