Update 'Style Guide'
parent
8db6d81d5c
commit
a752fe19e7
@ -46,9 +46,23 @@ err = parser.nextToken()
|
|||||||
if err != nil { return }
|
if err != nil { return }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
# Pass By Value, Not by Reference
|
||||||
|
|
||||||
|
Always pass by value and return by value unless there is an explicit semantic reason not to do so, or the data you are passing is exceptionally large in size. Stack memory is more performant, and passing by reference can add complexity to your code (and increase the likliehood of annoying segfaults).
|
||||||
# Returning Values
|
# Returning Values
|
||||||
|
|
||||||
Golang lets you return multiple values from functions, and it also allows you to give them names. When writing code for the compiler, all function
|
Golang lets you return multiple values from functions, and it also allows you to give them names. When writing a function, you should name all return values, even if there is only one of them. Always return values by *setting* your returns and then using a naked return statement like this:
|
||||||
|
|
||||||
|
```
|
||||||
|
func someFunction () (value int, err error) {
|
||||||
|
_, value, err = otherFunction(5)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
View the return statement like a `break` statement, but for functions.
|
||||||
|
|
||||||
|
Some may claim that using naked returns like this is a bad idea, but it is incredibly useful when parsing/lexing things, as if you encounter an error you can return *what you have so far*. Thus, this is the dominant pattern in the ARF compiler.
|
||||||
|
|
||||||
# Nesting
|
# Nesting
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user