辅助函数的命名约定是什么?

发布于 2024-07-11 20:30:47 字数 1431 浏览 4 评论 0原文

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

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

发布评论

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

评论(7

思慕 2024-07-18 20:30:47

您可以随意调用辅助函数,只要不将辅助函数放在“全局”命名空间中就没有关系。 简单地添加“素数”似乎是一种常见的做法。 :) 例如,在 Haskell 中,

reverse :: [a] -> [a]
reverse = reverse' []
    where reverse' :: [a] -> [a] -> [a]
          reverse' result [] = result
          reverse' result (x:xs) = reverse' (x:result) xs

You can call the helper function anything you want, and it won't matter as long as you don't put the helper function in the "global" namespace. Simply adding a "prime" seems a common practice. :) E.g., in Haskell,

reverse :: [a] -> [a]
reverse = reverse' []
    where reverse' :: [a] -> [a] -> [a]
          reverse' result [] = result
          reverse' result (x:xs) = reverse' (x:result) xs
淡水深流 2024-07-18 20:30:47

我同意 ShreevatsaR 的观点,如果你不将辅助函数设置为顶级(或者更糟,将其放入导出列表中),那么它的名称是什么并不重要。
我倾向于调用辅助函数 fg

reverse :: [a] -> [a]
reverse = f []
  where
    f ys []     = xs
    f ys (x:xs) = f (x:ys) xs

我只是对小函数使用这个命名方案(否则我不知道f指的是什么)。 话又说回来,你为什么要写大函数呢?

但是,如果您确实想导出“帮助器”函数,因为它可能对其他人有用,我会称其为:

reverseAccumulator

像 Haskell 的 zipzipWith 一样。
但我不会称这些为“帮助器”函数,zipWith 只是一个通用函数,zip 是默认实现(可能是最常用的实现)。

I agree with ShreevatsaR, if you don't make the helper function top-level (or worse, put it in the export list), than it doesn't matter what its name is.
I tend to call helper functions f and g.

reverse :: [a] -> [a]
reverse = f []
  where
    f ys []     = xs
    f ys (x:xs) = f (x:ys) xs

I just use this naming scheme for small functions (otherwise I don't know what the f refers to). Then again, why would you ever write big functions?

However, if you do want to export your 'helper' function because it might be useful to others, I would call it:

reverseAccumulator

Like Haskell's zip and zipWith.
But I wouldn't call those 'helper' functions, zipWith is just a generic function and zip is the default implementation (probably the one thats used the most).

坏尐絯℡ 2024-07-18 20:30:47

我总是使用 do_,就像“do_compute”和“compute”一样。 我发现它非常具有描述性,因为它实际上是执行操作的函数的一部分,而被调用的“计算”需要对外部世界有一个简单的描述性名称。

I always use do_, like "do_compute" with "compute". I find it quite descriptive as it is effectively the part of the function that performs the action, whereas the "compute" that gets called needs to have a simple descriptive name for the outside world.

单身狗的梦 2024-07-18 20:30:47

我使用 auxfoo_aux (用于主函数 foo),并嵌套定义,使其在外部不可见。

I use aux or foo_aux (for main function foo), and nest the definition so it's not externally visible.

眼中杀气 2024-07-18 20:30:47

我倾向于在末尾添加“_recurse”。 所以“reverse_recurse”。 不知道我从哪里得到的。 我喜欢让基本情况函数像您的示例中那样简单。 它往往是“公共”函数,并且它使用辅助函数来执行迭代这一事实与调用者无关。 在 javascript 中,我有时甚至通过闭包隐藏迭代函数,以使其真正清楚地表明它不会被直接调用。

I tend to add "_recurse" to the end. So "reverse_recurse". Not sure where I got that from. I like leaving the base case function simple as you have in your example. It tends to be the "public" function and the fact that it uses a helper function to perform the iteration is irrelevant to the caller. In javascript I sometimes go so far as to hide the iterative function via a closure, to make it really clear it is not to be called directly.

何以畏孤独 2024-07-18 20:30:47

我也同意 ShreevatsaR 的观点,在这个例子中,我会将助手设为私有函数。

对于其他情况,我需要辅助函数在整个模块中可见,但不导出,我倾向于在函数前加上“_”前缀。 当然,有明确的导出声明,但在开发过程中我倾向于导出所有函数以方便交互式探索,例如在 ghci 中。 后来我添加了导出函数的列表,并且下划线可以很容易地记住我是否打算将函数作为本地函数。

I also agree with ShreevatsaR, in this example I would make the helper a private function.

For other cases where I need helper functions to be visible in the whole module, but not exported, I tend to prefix functions with '_'. Sure, there is the explicit exports statement but during development I tend to export all functions to ease interactive exploration, e.g. in ghci. Later on I add the list of exported functions and the underbar makes it easy to remember whether I intended a function to be local or not.

哭泣的笑容 2024-07-18 20:30:47

设置并执行

示例:

function whateverSetup() { ... }
function whateverExecute() { ... }

setup and execute

example:

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