# How to detect globals (inadvertently used) in a function?

**URL:** <https://discourse.julialang.org/t/how-to-detect-globals-inadvertently-used-in-a-function/82011>\
**Category:** General Usage\
**Created:** [May 31, 2022, 7:10pm UTC](https://discourse.julialang.org/t/how-to-detect-globals-inadvertently-used-in-a-function/82011 "2022-05-31T19:10:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [May 31, 2022, 7:10pm UTC](https://discourse.julialang.org/t/how-to-detect-globals-inadvertently-used-in-a-function/82011/1 "2022-05-31T19:10:44Z")

</div>

There is probably a simple answer, but it has somehow escaped me.

Is there a way to list the variables used in a function? In particular, I want to spot any globals that are inadvertently used.

```julia
a = 1
fn1(x) = a + x #I really meant to make `a` an argument, but forgot. How do I catch this?

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [May 31, 2022, 7:18pm UTC](https://discourse.julialang.org/t/how-to-detect-globals-inadvertently-used-in-a-function/82011/2 "2022-05-31T19:18:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 1, 2022, 2:18pm UTC](https://discourse.julialang.org/t/how-to-detect-globals-inadvertently-used-in-a-function/82011/3 "2022-06-01T14:18:23Z")

</div>

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
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.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [June 2, 2022, 7:51am UTC](https://discourse.julialang.org/t/how-to-detect-globals-inadvertently-used-in-a-function/82011/4 "2022-06-02T07:51:57Z")

</div>

I also noticed that the _VS code debugger_ shows locals/globals when stepping into a function.

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 2, 2022, 8:21am UTC](https://discourse.julialang.org/t/how-to-detect-globals-inadvertently-used-in-a-function/82011/5 "2022-06-02T08:21:13Z")

</div>

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.
