Tips for refactoring code


I wonder if there are any good ways to refactor julia code (or workflows which reduces the labour)?

I’m trying to do TDD but with my current setup I need to update at least three files even for a trivial thing like a name change: 1) The actual source code, 2) my module file with the exports 3) the test case file.

This is taking alot of fun out of it and I keep thinking I must be doing something wrong.

In case it matters I’m currently using juno and I kinda like it (except for the topic of this post).

I have searched for things like “julia refactoring” but I only found very specific things (like “how do I refactor this code into xxx”.

I usually put the exports at the top of the file that (first) defines a function.

Any decent editor should be able to search & replace in multiple files.

That said, renames are usually the cheapest when refactoring.

I believe you have to be a bit more specific about the refactoring. If it amounts to renaming, it is a trivial task, as @Tamas_Papp just mentioned. There are some small problems there as well related to the alignment of arguments etc.

A slightly more complicated refactor is related to splitting functionality in files, modules etc and there you have to be more specific.

I remember a very good talk on a Java developers conference (or a PyCon) a few years back where a guy what talking about coding for minimal future refactor work, you may try searching for it on youtube. Very similar to this

I have this piece of code that does method extraction. It looks at the AST and try to figure out what variables are not assigned and will generate a function signature for it. Even though it’s a bit buggy I find it very useful, you can write code in the global scope and when you are happy with it just press ctrl-e and turn in into a function. It’s one of the thing I miss when using Juno.

That said I should try to rewrite it using MLStyle because it’s not pretty and sometimes just fails.

If you’re asking “can I easily set up the canonical package structure?” the answer is PkgTemplates.


Thanks alot for all the answers.

Sorry about not being very specific. It is just a general feeling I’m doing something wrong as I constantly find myself realizing stuff like I forgot to export the new method name after spending some 30 seconds to parse a stacktrace from a failed testcase after a round of refactoring. Some of it is probably just about getting used to the language overall (I really like it so far!) though.

I guess I was secretly hoping there was a plugin I forgot to install that would give access to options similar to the ones in eclipse or IntelliJ :slight_smile:

Doing (regex) replace in multiple files is of course workable in some cases. What I remember from it before I was spoiled by IDEs was that one still needs to be a bit careful to not accidentally mess up other parts (especially comments and documentation).

I will take a look at that Refactoring module as and the refactoring talk as well.

I did find that excellend PkgTemplates package and found it very useful.