代码结构:我应该使用很多函数来增加可读性吗?

发布于 2024-10-10 12:59:49 字数 374 浏览 11 评论 0原文

我的问题考虑到了 Bash 和 PowerShell 脚本,但我认为它也适用于其他语言。

据我了解,函数的目的是多次执行相同(或非常相似)的任务。这减少了脚本中的代码量,也使其更易于维护。

考虑到这一点,如果您发现您的脚本仅调用一个函数一次,那么该函数就没有理由作为函数存在。相反,您应该获取该函数的代码并将其放置在调用该函数的位置。

说了这么多,我的问题是:

如果我有一个复杂的脚本,我是否应该将每个代码部分移到它自己的函数中,即使每个函数只会被调用一次?这将大大增加脚本的可读性,因为它的逻辑(函数)将全部位于脚本的顶部,而执行流程将位于脚本的底部。由于 50 行代码仅用 1 行表示,因此更容易理解脚本的作用。

其他人也这么做吗?这种方法有缺点吗?

My question has Bash and PowerShell scripts in mind, but I suppose it applies to other languages as well.

It is my understanding that the purpose of a function is to perform the same (or a very similar) task multiple times. This decreases the amount of code in the script and it also makes it easier to maintain.

With that in mind, if you discover that your script only calls a function one time then there's no reason for that function to exist as a function. Instead, you should take the function's code and place it in the location where that function is being called.

Having said all that, here's my question:

If I have a complicated script, should I move each section of code into its own function even though each function will only be called once? This would greatly increase the script's readability because its logic (the functions) would all be at the top of the script and the flow of execution would be at the bottom of the script. Since 50 lines of code would be represented by just 1 line, it would be much easier to understand what the script is doing.

Do other people do this? Are there disadvantages to this approach?

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

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

发布评论

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

评论(7

只涨不跌 2024-10-17 12:59:49

拥有函数也可以增加可读性。因此,如果 bash 脚本如下所示,它可能看起来更好并且更容易理解:

getParams()

startTask()

doSomethingElse()

finishTask()

# implement functions below

即使函数实现很简单,它也读起来更好。

Having functions also increases readability. So a bash script might look better and be easier to follow if it reads:

getParams()

startTask()

doSomethingElse()

finishTask()

# implement functions below

even if the function implementations are simple, it reads better.

宫墨修音 2024-10-17 12:59:49

代码可读性确实是一个主要问题,通常(现在)比代码量或性能更重要。更不用说内联函数调用不一定具有明显的性能优势(非常特定于语言)。

因此,许多开发人员(我敢说是同类中的佼佼者:-)创建了像您所描述的小函数/方法,将他们的代码划分为逻辑上有凝聚力的部分。

Code readability is indeed a major concern, usually (nowadays) more important than sheer amount of code or performance. Not to mention that inlining function calls may not necessarily have noticeable performance benefits (very language specific).

So lots of developers (I venture to say that the better of the breed :-) create small functions/methods like you describe, to partition their code into logically cohesive parts.

云醉月微眠 2024-10-17 12:59:49

函数执行明确定义的任务。如果您有一个执行 5 项不同操作的大型函数,则强烈建议它应该调用 5 个较小的函数。

A function does a well-defined task. If you have a mega function that does 5 different things, it strongly suggests it should be calling 5 smaller functions.

看春风乍起 2024-10-17 12:59:49

我的理解是,函数的目的是多次执行相同(或非常相似)的任务。

嗯,我的理解是,函数是执行特定的离散实体,明确的任务。

考虑到这一点,如果您发现您的脚本至少调用给定函数一次,那么它就完成了它的工作。

It is my understanding that the purpose of a function is to perform the same (or a very similar) task multiple times.

Well, it is my understanding that a function is a discrete entity that performs a specific, well defined task.

With that in mind, if you discover that your script calls a given function AT LEAST ONCE, then it's doing its job.

纸伞微斜 2024-10-17 12:59:49

专注于能够阅读并轻松理解您的代码。

拥有清晰、可读的代码肯定比担心函数调用开销更有好处。这只是一个过早的优化。

另外,函数的目标是完成特定的任务。任务可以是子任务,这没有错!

Focus on being able to read and easily understand your code.

Having clear, readable code is definitely more a payoff than being afraid of function calls overhead. That's just a premature optimisation.

Plus, the goal of a function is to accomplish a particular task. A task can be a sub-task, there's nothing wrong with that!

栀子花开つ 2024-10-17 12:59:49

阅读本书

http://www.amazon.com/Clean-Code-Handbook-Software -Craftsmanship/dp/0132350882

以下是书中的一些引述。

“小的!
函数的第一条规则是它们应该很小。函数的第二条规则是
它们应该比这个小。”

“函数应该做一件事。他们应该把这件事做好。他们应该只做这件事。”

Read this book

http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882

Here are some quotes from the book.

"Small!
The first rule of functions is that they should be small. The second rule of functions is that
they should be smaller than that."

"FUNCTIONS SHOULD DO ONE THING. THEY SHOULD DO IT WELL.THEY SHOULD DO IT ONLY."

始于初秋 2024-10-17 12:59:49

据我所知,函数代表一系列步骤,这些步骤成为更大程序的一部分。
对于你的问题,我强烈同意函数可以提高可读性和可重用性。但同时将所有事情分解可能不是一个好的做法。
最后我想说一句:“凡事过度,皆无益!”

As far as my knowledge is concerned, a function represents a Sequence of steps which become a part of larger program.
Coming to your question, I strongly agree that function(s) improve readability and re-usability. But at the same time breaking every thing into pieces might not be a good practice.
Finally, I want to give one statement : "Anything In Excess Is Not Beneficial!"

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