英文:
Why create a function that returns a single line of a function that does the same thing?
问题
我正在尝试通过查看各种公共代码来理解不同的编码架构。其中之一是Go团队的mime/multipart
实现。
下面的代码片段是我看到的。https://cs.opensource.google/go/go/+/refs/tags/go1.19.3:src/mime/multipart/formdata.go;l=156
func (r *Reader) ReadForm(maxMemory int64) (*Form, error) {
return r.readForm(maxMemory)
}
func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
form := &Form{make(map[string][]string), make(map[string][]*FileHeader)}
defer func() {
if err != nil {
form.RemoveAll()
}
}()
// Reserve an additional 10 MB for non-file parts.
maxValueBy
...更多代码
我已经阅读了一些关于SOLID、DRY、公共/私有关系的内容,所以我不能说我了解很多最佳实践/常见策略。
从上面的代码来看,它似乎是将一个私有函数公开为公共函数。
我能想到的唯一一件事是,这纯粹是为了文档的缘故?但我脑海中没有具体的想法。
所以我在这里困惑的是这样做的好处是什么?
谢谢大家抽出时间阅读这篇文章。
非常感谢任何评论/阅读建议。
英文:
I'm trying to understand various coding architectures by looking into various public codes. One of which is the mime/multipart
implementation by the Go team.
The below snippet is what I've seen. https://cs.opensource.google/go/go/+/refs/tags/go1.19.3:src/mime/multipart/formdata.go;l=156
func (r *Reader) ReadForm(maxMemory int64) (*Form, error) {
return r.readForm(maxMemory)
}
func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
form := &Form{make(map[string][]string), make(map[string][]*FileHeader)}
defer func() {
if err != nil {
form.RemoveAll()
}
}()
// Reserve an additional 10 MB for non-file parts.
maxValueBy
...more code here
I've read through some stuff about SOLID, DRY, public/ private relationships so I can't say I know a lot of best practices/ common strategies.
Looking at the above, it looks to me like its a function that makes a private function public.
The only thing that comes to my mind is that its purely for documentation sake? But nothing concrete in my mind.
So what I'm struggling to understand here is what's the benefit of doing so?
Thank you all for taking the time to read this.
Any comments/ reading suggestions is very much appreciated.
答案1
得分: 2
这是为了文档的目的。PR评论解释道:
> 仅当命名返回值对文档有所贡献时,才应在公共函数和方法中使用命名返回值。
>
> 如果命名返回值仅仅是为了在函数体内节省几行代码,尤其是如果这导致文档中出现重复的词语或者只是为了让程序员使用裸返回语句而存在,那么就不应该使用命名返回值。(除非在非常小的函数中,否则不应使用裸返回语句)
>
> 此更改是对公共函数签名的手动审核和清理。
为了隐藏返回值的名称,原始函数
func (r *Reader) ReadForm(maxMemory int64) (f *Form, err error) {
⋮
}
被更改为
func (r *Reader) ReadForm(maxMemory int64) (*Form, error) {
return r.readForm(maxMemory)
}
func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
⋮
}
错误返回值的名称不能被消除,因为一个延迟函数访问了错误返回值。
英文:
It is for the sake of the documentation. The PR comment explains:
> Named returned values should only be used on public funcs and methods when it contributes to the documentation.
>
> Named return values should not be used if they're only saving the programmer a few lines of code inside the body of the function, especially if that means there's stutter in the documentation or it was only there so the programmer could use a naked return statement. (Naked returns should not be used except in very small functions)
>
> This change is a manual audit & cleanup of public func signatures.
To hide the return value names, the original function
func (r *Reader) ReadForm(maxMemory int64) (f *Form, err error) {
⋮
}
was changed to
func (r *Reader) ReadForm(maxMemory int64) (*Form, error) {
return r.readForm(maxMemory)
}
func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
⋮
}
The error return value name can not be eliminated because a
deferred function accesses the error return value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论