Tips for refactoring code

Hi,

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

1 Like

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.

5 Likes

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.

I agree! Coming from the Java/Scala world I really miss the automatic refactoring of IntelliJ and/or Eclipse. Any decent environment should include “rename”, “move”, “change signature”, “extract function”, “inline” etc. And it is not just search and replace-- “extract function” for example finds all the equivalent blocks of code, and replaces them with a call to a function. “Move” for Julia would move a function to a different module, and fix all the places that reference it. “Change signature” changes the arguments and fixes all the places that call that function. Such tools are the central to the “refactor mercilessly” strategy of maintaining clean code http://www.extremeprogramming.org/rules/refactor.html

2 Likes

I suspect this wasn’t the case when this question was asked, but the Julia extension for VS Code now provides great renaming functionality, very similar to how JetBrains products’ refactor->rename command works.

I thought is was worth reviving this topic, since this page seems to be in the top search results for people wondering how to get this kind of functionality from a Julia IDE.

Also, see the following link, it appears that the Julia devs are switching from Juno to VS Code, so VS Code is probably the best option for most users at this point: https://devclass.com/2020/07/30/juno-julia-for-vscode-1_0/

2 Likes

Thanks for the tip. I’m using VS code but I didn’t know about that option.

As a debrief about one year later, I pretty quickly found a good workflow in which the lack of refactoring tools was more than well made up for by the language itself being so easy to use.

It is hard to put a finger on it without writing a whole blogpost, but the combination of dynamic typing (and the awesome type system in general) and reliance on class based programming seem to make refactoring tools a lot less necessary.

I still use IntelliJ and eclipse quite a bit so it’s not like I have gotten used to not having refactoring tools either.