# Static Analysis vs. Unit Tests

**URL:** <https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477>\
**Category:** Community\
**Created:** [November 20, 2023, 4:38pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477 "2023-11-20T16:38:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 20, 2023, 4:38pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/1 "2023-11-20T16:38:23Z")

</div>

> [@What don't you like about Julia for "serious work"?](https://discourse.julialang.org/t/what-dont-you-like-about-julia-for-serious-work/54591/172):
>
> After trying out [JET.jl](https://juliahub.com/ui/Packages/JET) thoroughly in a larger project, I find that it doesn’t really help much. I still find a lot of “not found” errors only by covering the specific code (i.e., [JET.jl](https://juliahub.com/ui/Packages/JET) missed them) that in C/C++ would have lighted up while I type… 😞

That sounds like not enough unit tests.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 20, 2023, 4:43pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/2 "2023-11-20T16:43:00Z")

</div>

That’s rather beside the point — one of the goals of static analysis in general is to catch errors of this sort without (or in addition to) unit tests.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 20, 2023, 4:44pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/3 "2023-11-20T16:44:27Z")

</div>

If you responded to me: my point was that it is so much better to have unit tests, because correctness of the code is not just about typos, but mainly about the internal logic.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 20, 2023, 4:53pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/4 "2023-11-20T16:53:24Z")

</div>

I feel like you’re missing the point - typo detection is valuable while writing the very code you don’t/can’t yet test. It might also take quite a long time to run that kind of testsuite, which is very counterproductive when you’re still writing code; you want to iterate fast, and that includes being reasonably sure that your unit tests test functionality and not whether you hit the wrong key somewhere half an hour ago.

In other languages (much more common in software engineering circles than Julia), this kind of early feedback is invaluable, and a large part why the “incumbents” in software are who they are.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 20, 2023, 4:55pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/5 "2023-11-20T16:55:20Z")

</div>

Perhaps. Or, you are writing chunks of code which are way too big and should be broken up into testable pieces.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 20, 2023, 4:58pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/6 "2023-11-20T16:58:09Z")

</div>

There is no need to blame a user of the language here. No code is small enough to prevent typos from accidentally creeping in - be it capitalization, writing `8` instead of `(` (by mistiming pressing SHIFT on my keyboard, for example) or simply hitting the wrong key by slipping to a neighboring one.

Unit tests don’t help here, they are not a solution for this kind of problem. This is something an IDE should be able to report immediately, as is the case in other languages (some of which are much, MUCH more verbose/require more boilerplate than Julia).

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 20, 2023, 5:24pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/7 "2023-11-20T17:24:48Z")

</div>

No one is blaming anyone here! The point is valid: no IDE will be able to catch logical errors. And unit tests are a necessity. So why not do them right, which will prevent typos popping up much later in production code.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [November 20, 2023, 5:27pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/8 "2023-11-20T17:27:27Z")

</div>

I think the kind of error being discussed here is not the same that is really appropriate to catch in a unit test (or even have a unit test for at all)

> [@PetrKryslUCSD](#):
>
> logical errors

exactly, these are not really “logical” errors so much as “obvious” errors. not obvious in the sense that committing one is any reflection on the user’s skill level, but obvious in the sense that the user will likely be able to fix it within seconds of noticing.

nobody in the world is writing a unit test to “make sure I spelled this function correctly.”

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 20, 2023, 5:30pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/9 "2023-11-20T17:30:02Z")

</div>

That is one of the purposes of UT in Julia: it is a dynamic language. Therefore writing `a1 = 1` and `a_1 = 1` (the first is missing an underscore) is not an obvious error. It may be another variable being introduced. Logical error? Find it with a UT. No IDE will be able to help you here.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [November 20, 2023, 5:31pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/10 "2023-11-20T17:31:13Z")

</div>

yes, you have identified a type of error that IDEs cannot help with

not sure what relevance that has to the types of errors that IDEs can

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 20, 2023, 5:38pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/11 "2023-11-20T17:38:14Z")

</div>

I think you’re still missing my point: no ide will be able to catch all errors, therefore UTs are essential, and when done well, they can quickly and efficiently catch _all_ errors.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [November 20, 2023, 5:44pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/12 "2023-11-20T17:44:18Z")

