# \`local\` just in case: good idea or code smell?

**URL:** https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668
**Category:** General Usage
**Tags:** scope, while-for-scope
**Created:** [June 14, 2024, 6:40pm UTC](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668 "2024-06-14T18:40:46Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [June 14, 2024, 6:40pm UTC](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668/1 "2024-06-14T18:40:46Z")

</div>

I often write `local` to ensure a variable does not accidently reuse another definition such as in the example below. Is that a good idea, or is there something more sophisticated I should be doing to test local scoping?

```julia
function f(input)
  ...
  p = plot(...)
  for i in input.n
      local a = ...
      local b = ...
      local c = ...
      local x = ...
      local y = ...
      local z = ...
      plot!(p, x, y, z)
  end
  ...
  return MyType(..., p, ...)
end

```

---

<div class="post-metadata">

### Author: ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)
#### Post date: [June 14, 2024, 7:22pm UTC](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668/2 "2024-06-14T19:22:06Z")

</div>

This is a little abstract, but for the case you seemed to have sketched out, why not just write a new function to call in the loop?

```julia
for i in input.n
    do_my_thing!(p, i)
end

```

You can declare all your locals `a, b, c, ...` in `do_my_thing!` and have no worries about polluting your outer function scope then.

---

<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 14, 2024, 7:37pm UTC](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668/3 "2024-06-14T19:37:47Z")

</div>

> [@Nathan\_Boyer](#):
>
> is there something more sophisticated

I guess that’s `let`:

```julia
  for i in input.n
      let a = ...,
          b = ...,
          c = ...,
          x = ...,
          y = ...,
          z = ...
          plot!(p, x, y, z)
       end
  end

```

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [June 14, 2024, 7:39pm UTC](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668/4 "2024-06-14T19:39:45Z")

</div>

That feels to me like a weird thing to break out into its own function. I can’t call it `plot_my_data` because the setup for the plot is outside the loop. I would have to call it like `add_data_to_p_plot!` and move its definition outside the main function, away from the external related lines.

I guess the main reason probably comes down to function naming. I could move everything starting from `p=plot(...)` to its own function, but it would have to be named `plot_some_aspect_of_my_data`, since `f` in my actual code is already basically named `plot_my_data`.

The other scenario in which I use this pattern is if I need to calculate another index based on `i` like `local j = 3(i-1)+1`. In this case, I need that name `j` locally for several subsequent calls using outer variables, so I don’t think it makes sense as a separate function.

In any case, making up a new function name any time I need a `for` loop sounds difficult.

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [June 14, 2024, 8:21pm UTC](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668/5 "2024-06-14T20:21:18Z")

</div>

Cannot remember that I ever used `local` … but would probably consider the fact that the function has grown so long that I need to restrict variable bindings to a smaller subpart of it as the code smell – rule of thumb: don’t need to scroll to see whole function.

---

<div class="post-metadata">

### Author: ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)
#### Post date: [June 14, 2024, 8:23pm UTC](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668/6 "2024-06-14T20:23:49Z")

</div>

To a certain extent, I guess this is a matter of style, and for me personally, one of the big reasons I switched from matlab to julia was that small-function overhead was so huge in matlab (at least at the time, not sure what it’s like now), which gave me a lot of the problems you describe, but with julia I just make a bunch of little functions.

Perhaps a related problem is that there are so many local variables in scope you don’t feel confident you’re not overwriting something important, perhaps these could be organized into something a bit more structured? Say that your `x`, `y`, and `z` represent coordinates. You could then write

```julia
for i in input.n
    coords = calculate_coordinate_set_i(i)
    plot!(p, coords.x, coords.y, coords.z)
end

```

If other variables across your outer function are similarly contained in `struct`s, `NamedTuple`s, or what-have-you with descriptive names, it seems you could use small variable names like `j` for indices without it overwriting anything important.

Ultimately though, if this is just you plotting things, and you found a system that works for you, I’d say you’re all good.

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [June 14, 2024, 8:25pm UTC](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668/7 "2024-06-14T20:25:34Z")

</div>

Still working on breaking my procedural Matlab habits I think lol

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [June 14, 2024, 9:51pm UTC](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668/8 "2024-06-14T21:51:34Z")

</div>

Perhaps bunching the local declerations together is easier on the eyes:

```julia
function f(input)
  ...
  p = plot(...)
  for i in input.n
      local a,b,c,x,y,z
      a = ...
      b = ...
      c = ...
      x = ...
      y = ...
      z = ...
      plot!(p, x, y, z)
  end
  ...
  return MyType(..., p, ...)
end

```

---

<div class="post-metadata">

### Author: ![savq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/savq/32/22063_2.png) [@savq](https://discourse.julialang.org/u/savq)
#### Post date: [June 16, 2024, 3:55am UTC](https://discourse.julialang.org/t/local-just-in-case-good-idea-or-code-smell/115668/9 "2024-06-16T03:55:03Z")

</div>

> Perhaps bunching the local declerations together is easier on the eyes:

I would recommend against this. I used to do this in Lua a lot, but it’s easy for the declarations and the assignments to get out of sync, making refactoring harder.

`let` is basically the same, without having to write the variable names twice.
