A VECTR-Guided Refactor with Cursor.

Software PhilosophyEngineering Best Practices Coding Practices
A VECTR-Guided Refactor with Cursor.

What's up?

Let walk through a recent example i've had to contend with and apply the VECTR principles. The example is redacted and domain mapped out of insurance plan mapping to a made up warehouse inventory mapping function... to protect the innocent... aka me, cause I wrote the first version.

The OG function is a bit long, a little dense, and full of nested conditionals but it works, its even got comprehensive 100% coverage unit tests that are quite good if I do say so myself, but gnarly for others to contend with.

Let's walk through it with some cursor assistance and see how I do.

The "Before" State: A Function with Hidden Logic

The function, map_source_statuses_to_internal_codes, had a critical job: map statuses from an external API to internal codes. It had to handle various scenarios: standard stock, refurbished items, different product conditions ("Premium," "Standard"), and several fallback rules. The original code looked something like this (simplified for clarity):

map_source_statuses_to_internal_codes.py
# Original Structure
def map_source_statuses_to_internal_codes(self, ...):
    if some_precondition_is_met:
        return {}

    final_mapping = {}
    source_statuses_by_category = {}
    internal_statuses_by_category = {}

    # --- Repetitive categorization logic for source statuses ---
    for item in source_statuses:
        # ... logic to categorize items ...
        source_statuses_by_category.setdefault(category, []).append(item)

    # --- Nearly identical categorization logic for our internal codes ---
    for name in internal_status_names:
        # ... logic to categorize items ...
        internal_statuses_by_category.setdefault(category, []).append(name)

    # --- A tangled mess of mapping and fallback logic ---
    for category, internal_statuses in internal_statuses_by_category.items():
        # Try for a direct mapping first
        source_statuses = source_statuses_by_category.get(category)
        if source_statuses and len(source_statuses) == 1 and len(internal_statuses) == 1:
            final_mapping[internal_statuses[0]] = [source_statuses[0].item_id]
            continue # Found a match, but this makes the loop harder to follow

        # If that failed, is it a refurbished item needing a fallback? This logic is
        # inside the same loop, making it hard to separate from the direct mapping.
        if category.startswith("refurbished_"):
            standard_category = "standard_" + category[len("refurbished_"):]
            source_standard_statuses = source_statuses_by_category.get(standard_category)

            # Only apply fallback if a direct refurbished status wasn't found
            if not source_statuses:
                if (
                    source_standard_statuses
                    and len(source_standard_statuses) == 1
                    and len(internal_statuses) == 1
                ):
                    final_mapping[internal_statuses[0]] = [
                        source_standard_statuses[0].item_id
                    ]

    # Now, a completely separate loop for another fallback... Why a new loop?
    # This one handles cases where the supplier gives us an "unspecified" condition.
    for category, internal_statuses in internal_statuses_by_category.items():
        if category.endswith("_unspecified"):
            base_category = "refurbished_standard" if category.startswith("refurbished_") else "standard_standard"
            source_standard_items = source_statuses_by_category.get(base_category)
            if source_standard_items and len(source_standard_items) == 1:
                for internal_status in internal_statuses:
                    # We have to check if a mapping already exists from the PREVIOUS loop.
                    # This shared state modification is a classic source of bugs.
                    if internal_status not in final_mapping:
                        final_mapping[internal_status] = [source_standard_items[0].item_id]

    return final_mapping

The Problem:

  1. Convoluted Logic: The mapping rules were a tangle of nested if statements spread across multiple loops. It's impossible to tell at a glance what the hierarchy of rules is.
  2. Duplication: The categorization logic was copied and pasted for both source_statuses and internal_statuses. A bug in one would have to be fixed in both. "Dangerous" duplication.
  3. Implicit Algorithm: The code is trying to implement a cascading set of rules, but you'd have to read it multiple times to figure it out.

The Refactoring Journey with VECTR

