私有函数vs嵌套函数 [英] private functions vs nested functions

查看:133
本文介绍了私有函数vs嵌套函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:何时使用私有函数以及何时使用嵌套函数?

(我问的是 F#,但也许答案可能与其他功能语言相关)



一个小例子

  namespace SomeName 

模块BinaryReaderExt =
打开System.IO

let seek(reader :BinaryReader)positions =
reader.BaseStream.Seek(positions,SeekOrigin.Current)|>忽略

模块Mod =
打开System.IO

让私人prFun阅读器:BinaryReader =
//做某事
BinaryReaderExt.seek阅读器10L


let outerFun(stream:System.IO.Stream)=
let reader = new System.IO.BinaryReader(stream)
let seek = BinaryReaderExt .seek reader

让nestedFun()=
寻找10L
//做某事

nestedFun()
prFun reader

嵌套函数可以使用更高范围的数据,这是一个很大的好处。也不污染周围的模块。但看起来很笨拙,不是吗?特别是当有一些大的嵌套函数时,私有函数可以被公开并被测试。看起来它们看起来更具可读性



您的意见是什么? 解决方案

< blockquote>

嵌套函数可以使用更高范围的数据,这是一个很大的好处。它也不会污染周围的模块。


我同意你的观点。我的建议是将功能保留在正确的范围内。例如,如果函数仅用于一个地方,最好是嵌套函数。例如,向上移动 loop 并使其成为 private 函数是没有任何意义的。

 让长度xs = 
让rec循环acc =函数
| [] - > acc
| _ :: xs - >循环(acc + 1)xs
循环0 acc




但看起来很笨拙,不是吗?特别是当有一些大的嵌套函数时


如果您需要大型嵌套函数,很可能您做错了。它们应该被分成多个小的嵌套函数,或者最外面的函数应该被转换为一个类型。


相反,可以创建私有函数公开和被测试。看起来它们看起来更具可读性。

可读性是一个主观的问题。我认为组织问题更重要。嵌套函数的要点是它们很简单,可以通过测试最外层函数来测试。



当函数具有更多的适用性时,可以将它们放入实用程序模块中,需要时打开该模块。请注意,除了标记 private 之外,还有其他技巧可以隐藏功能。例如,您可以使用一个 fsi 文件来指示哪个接口被暴露。


the question is:

when to use private functions and when to use nested functions? (i'm asking about F# but maybe answers can be relevant in other functional languages)

a small example

namespace SomeName

module BinaryReaderExt =
    open System.IO

    let seek (reader : BinaryReader) positions =
        reader.BaseStream.Seek(positions, SeekOrigin.Current) |> ignore

module Mod =
    open System.IO

    let private prFun reader:BinaryReader =
        //do something
        BinaryReaderExt.seek reader 10L


    let outerFun (stream :System.IO.Stream) =
        let reader = new System.IO.BinaryReader(stream)
        let seek = BinaryReaderExt.seek reader

        let nestedFun () =
            seek 10L
            //do something

        nestedFun()
        prFun reader

it's a big bonus that a nested function can use data from higher scope. also it does not pollute the surrounding module. but is looks clumsy, isn't it? especially when there are some large nested functions

on opposite, private functions can be made public and be tested. and it seems that they look more readable

what's your opinion?

解决方案

It's a big bonus that a nested function can use data from higher scope. also it does not pollute the surrounding module.

I agree with your points. My advice is to keep functions at the right scopes. For example, if the function is used in only one place, it's better to be a nested function. For example, there's no point to move loop upwards and make it a private function.

let length xs =
   let rec loop acc = function
      | [] -> acc
      | _::xs -> loop (acc + 1) xs
   loop 0 acc

But is looks clumsy, isn't it? especially when there are some large nested functions

If you need large nested functions, it's likely you're doing it wrong. They should be broken into multiple small nested functions or the outermost function should be converted to a type.

On opposite, private functions can be made public and be tested. and it seems that they look more readable.

Readability is a subjective matter. I think organization issue is more important. The points of nested functions are that they're simple and could be tested by testing outermost functions.

When functions have more applicability, you can put them into a utility module and open that module when needed. Note that there are other techniques to hide functions other than marking them private. For example, you can use an fsi file to indicate what interface is exposed.

这篇关于私有函数vs嵌套函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