Why create a function that returns a single line of a function that does the same thing?

Justin Yeoh

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.

Cerise Limón

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does one Bayes evaluation function work and the other not? it the same thing in my point and i must know why

Why does this definition returns a function?

Why cant a function name and a variable name be called the same thing in a class?

Are eval() and new Function() the same thing?

Why does the function call get skipped when there are 2 function calls on the same line?

Why glGetSubroutineIndex returns the same value for different function?

Why does this function returns '' instead of a concatenated string

Why does printing a function name returns a value?

why does groupby function returns duplicated data

Why does this query function returns incorrect results?

why does an external function that takes a class object as a parameter calls a move constructor when it returns the same object

Javscript why does String interpolation for single $ and double $$ have same result in replace function

JavaScript Why does string interpolation for single $ and double $$ have same result in replace function?

Why does the function not execute a specific line

how to write a function, it will do the same thing with replace()

Why does this function work but still returns the typeError ... is not a function?

Why does my string.strip() function in python fails to strip other characters along the same line?

Why does it show that my function returns an int when it returns a char*?

Python: Create list from function that returns single item or another list

Why are the changes made to single pointer persists even after the function returns

Why my Array find function doesn't work properly even I did the same thing document

If I have a function does basically the same thing as another, what would be the best way to join them?

Why does C language allow users to create Macros whose name are the same as a pre-existing library function?

Why does function not create new dataset?

Why does a JavaScript function that returns an object does it by reference (to its arguments)?

create a function that returns an object

Is function literal the same thing as Lambda expression and anonymous function?

Why function returns None?

why this function returns null?