---
---
# Style Guide
This style guide is adapted from [Invenia](https://invenia.ca/labs/)'s style guide. We would like to thank them for allowing us to access and use it. Please don't let not having read it stop you from contributing to Turing! No one will be annoyed if you open a PR whose style doesn't follow these conventions; we will just help you correct it before it gets merged.
These conventions were originally written at Invenia, taking inspiration from a variety of sources including Python's [PEP8](http://legacy.python.org/dev/peps/pep-0008), Julia's [Notes for Contributors](https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md), and Julia's [Style Guide](https://docs.julialang.org/en/latest/manual/style-guide/).
What follows is a mixture of a verbatim copy of Invenia's original guide and some of our own modifications.
## A Word on Consistency
When adhering to this style it's important to realize that these are guidelines and not rules. This is [stated best in the PEP8](http://legacy.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds):
> A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is most important.
> But most importantly: know when to be inconsistent – sometimes the style guide just doesn't apply. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!
## Synopsis
Attempt to follow both the [Julia Contribution Guidelines](https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md#general-formatting-guidelines-for-julia-code-contributions), the [Julia Style Guide](https://docs.julialang.org/en/latest/manual/style-guide/), and this guide. When convention guidelines conflict this guide takes precedence (known conflicts will be noted in this guide).
* Use 4 spaces per indentation level, no tabs.
* Try to adhere to a 92 character line length limit.
* Use upper camel case convention for [modules](https://docs.julialang.org/en/latest/manual/modules/) and [types](https://docs.julialang.org/en/latest/manual/types/).
* Use lower case with underscores for method names (note: Julia code likes to use lower case without underscores).
* Comments are good, try to explain the intentions of the code.
* Use whitespace to make the code more readable.
* No whitespace at the end of a line (trailing whitespace).
* Avoid padding brackets with spaces. ex. `Int64(value)` preferred over `Int64( value )`.
## Editor Configuration
### Sublime Text Settings
If you are a user of Sublime Text we recommend that you have the following options in your Julia syntax specific settings. To modify these settings first open any Julia file (`*.jl`) in Sublime Text. Then navigate to: `Preferences > Settings - More > Syntax Specific - User`
```json
{
"translate_tabs_to_spaces": true,
"tab_size": 4,
"trim_trailing_white_space_on_save": true,
"ensure_newline_at_eof_on_save": true,
"rulers": [92]
}
```
### Vim Settings
If you are a user of Vim we recommend that you add the following options to your `.vimrc` file.
```
set tabstop=4 " Sets tabstops to a width of four columns.
set softtabstop=4 " Determines the behaviour of TAB and BACKSPACE keys with expandtab.
set shiftwidth=4 " Determines the results of >>, <<, and ==.
au FileType julia setlocal expandtab " Replaces tabs with spaces.
au FileType julia setlocal colorcolumn=93 " Highlights column 93 to help maintain the 92 character line limit.
```
By default, Vim seems to guess that `.jl` files are written in Lisp. To ensure that Vim recognizes Julia files you can manually have it check for the `.jl` extension, but a better solution is to install [Julia-Vim](https://github.com/JuliaLang/julia-vim), which also includes proper syntax highlighting and a few cool other features.
### Atom Settings
Atom defaults preferred line length to 80 characters. We want that at 92 for julia. To change it:
1. Go to `Atom -> Preferences -> Packages`.
2. Search for the "language-julia" package and open the settings for it.
3. Find preferred line length (under "Julia Grammar") and change it to 92.
## Code Formatting
### Function Naming
Names of functions should describe an action or property irrespective of the type of the argument; the argument's type provides this information instead. For example, `buyfood(food)` should be `buy(food::Food)`.
Names of functions should usually be limited to one or two lowercase words. Ideally write `buyfood` not `buy_food`, but if you are writing a function whose name is hard to read without underscores then please do use them.
### Method Definitions
Only use short-form function definitions when they fit on a single line:
```julia
# Yes:
foo(x::Int64) = abs(x) + 3
# No:
foobar(array_data::AbstractArray{T}, item::T) where {T<:Int64} = T[
abs(x) * abs(item) + 3 for x in array_data
]
# No:
foobar(
array_data::AbstractArray{T},
item::T,
) where {T<:Int64} = T[abs(x) * abs(item) + 3 for x in array_data]
# Yes:
function foobar(array_data::AbstractArray{T}, item::T) where T<:Int64
return T[abs(x) * abs(item) + 3 for x in array_data]
end
```
When using long-form functions [always use the `return` keyword](https://groups.google.com/forum/#!topic/julia-users/4RVR8qQDrUg):
```julia
# Yes:
function fnc(x::T) where T
result = zero(T)
result += fna(x)
return result
end
# No:
function fnc(x::T) where T
result = zero(T)
result += fna(x)
end
# Yes:
function Foo(x, y)
return new(x, y)
end
# No:
function Foo(x, y)
new(x, y)
end
```
Functions definitions with parameter lines which exceed 92 characters should separate each parameter by a newline and indent by one-level:
```julia
# Yes:
function foobar(
df::DataFrame,
id::Symbol,
variable::Symbol,
value::AbstractString,
prefix::AbstractString="",
)
# code
end
# Ok:
function foobar(df::DataFrame, id::Symbol, variable::Symbol, value::AbstractString, prefix::AbstractString="")
# code
end
# No:
function foobar(df::DataFrame, id::Symbol, variable::Symbol, value::AbstractString,
prefix::AbstractString="")
# code
end
# No:
function foobar(
df::DataFrame,
id::Symbol,
variable::Symbol,
value::AbstractString,
prefix::AbstractString="",
)
# code
end
```
### Keyword Arguments
When calling a function always separate your keyword arguments from your positional arguments with a semicolon. This avoids mistakes in ambiguous cases (such as splatting a `Dict`).
```julia
# Yes:
xy = foo(x; y=3)
# No:
xy = foo(x, y=3)
```
### Whitespace
Avoid extraneous whitespace in the following situations:
* Immediately inside parentheses, square brackets or braces.
```julia
Yes: spam(ham[1], [eggs])
No: spam( ham[ 1 ], [ eggs ] )
```
* Immediately before a comma or semicolon:
```julia
Yes: if x == 4 @show(x, y); x, y = y, x end
No: if x == 4 @show(x , y) ; x , y = y , x end
```
* When using ranges unless additional operators are used:
```julia
Yes: ham[1:9], ham[1:3:9], ham[1:3:end]
No: ham[1: 9], ham[1 : 3: 9]
```
```julia
Yes: ham[lower:upper], ham[lower:step:upper]
Yes: ham[lower + offset : upper + offset]
Yes: ham[(lower + offset):(upper + offset)]
No: ham[lower + offset:upper + offset]
```
* More than one space around an assignment (or other) operator to align it with another:
```julia
# Yes:
x = 1
y = 2
long_variable = 3
# No:
x = 1
y = 2
long_variable = 3
```
* Always surround these binary operators with a single space on either side: assignment ($=$), [updating operators](https://docs.julialang.org/en/latest/manual/mathematical-operations/#Updating-operators-1) ($+=$, $-=$, etc.), [numeric comparisons operators](https://docs.julialang.org/en/latest/manual/mathematical-operations/#Numeric-Comparisons-1) ($==$, $<$, $>$, $!=$, etc.). Note that this guideline does not apply when performing assignment in method definitions.
```julia
Yes: i = i + 1
No: i=i+1
Yes: submitted += 1
No: submitted +=1
Yes: x^2 < y
No: x^2