Compiler Assisted Refactoring April 7, 2025
I learned all the tricks over 25+ years of coding.
The title of this post is a new phrase I learned thanks to a youtuber I watch from Russia, called tsoding. It's not a new concept though, by any means. He just was the first I heard call it something.
Compiler assisted refactoring is when you break your code, and instead of finding/replacing breaking references, you just compile and let the compiler tell you where it's broken. By breaking the code I mean like changing the signature of a method, or class names or properties, things of that nature. Changing the signatures of things are easily found by the compiler.
Listening to the compiler is also a good move, generally. The compiler is very good.
I recently found an issue in the LPGA service code that updates all of the stats. The most recent tournament was a Match Play tournament, and the stats compilation, for lack of a better word but not to do with code compilation :D, is different for a stroke play tournament. Basically, match play doesn't care about stats, it's just your opponent.
I noticed that one call to the API, though, was failing, and would fail repeatedly. However, there's a property on tournaments called “is included in stats”, and that is false for a match play tournament. I noticed stepping through code that only one of the many stats calls were failing, apparently.
Now, there are a few ways to go about this. First of all, I could contact the LPGA and let them know that the one stats call is failing when the others are succeeding. They are returning no data, but it's not a 500 error. This is good long term but doesn't fix it right now, it could take a few hours or days. They could tell me that I shouldn't be making any stats calls :D
The other ways involve fixing it in code. There are two options there: #1, wrap the calls to UpdateTournamentStats in an if statement that checks if the tournament is included in stats. This way is ok, but it's kind of open ended. If I wrote new calls to that from new service workers, I'd have to do the same thing.
if (includedInStats) UpdateTournamentStats(tournamentId)
Lots of code clipped for brevity :D but you get the idea. The other way is to update the signature of the method that updates stats to include the bool value of whether that tournament is included in stats. This way is much better and future proof. I will have to get the tournament from the database if I don't have it (sometimes I am just given a tournament id), and get the included in stats value, but I can't adequately describe how much more I like this approach.
UpdateTournamentStats(tournamentId, includedInStats)
Enter Compiler Assisted Refactoring. I change the signature, apply that flag within the method (like if !includedInStats then just return), the next step is to compile the project to see what breaks. Two breaks. Easy. In one case I had the tournament object so I could just pass the flag from that, the other instance I had to get the tournament from the database. The code is updated and running in production in no time.
Coding is art. Happy coding!