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.