Nice, make it simple.

On Friday, November 12, 2021 at 3:08:31 PM UTC+8 axel.wa...@googlemail.com 
wrote:

> I don't prefer either. I decide on a case-by-case basis. I generally 
> ignore the question of scope, though. The relevant question (to me) is 
> readability. If the statement is short, I use the one-line version, 
> otherwise (or if I need to use either result after the conditional) I use 
> the version with a separate statement. But that's a rule-of-thumb, as I 
> said, I decide case by case.
>
> On Fri, Nov 12, 2021 at 5:05 AM Kn <hit.zh...@gmail.com> wrote:
>
>> Hi, guys, I want to know which error handling pattern do you prefer. 
>> Following is a code snippet from go stdlib.
>>
>>
>> https://sourcegraph.com/github.com/golang/go/-/blob/src/net/http/h2_bundle.go?L1848
>>
>> Let me simplify my questions:
>>
>> Pattern1: like the code in go stdlib, in the same function, we first 
>> declare one error variable, then in the following if-statement we use 
>> one-liner pattern to declare a new error variable.
>>
>> ```go
>> func MyFunc() error {
>>   v, err := doSomething()
>>   if err != nil {
>>     return err
>>   }
>>   // stuff about processing `v`
>>
>>   if err := doAnotherthing(); err != nil {
>>     return err
>>   }
>>
>>   // stuff about processing ...
>> }
>> ```
>>
>> I think pretty code should not only be readable but also to conform to 
>> same code style. So I think why not use the already declared variable 
>> before. Then we have the following pattern.
>>
>> Pattern2: Firstly doSomething() returns multiple return values and we 
>> need processing `v` later, so we use `v, err := doSomething()` as before. 
>> But, I think the `error` encountered is special compared to other local 
>> variables, it represents a state of current function (success or failure). 
>> So I think only one error variable is enough, it can be used for indicating 
>> any error, no matter the error is generated from `doSomething()` or 
>> `doAnotherthing()`. 
>>
>> And, I didn't use the one-liner pattern which may be used for minimize 
>> the variable's scope. Some people think we should use one-line pattern here 
>> to conform to this rule.
>>
>> ```go
>> func MyFunc() error {
>>   v, err := doSomething()
>>   if err != nil {
>>     return err
>>   }
>>   // stuff about processing `v`
>>
>>   err := doAnotherthing()
>>   if err != nil {
>>     return err
>>   }
>>   
>>   // stuff about processing ...
>> }
>> ```
>>
>> Of course I know the rule to minimize the variable's scope, but I think 
>> error is special and consistent coding style is important and beautiful. I 
>> searched the go source code, these two patterns are both used.
>>
>> I want to know which pattern do you prefer and suggested.
>>
>> Thanks!
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to golang-nuts...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/2999eca8-1cc4-475c-8f85-0d2c8b966268n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/2999eca8-1cc4-475c-8f85-0d2c8b966268n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/34a54f5a-78e5-4f0f-bb64-72fd16d55e03n%40googlegroups.com.

Reply via email to