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?

4
  • 3
    Ever considered using a text editor or IDE that allows to search over multiple files? Dec 12, 2015 at 19:26
  • ^ this. I just cannot imagine having to work on codebase bigger than a couple of tens of lines without the help of proper search - your friend really urgently needs to learn some new tools
    – stijn
    Dec 12, 2015 at 20:08
  • 1
    How do I explain ${something} to ${someone}?
    – user22815
    Dec 12, 2015 at 20:21
  • I feel like it's ironic that this question is -2 but I just got the popular question badge for over 1000 views and the accepted answer says "Very good question!" on the first line. This is a good question for beginner programmers. Apr 28, 2017 at 16:49

2 Answers 2

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.

4
  • My code is OOP. The single file was actually a class with a bunch of methods. The multiple files would be one class per file with a bunch of methods. But this didn't answer my question, why is the "sixties-style function-call-function code" bad if you consider how much faster it is to find stuff? Dec 12, 2015 at 20:03
  • well then, we did not care at all to hear about your methods. Your question should have been "All classes in one file versus multiple files". Methods would be irrelevant, and "functions" is misleading.
    – Mike Nakis
    Dec 12, 2015 at 20:06
  • 1
    How is it any different? Just putting class MyClass { } around the functions doesn't make much of a difference. Dec 12, 2015 at 20:08
  • 1
    o rly? srsly? OOP is putting class MyClass { } around hodgepodges of functions? Well, in that case, my answer is definitely irrelevant.
    – Mike Nakis
    Dec 13, 2015 at 13:51

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