对于一个文件来说多少行 PHP 代码太多?

发布于 2024-08-16 08:05:23 字数 204 浏览 6 评论 0原文

我正在创建一个 PHP 文件,该文件执行 2 个 mysql 数据库调用,脚本的其余部分是用于 file_exists 和其他简单变量等内容的 if 语句。到目前为止,我在这个文件中有大约 2000 行代码。

如果某个陈述是正确的,那么包含一个单独的文件是否是更好的做法?或者直接在 if 语句本身中键入代码?

PHP 应该遵守的单个文件的最大代码行数是多少?

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 lines of code in this file so far.

Is it better practice to include a separate file if a statement is true; or simply type the code directly in the if statement itself?

Is their a maximum number of lines of code for a single file that should be adhered to with PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

踏雪无痕 2024-08-23 08:05:23

我想说不应该有任何与 php 文件中的行数相关的性能问题,它可以根据您的需要而定。

现在,对于模式和最佳实践,我想说你必须自己判断,我看到了许多组织良好的数千行文件和许多实际上很小且难以阅读的文件。
我的建议是:

  • 判断源代码的可读性,始终组织良好。
  • 如果您的文件同时执行以下两项任务,那么在某种程度上进行逻辑分离很重要:大量数据库访问、写入、修改、html 渲染、ajax 等等。您可能想要分离事物或使用面向对象的方法。
  • 始终寻找逻辑分离和代码之间的平衡。大量 10 行文件不应该显得杂乱或过于整洁

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
灰色世界里的红玫瑰 2024-08-23 08:05:23

从计算机的角度来看,单个文件中包含 2000 行代码并不完全是坏事,但在大多数情况下可能是可以避免的,请查看 MVC设计模式,它将帮助您更好地组织代码。

此外,请记住,包含(大量)文件会减慢代码的执行速度。

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.

夏の忆 2024-08-23 08:05:23

您可能想读一本类似 Bob Martin 的《简洁代码》 的书。以下是该书中的一些要点:

  • 类应该承担一项责任
  • 函数应该做一件事并且做得很好

对于 PHP,如果您不使用类方法;你会遇到重复问题。帮自己一个忙,阅读一些有关该主题的文章;它将为您节省更多的扩展和维护时间。

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.

何时共饮酒 2024-08-23 08:05:23

行数并不是性能的良好指标。确保您的代码被有效地组织,分为逻辑类或块,并且不要将不相关的代码组合到单个模块中。

像 PHP 这样的语言的问题之一是,除非进行一些创造性的缓存,否则每次请求托管页面时,每个包含文件的每一行都必须标记化,通过解析树压缩并转换为有意义的指令。 .NET 和 Java 等编译平台不会受到这种性能杀手的影响。

另外,由于其他海报之一提到 MVC 作为保持文件简短的一种方式:良好的代码组织是经验和常识的函数,并且与任何特定的模式或架构无关。 MVC 很有趣,但并不是这个问题的解决方案。

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.

七月上 2024-08-23 08:05:23

您需要关注行数吗?不,不一定。只要确保您的代码是有组织的、高效的并且没有不必要的冗长即可。

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.

硪扪都還晓 2024-08-23 08:05:23

这其实并不重要,只要您正确记录了代码,尽可能模块化,并检查是否存在任何低效率的情况。您很可能有一个 10,000 行的文件。尽管我通常将申请的每个部分拆分为 500-1000 左右。

It really doesn't matter, so long as you have documented your code properly, modularised as much as possible, and checked for any inefficiencies. You may well have a 10,000 line file. Although I usually split at around 500-1000 for each section of an application.

会傲 2024-08-23 08:05:23

2k 行对我来说听起来太多了...尽管这取决于您遵循的代码风格,例如许多换行符、许多小函数或良好的 api 契约注释可以增加大小,尽管它们是很好的做法。良好的代码格式也可以增加行数。

关于 PHP,最好知道:它是 2k 行,只有一个类,还是只有一个大的包含非 OOP PHP 代码?它是否与模板语句和程序逻辑混合在一起(就像我在 PHP 代码中经常发现的那样)?

通常我不计算这些行,何时分割。他们只是养成了习惯。如果代码变得混乱,我会做出反应并重构。仍然查看了我们作为一个团队最近编写的一些代码,我可以看到一些模式:

  • 如果大小大于 20LOC(无注释),则提取函数/方法,
  • 如果大小 > 200,则使用 if/else 子句提取到另一个类 - 300LOC
  • 如果工件 > 10,则

提取到另一个包/文件夹但这仍然取决于我拥有的代码类型。例如,如果涉及大量逻辑(if/else/switch/for),每个函数的 LOC 就会减少。如果几乎不涉及任何逻辑(简单愚蠢的单路径代码语句),限制就会增加。最后最重要的规则是:人类能否理解代码。她/他能很好地阅读它吗?

2k lines sound too much to me... Though it depends what code style you are following, e.g. many linebreaks, many little functions or good api-contract comments can increase the size though they are good practice. Also good code formatting can increase lines.

Regarding PHP it would be good to know: Is it 2k lines with just one class or just one big include with non-OOP PHP code? Is it mixed with template statements and programm logic (like I find often in PHP code)?

Usually I don't count these lines, when to split. They just went into habits. If code gets confusing I react and refactor. Still having looked into some code we as a team wrote recently, I can see some patterns:

  • extract function/method if size is bigger than 20LOC (without comments) and usage of if/else clauses
  • extract to another class if size >200-300LOC
  • extract to another package/folder if artifacts >10

Still it depends what the kind of code I have. For instance if loads of logic is involved (if/else/switch/for), the LOC per function decreases. If there is hardly any logic involved (simple stupid one-path code statements) the limits increase. In the end the most-important rule is: Would a human understand the code. Will she/he be able to read it well.

開玄 2024-08-23 08:05:23

我不知道有什么有用的方法来分割这么简单的代码,特别是如果它们在语义上属于在一起的话。

考虑是否可以通过重构消除一些代码可能更有趣。例如,如果您经常使用具有稍微不同的变量的特定检查组合,则将检查组合外包到函数中并在适当的地方调用它可能会有所帮助。
我记得曾经看到过一个项目,大部分内容都写得很好,但它有这样的问题。例如,解析其配置文件的代码是这样重复的:

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

这是一个极端的例子,但你明白了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文