Style/convention rules for SciBmad
- Type stability as much as possible! Use the macro
@report_opt
inJET.jl
to aid in writing good code - Two (2) spaces should be uses for tabs. No tab characters! (in most text editors you can set the tab length to two spaces)
- CamelCase for modules and structs
- A file which contains a module should have the file name be the same as the module name: e.g.
Module MyModule
should be inMyModule.jl
). - Non-module files should be snake_case
- Global constants should be capitalized SNAKE_CASE
- Longer function names should be snake_case, short ones could go either way (e.g.
set_units!
andsetunits!
are both ok, buttrack_a_drift
should be used instead oftrackadrift
). Exception: struct constructors should have the same name as the struct and will therefore inherit the CamelCase struct name. - Bang (!) at end of function that mutates any argument per Julia standard
- For functions with many arguments/keyword arguments, format the arguments like:
function Coords(n::Integer;
d_x::Distribution=Normal(0,0), d_px::Distribution=Normal(0,0),
d_y::Distribution=Normal(0,0), d_py::Distribution=Normal(0,0),
d_z::Distribution=Normal(0,0), d_pz::Distribution=Normal(0,0) )
# stuff
end
- In struct definitions, use concrete-types as much as possible, and try to opt for parametric types if not fully possible. E.g. instead of
AbstractString
, useString
and forTPS
which could representFloat64
orComplexF64
, usingTPS{Float64}
andTPS{ComplexF64}
(parametric types) instead of two separate types - Avoid specifying types as much as possible unless that argument must be a specific type. This is for composability with the rest of the Julia environment. Type-stable functions will then incur no cost. E.g.,
AbstractString
should be used instead ofString
for function arguments (e.g.function foo(mystring::AbstractString)
instead offunction foo(mystring::String)
- All exports should be included at the top of the main package file in the module. See
GTPSA.jl
for an example - File names should concisely represent what's in those files. E.g. a file containing all type definitions might be called
types.jl
, and a file containing all constructors for a typeType
might betype_ctors.jl
or justctors.jl
. Directory structure could be good too,type/ctors.jl
- Mark internal functions, structs, etc. with the string "Internal:" For example
"""
Internal: my_func(...)
"""
- Document the return type of functions using "-> " suffix. For example:
"""
Internal: my_func(...) -> Int
"""