I'm familiar with refactoring fairly large code bases in C# and Java but Clojure is something of a different beast, especially since it:
Given the above, what is the best way to approach code refactoring in Clojure?
In "Working effectively with legacy code" Michael Feathers suggests adding unit tests to create artificial "inflection points" in the code that you can re-factor around.
a super brief and wholly incomplete overview on his approach to adding order to unstructured code:
The recursive approach seemed to fit well with the mental processes I use in thinking about Clojure so I have come to associate them. even new languages can have legacy code right?
This is what I got from my reading of that one book while thinking about clojure. So I hope it is useful as a general guideline. perhaps your codebase already has good tests, in which case you're already beyond this phase.
I'm not an expert. But anyway:
God
functions. If you have a big function, break it down to smaller functions and each of these functions is doing one thing, and it is doing it well.defrecord
and defprotocol
.Also, have a look at CursiveClojure. I think it is really promising.
I'm not the creator of CursiveClojure.
I'm not familiar with refactoring fairly large code bases in C# or Java, but here goes.
Clojure:
Has a mix of macros and functions: I could be wrong, but I think you'll find that refactoring seldom moves the interface between macros and functions.
Uses dynamic typing (so you don't get compile time checks on refactored code): ... nor on any other code: you need more tests in either case.
Is functional rather than object-oriented in style Refactorings are stated in OO terms, but often survive simple transcription: method to function, class to function or closure or map.
Has less support for refactoring in current IDEs True: more busywork.
Is less tolerant of cyclic dependencies in code bases Two cases: mutual recursion in a namespace/file should be easier to cope with than it is; but cyclic dependencies between namespaces/packages cause confusion and ambiguity. I think Java only allowed them because C++ did, and C# followed suit.
I have found it useful to look through Martin Fowler's catalogue of refactorings. Most survive translation from OO to functional lingo. Some (such as Change Value to Reference) disappear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With