Step 1: Reveal the Truth (VECTR Tenet #7)

The first step was to separate the data preparation from the mapping logic.

  • VECTR #7: Simplicity Should Reveal Truth, Not Hide It.

Step 2: Create an Abstraction That Solves a Real Problem (VECTR Tenets #2 & #3)

With the tangled logic identified, the next step isn't just to start breaking things into smaller functions for the sake of it. The abstractions must be purposeful and should solve a real problem like reducing complexity, clarifying intent, or, as in this case, eliminating dangerous duplication.

I am gonna apply...

  • VECTR #3: Duplication is Cheaper Than the Wrong Abstraction.
  • VECTR #2: Abstractions Should Make Things Easier.
  1. _get_status_category(): Takes a status (from either system) and returns its category string (e.g., "refurbished_standard"). This centralized the messy business rules.
  2. _categorize_statuses(): A simple helper that uses the first to group a list of statuses into a dictionary.
helpers.py
# New Helpers
def _get_status_category(self, status_info: str | SourceStatus) -> str:
    # ... logic to determine category ...

def _categorize_statuses(self, statuses: list) -> dict[str, list]:
    categorized = {}
    for status in statuses:
        category = self._get_status_category(status)
        categorized.setdefault(category, []).append(status)
    return categorized

By creating these, I didn't just remove duplication; I gave a name to a core concept in our domain: "status categorization."

Step 3: Structure for Evolution (VECTR Tenet #6)

  • VECTR #6: Think in Lifecycles, Not Layers. Software evolves. Build for versioning, iteration, and real-world growth.

With the helpers in place, the main function became clearer. I structured it to explicitly reflect the algorithm: a series of prioritized strategies.

map_source_statuses_to_internal_codes.py
# The "After" State
def map_source_statuses_to_internal_codes(self, ...):
    if self.__is_mapping_impossible(...):
        return {}

    # 1. Prepare the data (using our new helpers)
    source_statuses_by_category = self._categorize_statuses(source_statuses)
    internal_statuses_by_category = self._categorize_statuses(internal_status_names)
    final_mapping = {}

    # 2. Execute the mapping algorithm as a series of clear, prioritized strategies.
    # The comments now describe the business logic, not just the code.

    # Strategy 1: Direct Mapping (e.g., standard_premium -> standard_premium)
    # ... loop ...

    # Strategy 2: Fallback for refurbished items to map to a standard status.
    # ... loop ...

    # ... and so on ...

    return final_mapping

This version is longer in lines of code (because of the new helper methods), but the primary function is infinitely more readable. Its structure mirrors how the business rules will likely evolve, by adding, removing, or re-prioritizing these strategies

The Counter-Intuitive Part: Why Keep It in One Function?

A common instinct might be to break each "Strategy" into its own private method. But this is where VECTR's pragmatism shines. Doing so would have hidden the true nature of the algorithm, a single, sequential waterfall of rules. It would force a developer to jump between five different functions to understand one cohesive process.

The Payoff

The final code is:

  • Safer to Change: The logic is no longer intertwined. You can confidently add a new rule without breaking existing ones.
  • Easier to Debug: When a mapping is wrong, you can trace the strategies in order to see exactly where the logic failed.
  • Faster to Onboard: A new engineer can read one function and understand the entire mapping process.

AI

... if you want to apply VECTR to you work, copy this to a cursor agent.

cursor_agent.md
You're a senior Python engineer and a VECTR refactoring assistant (Velocity-Engineered Code for Rapid Teams).

Your role is not to beautify code for aesthetics, but to improve it based on practical tradeoffs, team sustainability, and product evolution. You're here to refactor and improve this code with judgment.

Apply the following VECTR tenets to your analysis and rewrite:

---

### 1. Code is a Practical Tool  
*What:* Code isn’t art—it’s a tool. Prioritize readability, maintainability, and speed to understanding.  
*Why:* Over-cleverness slows down teams. Optimizing for elegance over clarity makes code harder to debug, extend, or onboard into. Write like you’re handing it off to a smart but rushed teammate.

---

### 2. Abstractions Should Make Things Easier  
*What:* Abstract only when the benefit is real—reducing pain, duplication, or complexity.  
*Why:* Abstractions that don't solve pain just add confusion. Every new layer has a maintenance cost. If it’s not pulling its weight today, it’s overhead.

---

### 3. Duplication is Cheaper Than the Wrong Abstraction  
*What:* Don’t DRY too early. It’s okay to repeat yourself until patterns emerge.  
*Why:* Premature generalization creates tight coupling and vague intent. Two similar things today may need to diverge tomorrow. Clarity now is more valuable than abstraction that breaks later.

---

### 4. Tests Should Serve Development, Not Delay It  
*What:* Write tests that increase confidence and aid discovery—not as ceremony.  
*Why:* Over-tested or brittle test suites slow development. Focus testing where failure would hurt, and let architecture stabilize before formalizing behavior with tests.

---

### 5. Systems Reflect Teams  
*What:* Code structure will mirror communication structure. Design with people in mind.  
*Why:* Conway’s Law is real. Silos produce fractured systems. Organize code around how teams naturally collaborate—not idealized boundaries.

---

### 6. Think in Lifecycles, Not Layers  
*What:* Software evolves. Build for versioning, iteration, and real-world growth.  
*Why:* Layered architectures ossify fast. Real products mutate over time. If your system can’t bend, it will break. Lifecycle-aware design reduces rewrite risk.

---

### 7. Simplicity Should Reveal Truth, Not Hide It  
*What:* Use simplicity to expose the structure and logic of your code.  
*Why:* Brevity is not the same as clarity. One-liners and chaining hide logic, especially under pressure. Step-by-step code is easier to debug and safer to change.

---

### 8. Anticipate Change, But Don’t Hallucinate It  
*What:* Design for real, expected changes—not imaginary futures.  
*Why:* Overbuilding for edge cases that don’t exist wastes time and adds complexity. Complexity is only worth it when grounded in actual business or architectural need.

---

### 9. Documentation is a Power Tool, Not a Chore  
*What:* Document the *why*, not just the *what*.  
*Why:* A single comment can save hours later. Future devs (including you) will forget the reasoning behind weird edge cases. Inline intent beats buried docs.

---

### 10. Pragmatists Use What Works  
*What:* Choose the best tool for this context—even if it’s not the trendy one.  
*Why:* Tooling fads change. Productivity comes from clarity, not from dogmatic allegiance to frameworks or paradigms. Favor tools that move the team forward now.

---

### Instructions:

1. Refactor the code using these principles.
2. Make it easier to understand, easier to extend, and safer to change.
3. Favor practical changes over academic ones.
4. Add inline Python `# comments` to explain *why* a change was made when not obvious.