# Tips for refactoring code

**URL:** https://discourse.julialang.org/t/tips-for-refactoring-code/24353
**Category:** Tooling
**Created:** [May 18, 2019, 1:10pm UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353 "2019-05-18T13:10:14Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)
#### Post date: [May 18, 2019, 1:10pm UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/1 "2019-05-18T13:10:15Z")

</div>

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”.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 18, 2019, 2:02pm UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/2 "2019-05-18T14:02:48Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)
#### Post date: [May 18, 2019, 2:26pm UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/3 "2019-05-18T14:26:24Z")

</div>

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](https://www.youtube.com/watch?v=6wDoopbtEqk)

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [May 18, 2019, 2:44pm UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/4 "2019-05-18T14:44:30Z")

</div>

I have this [piece of code](https://github.com/jonathanBieler/GtkIDE.jl/blob/master/src/Refactoring.jl#L112) 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](https://github.com/thautwarm/MLStyle.jl) because it’s not pretty and sometimes just fails.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [May 18, 2019, 3:24pm UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/5 "2019-05-18T15:24:27Z")

</div>

If you’re asking “can I easily set up the canonical package structure?” the answer is [PkgTemplates](https://github.com/invenia/PkgTemplates.jl).

---

<div class="post-metadata">

### Author: ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)
#### Post date: [May 18, 2019, 9:10pm UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/6 "2019-05-18T21:10:10Z")

</div>

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 🙂

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.

---

<div class="post-metadata">

### Author: ![peter.wolf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter.wolf/32/6018_2.png) [@peter.wolf](https://discourse.julialang.org/u/peter.wolf)
#### Post date: [January 6, 2020, 7:09pm UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/7 "2020-01-06T19:09:58Z")

</div>

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 [Refactor Mercilessly](http://www.extremeprogramming.org/rules/refactor.html)

---

<div class="post-metadata">

### Author: ![JamesK](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jamesk/32/8989_2.png) [@JamesK](https://discourse.julialang.org/u/JamesK)
#### Post date: [August 28, 2020, 5:10am UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/8 "2020-08-28T05:10:01Z")

</div>

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: [Bye-bye Juno, hello Julia for VS Code - IDE shifts form, reaches 1.0 • DEVCLASS](https://devclass.com/2020/07/30/juno-julia-for-vscode-1_0/)

---

<div class="post-metadata">

### Author: ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)
#### Post date: [August 28, 2020, 7:03am UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/9 "2020-08-28T07:03:22Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![fins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fins/32/43444_2.png) [@fins](https://discourse.julialang.org/u/fins)
#### Post date: [October 11, 2022, 8:44am UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/10 "2022-10-11T08:44:36Z")

</div>

Hi,

I’m very new to julia and have just started migrating some code from java, within the scope of a private project to learn the basics and get started.

However I’m _pretty_ astounded (and even genuinely shocked) by the (it seems) complete lack of decent tools or even an awareness of the need for such.

I totally agree with @peter.wolf: Coming from a java-ecosystem, I’m used to being able to rename variables, fields, methods, classes, files / changing the signatures of functions and any datastructures _once, anywhere_ in my code and having an ide propagate those changes to every corner within the scope of my project (as defined by the project-setup in the ide): This includes files of different extensions (.java, .html, .xml, .json, .properties, …) and even extends to commented-out portions of those files (which is, when the ide would be polling me with the result of a search and ask for a decision to include those or not).  
This has been common practice for the past 20 yrs. or so, within the java-world.

Please note, that merely using “find & replace” in a texteditor, without applying any semantic and syntactic knowledge is like falling back from using word on a laptop for textediting to using a mechanical typewriter - yes it works, but it could hardly be considered an acceptable alternative, once you had adopted word (or any other editor, back in the day).

(In the case of refactoring the _signature_ of functions or datatypes in general, using “find & replace” doesn’t even work - it’s no use, finding all mentions of `UInt64`, when you want to change just the type of one parameter of one function to `Int128` within a large project - just as one trivial example).

So all ranting aside: How do people tackle this (apart from the obvious: Be less verbose in your style, start thinking more about names, like we did before refactoring ever became feasible and be disciplined with your conventions from the start, etc, to reduce tedious overhead, later)?

**Which tools seem to be most promising wrt. to these issues, atm?**

I’m using `vs code`, which _does_ recognize the option of refactoring (shows an entry in a contextmenu for it, when I select a variable, function, type, etc.), but then follows up with a `no refactorings available`, when I select that option. 😬

---

<div class="post-metadata">

### Author: ![fins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fins/32/43444_2.png) [@fins](https://discourse.julialang.org/u/fins)
#### Post date: [October 11, 2022, 9:04am UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/11 "2022-10-11T09:04:17Z")

</div>

Addendum: I _do_ recognize, it is not trivial, having (some of) these features for julia, in a way, that truely manages dynamic typing and multiple dispatch, but there has to be a way, to support some basic refactoring, at least for local names and static types, within a certain scope (project, package, module, function or whatever other unit of work).

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [October 11, 2022, 10:44am UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/12 "2022-10-11T10:44:33Z")

</div>

> [@jonathanBieler](#):
>
> I have this [piece of code](https://github.com/jonathanBieler/GtkIDE.jl/blob/master/src/Refactoring.jl#L112) 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.

It’s great that there’s any such code available, reminds me, while I should use an editor/VS Code IDE more/always, since this takes a string, is there any way to get code of functions back that you’ve just typed into the REPL (with comments, as is)? I know it’s in the edit history, and you can search it, just thinking if the code is only stored in (some) lowered form, or something like could be available:

```julia
@code_original my_func

```

> [@JamesK](#):
>
> the Julia extension for VS Code now provides great renaming functionality, very similar to how JetBrains products’ refactor-\>rename command works.

I need to check it out, it’s just renaming, not extract method or more advanced I hear of for other languages, Java?

---

<div class="post-metadata">

### Author: ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)
#### Post date: [October 11, 2022, 9:00pm UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/13 "2022-10-11T21:00:11Z")

</div>

I found this very frustrating to begin with. However, as I adopted a standard package structure (like using [PkgTemplates.jl](https://invenia.github.io/PkgTemplates.jl/stable/)) helps the language server in VS code know what symbols you are using which actually lets you do symbol refactoring across multiple files. I have found that having a separate environment to your package, such as in a `test` or `scripts` subdirectory and then `dev`ing your package lets the intellisense work properly. The only issue is that sometimes you need to restart the language server if your changes aren’t updated.

I’ve not really found a robust/standard workflow or tooling option that isn’t slightly frustrating, and it’s my least favourite part about using Julia.

---

<div class="post-metadata">

### Author: ![fins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fins/32/43444_2.png) [@fins](https://discourse.julialang.org/u/fins)
#### Post date: [October 12, 2022, 4:42pm UTC](https://discourse.julialang.org/t/tips-for-refactoring-code/24353/14 "2022-10-12T16:42:09Z")

</div>

> […] helps the language server in VS code know what symbols you are using which actually lets you do symbol refactoring across multiple files. I have found that having a separate environment to your package, such as in a `test` or `scripts` subdirectory and then `dev` ing your package lets the intellisense work properly […]

Hmmm, thx for this feedback!  
Makes me reconsider, procrastinating to learn about proper package / project-setup.  
As of now, vs-code doesn’t seem to be able to refactor anything in my setup (at least not anything beyond simple find-replace, purely based on lexicographic pattern-matching, that one could be doing in any basic text-editor).  
But your post makes me question, whether that is “just” due to my own ignorant setup, or not…  
I’ll be testing it, I guess! 🤔
