Iβd like to hear about what good ways people have found, too. When I do find accidental globals, I tend to wish I fixed them earlier than trying out a @code_warntype ... and find the glaring red type instabilities next to a MyModule.accidental_global. @code_warntype also isnβt always clear about accidentally shared locals despite the obvious type instabilities.
Tools for that do exist, Traceur.jl is even pointed out in the Manual in Juliaβs official documentation. Currently I prefer JET.jl, though:
julia> a = 1
1
julia> f(x) = a + x
f (generic function with 1 method)
julia> using JET
julia> @report_opt f(7)
βββββ 1 possible error found βββββ
β @ REPL[2]:1 Main.+(%1, x)
β runtime dispatch detected: Main.+(%1::Any, x::Int64)
Note that this is completely based on static analysis, but itβs possible to also use run time information with SnoopCompile.jl.
If we are talking about a module:
I try to use as few globals as possible. It also helps if your globals have βclearβ names, e.g. they could be capitalized or have a prefix global_
If we are talking about scripting: adding separate tests for each function is always helpful.