Skip to content

REPL based workflow

Francesc Verdugo edited this page Sep 17, 2019 · 7 revisions

Intro

This is a recommended workflow in order to boost productivity, when writing julia code using a conventional text editor (e.g., vim, emacs, gedit) and executing it via the Julia REPL (Read, Evaluate, and Print Loop).

Follow this rule

The rule is very simple: DO NOT CLOSE THE JULIA REPL UNTIL IT IS STRICTLY NEEDED. Each time you close the REPL most of the just-in-time (JIT) compiled code will be deleted. The next time you open the REPL all your code will be JIT-compiled again, causing a significant overhead for large projects.

A simple scenario

Let us assume that we are coding in a file code.jl and we wand to execute it, see results, fix problems, execute it again, etc. The way you don't want to do this is using the Unix command line as follows:

$ julia code.jl #Execute code. Then, see results, edit code
$ julia code.jl #Execute code. Then, see results, edit code
# etc.

The proper way to to this is to open the Julia REPL only once and include code as many times as you want.

$ julia # Open REPL
julia> include("code.jl") #Execute code. Then, see results, edit code
julia> include("code.jl") #Execute code. Then, see results, edit code
# etc

In this way, most of the code will be JIT-compiled only the first time you include it, boosting your productivity. In many cases it is recommended to enclose all the code in the file code.jl in a module. With this, we isolate each execution of the code.

Adding new functionality to a project

Say you want to add new functionality to a project called MyProject. The effective way to do this is via test-driven development. Write the new functionality and the code that tests it in two files (e.g.newstuff.jl newstufftest.jl) typically in the test folder of your project. Then, open a REPL

julia> using MyProject
julia> include("newstuff.jl"); include("newstufftest.jl") # Test new code, see results, edit code
julia> include("newstuff.jl"); include("newstufftest.jl") # Test new code, see results, edit code
# Etc.

The key is to develop the new code without changing anything in the project. If you change the something inside the project, you would need to close the REPL and open again, needing to JIT-compile a lot of code again. Once the new functionality is working, you can move it to the project and keep the tests in the test folder.

Editing small parts of the project

In some cases, however, it is required to edit small parts of the code to fix bugs, etc. Instead to close the REPL each time you modify something, you can use the Revise.jl package. Revise will make the changes visible in the REPL without the need to close it again and again.

pkg> add Revise # Install it the first time
julia> using Revise
julia> using MyProject
# Edit code in the project. The changes will be available in the REPL automatically

Revise is a very useful tool, but cannot deal with arbitrary changes in the code. Typically, it works fine if you only modify code inside function bodies. If you have to change the code structure a lot, it is better to comment out the concerned code and proceed as if you were adding new functionality as explained above.