Code That Cleans Itself: A Journey into Automated Refactoringby@offcode
1,622 reads
1,622 reads

Code That Cleans Itself: A Journey into Automated Refactoring

by Adam SchmidegOctober 6th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Ayse's frustration with her son Mert's messy room leads to an innovative idea for code refactoring at work. She and colleague Jamal devise a system that leverages Abstract Syntax Trees (AST) to automate code cleanup, streamlining their development process. As they embark on this journey, parallels between tidying a room and maintaining a codebase come to light, offering valuable insights into code organization and efficiency.
featured image - Code That Cleans Itself: A Journey into Automated Refactoring
Adam Schmideg HackerNoon profile picture

Ayse’s hand landed firmly on Mert’s desk, causing papers to scatter everywhere. The morning sun highlighted the mess in the room. Clothes, books, and various items were scattered everywhere, a testament to a teenager’s whirlwind life.

“Mert!” Ayse’s voice was filled with frustration. “How many times have I told you about this room? How can you find anything in here?”

Mert, looking defeated, responded, “I’ve searched everywhere, Mom. I can’t find it.”

Ayse took a deep breath and stepped into the room. “Describe the book to me,” she said, her voice calmer.

He thought for a moment. “It’s this size,” he showed with his hands. “It has a blue cover. I drew a maze on it with a silver pen. There’s also a red ribbon bookmark.”

Ayse began looking around, hoping to spot the book. Mert just watched, his energy spent from searching.

As she moved to a pile of bags near the closet, Ayse’s fingers brushed against the familiar texture of a book’s spine. She pulled out Mert’s weekend bag, the one he used for visits to his father’s house. Unzipping it, her heart leapt as she spotted the blue cover with the intricate silver labyrinth.

“Here it is!” Ayse exclaimed. “Why on earth was it in this bag?”

He looked away, slightly embarrassed. “I took it to Dad’s,” he said. “I wanted to show him.”

She handed him the book, her face showing a mix of emotions. “We need to leave,” she said, checking the time. “Now.”

Mert held the book to his chest as they left the room, the atmosphere thick with unspoken feelings.

The tram’s rhythmic hum provided a backdrop as Ayse and Mert found seats next to each other. The city passed by in a blur outside the window, but inside, the tension was palpable.

“This can’t continue, Mert,” Ayse began, her voice carrying a mix of concern and frustration.

Mert, feeling the weight of the morning’s events, nodded. “I know, Mom. I’ll try harder.”

She started to give him a detailed breakdown, “Your clothes should go in the wardrobe, books on the shelf, toys in the box…” But she stopped herself mid-sentence, realizing the futility of her words. “No, this isn’t the solution. It won’t help with the mess.”

Distracting herself, Ayse began to rummage through her purse. Her fingers brushed against some old business cards. She pulled them out, hoping to divert her mind from the current situation. Glancing at one, she read aloud, “System analyst.” An idea began to form.

Grabbing a pen, she scribbled on the back of the cards. Mert watched curiously, his earlier despondency replaced by intrigue.

Ayse cleared her throat, “This is my adventure game for you.” She held up the cards, reading them one by one:

  • “All items have a home. Return them after use.”
  • “Daily declutter: Spend 5 minutes before bed putting things away.”
  • “Group like items together: All books in one place, all toys in another.”
  • “If you haven’t used it in a month, consider if you really need it.”

Mert looked at the cards, then back at his mother. “It’s like a game?”

Ayse smiled, “Exactly. Think of it as leveling up in real life. Each step helps you maintain order and makes your life easier.”

He pondered for a moment, then nodded. “Okay, I’ll give it a try.”

The tram had come to a stop, and Ayse stepped out, heading towards her office building. As she approached the entrance, she spotted Jamal, her colleague, waiting for her.

“Morning, Ayse,” he greeted with a smile.

“Morning, Jamal,” she replied, still thinking about the conversation with Mert.

Jamal, always eager to discuss work, began, “You remember the ‘Vladimir’s revenge’ section of our codebase?”

Ayse chuckled, “How could I forget? That legacy code has been a thorn in our side for ages.”

Jamal nodded, “Exactly. So, I’ve been thinking about the evolution of refactoring.

Ayse raised an eyebrow, intrigued. “Go on.”

“Well,” Jamal began, counting off on his fingers, “First, we did it manually. Like, if we changed the parameters of a function, we’d search for every instance of that function and update it manually.”

Ayse grimaced, “Time-consuming and error-prone.”

“Exactly. Then, we moved to regex, using it to replace instances across multiple files.”

Ayse nodded, “A bit better, but still not foolproof.”

“Right. And then, we started using the IDE. It has some basic refactoring techniques, like adding a parameter or renaming a function.”

Ayse recalled the countless hours they’d spent on ‘Vladimir’s revenge’. “But we were still stuck with that part of the code.”

Jamal’s eyes lit up, “That’s when I did some digging and found out about tools that work with the AST.”

Ayse frowned, “AST?”

“Abstract Syntax Tree,” Jamal explained. “It’s a tree representation of the structure of code. Instead of seeing code as just text, AST sees it as a structure with nodes representing different elements, like functions, variables, and so on.”

Jamal could see the curiosity mixed with confusion in Ayse’s eyes. “Let me give you a simple example to illustrate AST,” he said, pulling out a notepad.

He quickly scribbled down a basic code snippet:

function add(x, y) {
    return x + y;

“This is a simple function in JavaScript,” Jamal began. “Now, when this code is parsed into an Abstract Syntax Tree, it’s broken down into a structured format.”

He drew a tree diagram:

├─ Identifier (name: "add")
├─ Parameters
│   ├─ Identifier (name: "x")
│   └─ Identifier (name: "y")
└─ BlockStatement
    └─ ReturnStatement
        └─ BinaryExpression
            ├─ Identifier (left: "x")
            ├─ Operator: "+"
            └─ Identifier (right: "y")

“This tree,” Jamal continued, “represents the structure of our code. Each node corresponds to a part of the code. For instance, FunctionDeclaration represents our function, Identifier nodes represent variable or function names, and BinaryExpression represents the addition operation.”

Ayse studied the tree, “So, when we refactor using tools that understand AST, they’re working with this structured representation rather than just text. That’s why it’s more accurate?”

“Exactly,” Jamal confirmed. “If we wanted to rename the function from add to sum, an AST-based tool would look for the Identifier node with the name ‘add’ under FunctionDeclaration and change it there. This ensures that only the function name is changed and not any other variable named ‘add’ in the code.”

Ayse’s face lit up with understanding. “That’s brilliant. It’s like having a blueprint of a building and making changes to it, rather than just painting over the exterior.”

Jamal nodded, pleased. “Precisely. It’s a more intelligent way of refactoring, especially for complex codebases like ours.”

Ayse smiled, “Thanks for the crash course, Jamal. Let’s put an end to ‘Vladimir’s revenge’ once and for all.”

Ayse’s initial enthusiasm began to wane as the implications of what Jamal was suggesting settled in. “Wait a minute,” she said, her brow furrowing. “Doesn’t this mean we’ll have to write a tool to convert our source code into this AST format? And then another tool to manipulate the AST? And yet another to convert the modified AST back into source code? That sounds like a lot of work.”

Jamal chuckled softly, sensing her apprehension. “I understand why you’d think that, but no, we don’t have to start from scratch. Every programming language has a parser that constructs an AST. It’s a fundamental part of how compilers and interpreters work.”

He continued, “For instance, in JavaScript, there’s a tool called ‘Esprima’ that can parse code into AST. And there are libraries like ‘Recast’ that allow us to manipulate the AST and then convert it back into source code.”

Ayse looked relieved. “So, we don’t have to reinvent the wheel?”

Jamal shook his head, “Not at all. The tools are out there. We just need to leverage them. Think of it as assembling a puzzle with most of the pieces already provided.”

The tram rumbled softly beneath them as Ayse and Mert settled into their seats. Despite the morning bustle, both seemed notably calmer than the previous week.

In Mert’s hands was a sleek pen holder, its design hinting at the precision of a 3D printer. He looked pleased with it.

Ayse, recognizing it, commented, “That’s the pen holder from your tech project, right?”

“Yeah,” Mert replied, a small smile forming. “And I found my design notes and the USB without having to turn my room upside down.”

Ayse gave a half-smile, “Seems like the game had some effect.”

Mert nodded, looking at the pen holder. “It made organizing less tedious. Projects like this became simpler.”

Ayse patted his hand gently. “It’s good to see. Both the tidier room and this impressive piece.”

Mert just shrugged modestly, “Feels good to have things in order.”

As the tram continued its journey, Ayse’s gaze wandered from the pen holder to the organized layout of the city outside. The structured design of the holder began to remind her of something else, something related to their codebase.

A thought began to crystallize in her mind. Without a word, she rummaged in her purse and pulled out the business cards, scribbling down the nascent idea.

Mert, noticing her sudden preoccupation, queried, “Something on your mind, Mom?”

Ayse looked up, her thoughts still forming. “I might be onto something for work. Just connecting some dots.”

In the open-concept office, Ayse approached Jamal’s desk, her steps purposeful. The hum of keyboards and hushed conversations filled the space, but for Ayse, the world had narrowed down to the idea she had scribbled on the back of a business card.

Jamal looked up from his monitor, noticing her approach. “Hey, Ayse. You look like you’ve had an epiphany.”

She smiled, holding up the business card. “Remember how we talked about refactoring using the AST?”

Jamal nodded, leaning back in his chair. “Of course. What’s on your mind?”

Ayse took a deep breath, laying out her idea. “Instead of directly refactoring the source code and then opening a PR with those changes, what if we took a different approach? We could write a program that does the refactoring for us. This program, or meta-program, would be what we review.”

Jamal raised an eyebrow, intrigued. “Go on.”

Ayse continued, her excitement evident, “Once we’re satisfied with the meta-program, we integrate it into our CI/CD pipeline. Here’s how it would work:”

  1. “We push the meta-program to a dedicated branch.”
  2. “The CI/CD pipeline picks it up and runs the transformation on our codebase, converting it using the AST logic we’ve defined.”
  3. “Automated tests are then run on the transformed code. If everything passes, the pipeline automatically merges the changes into the main branch. If there are issues, it flags them for us to review.”

Jamal leaned forward, his interest piqued. “So, instead of reviewing the refactored code, we review the logic that does the refactoring. It’s like… setting the rules and letting the system apply them.”

“Exactly,” Ayse confirmed. ” Think of it like a database migration, but for our source code. It could streamline our process, reduce human error, and ensure consistency. Plus, it would save us countless hours of manual code review. We’d be reviewing the transformation logic, not every single line of changed code.”

Jamal thought for a moment, then nodded. “It’s a bold approach, but it might just work. It’s like teaching the system how to clean its own room but on a much larger scale.”

Ayse chuckled, “Inspired by real-life events.”

[The next part of this story is Proglogging: The Developer's Detective Toolkit]