Tidying up source code
Oct. 27th, 2018 01:42 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Sometimes I change a bunch of code, get my changes working, then tidy up my mess, even working from the resulting large diff to recreate a series of commits that capture simpler logical steps. In thinking about some code reorganization yesterday I noticed that it includes activities that could be amenable to automated analysis:
This was especially in my mind over the past couple of weeks as I have had to repeatedly refactor some Java code: the extensions and changes I wanted to make turned out not to at all fit well into how state was previously distributed across classes so some exploratory rearchitecting was required and that came with an intermediate step where I did not bother to write tidily until I saw that the new approach worked at all; only then did I spend the extra effort to try to find orderly patterns in the resulting code.
- Recognizing clusters of state variables that are repeatedly affected with various inputs; perhaps that state can be broken out into a separate object and few methods may be needed to capture that mutation.
- Relatedly, recognizing similar code and breaking it out into a helper function. Last week this included an optional bit of code I turned into an optional parameter for such a function.
- Recognizing when a bunch of variables are read then written, then a bunch more are, with those later reads not depending on the previous writes: these could well be logically distinct blocks of code that at the least deserve separating with comments.
This was especially in my mind over the past couple of weeks as I have had to repeatedly refactor some Java code: the extensions and changes I wanted to make turned out not to at all fit well into how state was previously distributed across classes so some exploratory rearchitecting was required and that came with an intermediate step where I did not bother to write tidily until I saw that the new approach worked at all; only then did I spend the extra effort to try to find orderly patterns in the resulting code.