github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/docs/conventions.md (about) 1 # Conventions 2 3 This document will list conventions that this repo should follow. These are 4 the guidelines, and if you believe that one should not be followed, please state 5 why in your PR. If you believe that a piece of code does not follow one of the 6 conventions listed, please open an issue before making any changes. 7 8 When submitting a new convention, please open an issue for discussion, if 9 possible please highlight parts in the code where this convention could help the 10 code readability or simplicity. 11 12 ## Avoid named return parameters 13 14 func example(test int) (num int) { 15 a = test + 1 16 num = a * test 17 return 18 } 19 20 In the above function we have used a named return parameter, which allows you to 21 include a simple return statement without the variables you are returning. This 22 practice can cause confusion when functions become large or the logic becomes 23 complex, so these should be avoided. 24 25 ## Use error wrapping 26 27 Bad: 28 ``` 29 err = SomeAPI() 30 if err != nil { 31 return fmt.Errorf("something bad happened: %v", err) 32 } 33 ``` 34 35 Good: 36 ``` 37 err = SomeAPI() 38 if err != nil { 39 return fmt.Errorf("something bad happened: %w", err) 40 } 41 ``` 42 43 Error wrapping allows `errors.Is` and `errors.As` usage in upper layer 44 functions which might be useful.