miniblog.

← Back to all posts
28
I've been building an 'extract function' refactor feature. It's surprisingly nuanced. Where do you put the extracted function? Once you've done the static analysis, what order do you use for the parameters?

8 Comments

@wilfredh one hint for arg order can be taken from the declaration order of the extracted args. This means that any new args taken from the source function's param list go first. Maybe not perfect in all cases, but a good starting point.
@mattbrowndev Ooh, that's an elegant approach. I'd been thinking about trying to take the most common orderings from other functions in the file, but using the ordering of the enclosing function is much more straightforward.
@wilfredh Is that what rust-analyzer does? I think I'm usually unsatisfied with the parameter order, but it's probably more important to make it easy to swap/reorder parameters than to get everything right on the first try.
@arxanas I'm not sure what r-a does, although I've also been surprised occasionally with the parameter order. I don't recall a refactoring in rust-analyzer to reorder parameters though. In fairness it's hard to build: reordering is much nicer in a GUI, which doesn't map well to LSP.
@wilfredh @mattbrowndev Oh, nice, I'll be interested to try it out and see if it makes a noticeable difference. [tangential] In several codebases, I've wanted a lint rule/configuration that would let me establish a partial ordering for parameters of certain types and/or names to enforce consistency, but I haven't found any tool for any environment that would let me do it (and I haven't written one myself 😫). The information about desired parameter ordering *exists*, to some degree. It's just unfortunate that it's hard to write down. (Also, if you wrote down what you thought the rules were for a certain codebase, you might discover that your intuition is actually unsatisfiable in certain cases.) Obviously, we can just throw an LLM in there to help 🤓