When to Use Abstract Syntax Tree Tooling to Refactor at Scale
A rough formula to determine when to use AST-based tooling
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the Practical Abstract Syntax Trees course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Practical Abstract Syntax Trees, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
[00:00 - 00:17] Determining when to use an AST-based tool can be equally as important as understanding the tools themselves. In practice, AST-based tooling is most useful when working with large code bases, think hundreds of files or more, and when targeting code that follows a similar pattern.
[00:18 - 00:30] In smaller code bases, it's easy to manually update things across several files , or use a find-replace tool for simpler renaming. In a large code base, it could take days or longer to make manual changes depending on the exact changes.
[00:31 - 00:43] Potentially more concerning is that with so many repetitive changes, the chances of a making mistake increases. Another way you could say it is that AST-based tooling enables maintenance and refactoring at scale.
[00:44 - 00:54] This is primarily because using AST-based tooling is a one-time fixed cost. On the other hand, the cost of making manual changes grows linearly with the size of the code base.
[00:55 - 01:03] For AST-based tools, you write a custom script once and run it on all files. It doesn't matter if another file is added, removed, or changed.
[01:04 - 01:12] It takes the same amount of time to create the custom script for one file or a thousand. On the other hand, when making manual changes, the total number of files matters.
[01:13 - 01:25] As files are added, removed, or changed, it directly impacts the amount of time it takes to make those updates. With this logic, it's easier to determine if a task would be quicker to do manually, or quicker using an AST-based tool.
[01:26 - 01:34] This decision depends on how comfortable you are with AST-based tools. As you become more comfortable working with ASTs, writing custom scripts will take less time.
[01:35 - 01:42] This means over time, the break-even size of the code base will become smaller and smaller for it to be worth writing a custom script. script.