# Is there a linter to help write good Mathematica code?

I don't do much real programming, but I've recently been turned on to Atom, which has linter plugins for over 50 languages to make coding easier (most drawing on pre-existing lint-like programs).

In other languages, linters help me learn about operator precedence (unnecessary parentheses), good programming styles/techniques, and get warnings about code that may not have been intended. Is there something along these lines for Mathematica?

If not, is there a reason why not? At first glance it seems like a number of the suggestions/tips I pick up from reading answers here could be automated. For example, maybe the Mathematica linter would remind you to memoize your functions with f[n_]:=f[n]= if that setting is turned on.

Edit: For a couple days in November 2016 there was work on a linter for Atom for Mathematica on github, but the project seems to have died quickly and I couldn't get it to work.

• I wonder if you could make something like this get your features? mathematicaplugin.halirutan.de – Moo Apr 16 '16 at 12:11
• How is this different from the little popups that appear over each command when you hover the mouse? Or the "context sensitive" help? – bill s Apr 16 '16 at 14:14
• Like bill s I would say that a very strong capability along the lines you describe already exists in Mathematica. – m_goldberg Apr 16 '16 at 15:42
• I don't know of one, but it would be cool to have warnings for private symbols that start with an uppercase letter, implicit multiplications at line boundaries, and unlocalized iterator variables in Table constructs within functions (just to name the first few ideas that popped into my head). The pitfalls post would be an excellent source of inspiration for checks to implement. I wonder if a bit of spelunking would turn up a way to extend the front-end's existing bare-bone checks? – WReach Apr 16 '16 at 16:03
• @WReach Ideally this is what Workbench would do if version 3 ever comes out of beta. – Edmund Apr 16 '16 at 22:34

## 1 Answer

I am a developer at Wolfram Research and I have been working on linter technology that I think would be nice to share publicly.

There are 2 paclets on the public paclet server that you can download and use for finding problems with WL code.

CAVEAT: These paclets are experimental and not yet fully-formed, but they are constantly being worked on.

The two paclets you will need are AST and Lint:

In[1]:= PacletUpdate["AST","Site"->"http://pacletserver.wolfram.com","UpdateSites"->True]
Out[1]= Paclet[AST,0.8.1,<>]

In[2]:= PacletUpdate["Lint","Site"->"http://pacletserver.wolfram.com","UpdateSites"->True]
Out[2]= Paclet[Lint,0.8,<>]


The AST paclet provides functions for parsing WL code and returning syntax trees with interesting metadata attached, such as file and line information.

The Lint paclet uses AST and provides functions for scanning WL code and finding problems and reporting them.

Here is a demo of using the Lint paclet.

First, load the Lint package:

Needs["Lint"]


Next, take a snippet of code that you are interested in linting.

code = "Which[a, b, a, b]";


Use LintString to get a List of Lint objects:

lints = LintString[code];


The Lint objects are interesting by themselves, but it would be nice to see them formatted within lines of the source code.

LintStringReport[code, lints] // Column


The report shows formatted lines that you can mouse over to see the problem descriptions.

I included a screenshot to better show the result.

It is possible to also scan files with LintFile and LintFileReport.

The linter is very rudimentary right now, but it is constantly improving. It has already proven useful internally. Eventually I would like to solicit the community for more ideas for rules for the linter. I hope you enjoy using it!

• When you solicit the community, you may want to look at things like the current "hot meta post" Linting - Code analysis for bugs, errors and style issues in the Wolfram Language – Mark S. Mar 6 '19 at 1:38
• I figured out that LintFileReport does not give me all results because of the "SeverityExclusions" option, so that was an easy fix. Other comments: please support file names wrapped in File, and please add file name auto-completion to LineFile. – Szabolcs Mar 6 '19 at 11:03
• This seems really cool! Thanks for sharing. I look forward to future updates. In the meantime, let me start running this on my files... – ktm Mar 6 '19 at 17:00
• And, of course, the first thing I should have said is that having these tools is really great! Thanks for all your work. :-) – Szabolcs Mar 6 '19 at 19:24
• Big +1, Have you considered putting it e.g. on GitHub? @Szabolcs would have more space for issues :) – Kuba Mar 6 '19 at 22:35