How many lines of PHP code is too many for one file?

前端 未结 8 2295
日久生厌
日久生厌 2021-02-20 15:55

I\'m creating a PHP file that does 2 mysql database calls and the rest of the script is if statements for things like file_exists and other simple variables. I have about 2000

相关标签:
8条回答
  • 2021-02-20 16:07

    2000 lines of code in a single file is not exactly bad from a computer point of view but in most situations is probably avoidable, take a look into the MVC design pattern, it'll help you to better organize your code.

    Also, bear in mind that including (a lot of) files will slow down the execution of your code.

    0 讨论(0)
  • 2021-02-20 16:13

    You may want to read a book like Clean Code by Bob Martin. Here are a few nuggets from that book:

    • A class should have one responsibility
    • A function should do one thing and do it well

    With PHP, if you aren't using the Class approach; you're going to run into duplication problems. Do yourself a favor and do some reading on the subject; it'll save you a lot more time in extending and maintenance.

    0 讨论(0)
  • 2021-02-20 16:16

    I would say there should not be any performance issue related to the number of lines in your php files, it can be as big as you need.

    Now, for the patterns and best practices, I would say that you have to judge by yourself, I saw many well organized files of several thousand lines and a lot of actually small and difficult to read files. My advise would be:

    • Judge the readability of the source code, always organize it well.
    • It's important to have a logical separation to some extent, if your file does both: heavy database access, writing, modification, html rendering, ajax and so on.. You may want to separate things or use object oriented approach.
    • Always search the balance between the logical separation and code. It should not be messy nor extra-neat with a lot of 10-line files
    0 讨论(0)
  • 2021-02-20 16:16

    Do you need to focus on the number of lines? No, not necessarily. Just make sure your code is organized, efficient, and not unnecessarily verbose.

    0 讨论(0)
  • 2021-02-20 16:16

    I don't know any useful way to split code that's that simple, particularly if it all belongs together semantically.

    It is probably more interesting to think about whether you can eliminate some of the code by refactoring. For example, if you often use a particular combination of checks with slightly different variables, it might help to outsource the combination of checks into a function and call it wherever appropriate.
    I remember seeing a project once that was well-written for the most part, but it had a problem of that kind. For example, the code for parsing its configuration file was duplicated like this:

    if (file_exists("configfile")) {
      /* tons of code here */
    } else if (file_exists("/etc/configfile")) {
      /* almost the same code again */
    }
    

    That's an extreme example but you get the idea.

    0 讨论(0)
  • 2021-02-20 16:17

    Line count is not a good indicator of performance. Make sure that your code is organized efficiently, divided into logical classes or blocks and that you don't combine unrelated code into single modules.

    One of the problems with a language like PHP is that, barring some creative caching, every line of every included file must be tokenized, zipped through a parse tree and turned into meaningful instructions every time the hosting page is requested. Compiled platforms like .NET and Java do not suffer from this performance killer.

    Also, since one of the other posters mentioned MVC as a way to keep files short: good code organization is a function of experience and common sense and is in no way tied to any particular pattern or architecture. MVC is interesting, but isn't a solution to this problem.

    0 讨论(0)
提交回复
热议问题