</div>

who said anything about catching _all_ errors?

- there are errors that IDEs can catch
- it is useful to the user when said errors are caught

do we disagree on either of these ?

---

<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:** [November 20, 2023, 5:49pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/13 "2023-11-20T17:49:38Z")

</div>

Even for an interactive language, catching some “maybe-not-errors” immediately is a useful thing, even if they’re not as solid as unit tests. For example,

```julia
julia> pie = 3.14
3.14

julia> circumference(diameter) = diameter*pue
circumference (generic function with 1 method)

```

would be nice if I was warned that I have yet to define `pue` in the global scope, even before I throw an actual error in a call. If this was in a source file

```julia
pie = 3.14

circumference(diameter) = diameter*pue

```

then the additional assumption of best practices can highlight `pue` and say it wasn’t defined in the file yet. I could even have an option to toggle highlighting of global variables if I’m trying to avoid them for performance reasons, even if it’s valid code. Unconditional errors aren’t the only things that should be brought to our attention.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [November 20, 2023, 6:30pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/14 "2023-11-20T18:30:10Z")

</div>

> [@Benny](#):
>
> then the additional assumption of best practices can highlight `pue` and say it wasn’t defined in the file yet.

It looks like the VS Code linter already treats references to global variables as missing references:

![image](https://global.discourse-cdn.com/julialang/original/3X/8/a/8aff93e91570105bda537cb0a43f1870bc78d243.png)

That example is from a vanilla Julia script, i.e. that file is not contained in a module, so the definition of `circumference` is technically valid. As far as I know, there is no linter option to only highlight missing local references when the method definition is inside a module.

---

<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:** [November 20, 2023, 6:33pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/15 "2023-11-20T18:33:47Z")

</div>

Right, so I actually am not sure what scenario prompted the original complaint about JET, not enough specifics to tell if a linter can handle it. Point was it’s not a bad thing to have several tools to catch different things at different times.

---

<div class="post-metadata">

**Author:** ![DOT](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DOT](https://discourse.julialang.org/u/DOT)\
**Post date:** [November 21, 2023, 2:24pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/16 "2023-11-21T14:24:12Z")

</div>

My point is, I’d expect JET.jl to find the obvious mistakes, so that I can save my unit-test-writing time on the actual functionality.

---

<div class="post-metadata">

**Author:** ![DOT](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@DOT](https://discourse.julialang.org/u/DOT)\
**Post date:** [November 21, 2023, 2:35pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/17 "2023-11-21T14:35:14Z")

</div>

Thanks! This is exactly why I’m avoiding Julia now. Currently, I don’t see the advantage of a dynamic language over one that catches my mistakes.

The language not enforcing declaration of variables just clashes with my basic tendency to make many mistakes when I type code. The set of variables that are in scope being determined at runtime – that’s always been a nightmare characteristic of Julia (and Python) for me.

---

<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:** [November 21, 2023, 3:20pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/18 "2023-11-21T15:20:11Z")

</div>

> [@DOT](#):
>
> The language not enforcing declaration of variables just clashes with my basic tendency to make many mistakes when I type code. The set of variables that are in scope being determined at runtime

Local variables are definitely set in stone at lower-time. What exactly is the sort of mistake you’re referring to, it wasn’t made explicit earlier despite people reaching more or less the same guess. Maybe a MWE? It’ll be important to verify whether JET.jl is even the suitable tool to catch such mistakes; JET.jl specifically deals with type inference and method dispatch issues in function calls, not bugs in general.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 21, 2023, 3:28pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/19 "2023-11-21T15:28:23Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [November 21, 2023, 3:32pm UTC](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477/20 "2023-11-21T15:32:11Z")

</div>

> [@PetrKryslUCSD](#):
>
> So what exactly do you want to accomplish here?

the original thread was “What don’t you like about Julia for serious work”

I think to get a valuable discussion out of that prompt, it’s important to solicit the opinions of those who in fact do not like Julia for serious work.

Hence, what @DOT is accomplishing is the task set out in the OP: to provide a perspective on why one might not choose Julia

[Next page](https://discourse.julialang.org/t/static-analysis-vs-unit-tests/106477.md?page=2)
