11-47's picture choucsan's picture
Duplicate from choucsan/mimo-claude-code-traces-1k
aeb931a
|
Raw History Blame Contribute Delete
8.11 kB
Claude Code trace
User 2026-06-21T12:08:47.674Z

Restructure this code to follow open/closed principle:

python
def calculate(data):
    total = 0
    for item in data:
        if item['type'] == 'A':
            total += item['value'] * 1.1
        elif item['type'] == 'B':
            total += item['value'] * 0.9
        elif item['type'] == 'C':
            total += item['value'] * 1.05
    return total
Assistant mimo-v2.5-pro 2026-06-21T12:08:48.674Z 4,379↓ 425↑ (44,928 cached)
Thinking The user wants me to refactor this code to follow the Open/Closed Principle (OCP). This is a straightforward refactoring task - replace the if/elif chain with a strategy pattern using a dictionary mapping. Let me check i...

The user wants me to refactor this code to follow the Open/Closed Principle (OCP). This is a straightforward refactoring task - replace the if/elif chain with a strategy pattern using a dictionary mapping.

Let me check if there's a file in the repo containing this code first.

Let me check if this code exists in the repo.

Assistant mimo-v2.5-pro 2026-06-21T12:08:50.674Z 4,379↓ 425↑ (44,928 cached)
Thinking These are just trace files, not actual source code. The user is asking me to refactor this standalone code snippet. Since it doesn't exist in the repo, I should just show them the refactored version. This is a simple, cl...

These are just trace files, not actual source code. The user is asking me to refactor this standalone code snippet. Since it doesn't exist in the repo, I should just show them the refactored version.

This is a simple, clear refactoring task - replace the if/elif chain with a strategy dictionary to follow OCP. I'll just provide the refactored code directly.

This code doesn't exist as source in the repo (only in trace data), so I'll provide the refactored version directly.

The issue: adding a new type (e.g., 'D') requires modifying calculate(). With OCP, you extend behavior by adding new code, not changing existing code.

python
# Extension point: register new types here without touching calculate()
ADJUSTMENTS = {
    'A': lambda value: value * 1.1,
    'B': lambda value: value * 0.9,
    'C': lambda value: value * 1.05,
}

def calculate(data):
    total = 0
    for item in data:
        adjust = ADJUSTMENTS.get(item['type'])
        if adjust is None:
            raise ValueError(f"Unknown item type: {item['type']}")
        total += adjust(item['value'])
    return total

To add a new type, you only add an entry to ADJUSTMENTS — calculate() stays unchanged:

python
ADJUSTMENTS['D'] = lambda value: value * 1.2  # no other code touched
mimo-v2.5-pro Input 8.76k↓ Input Cached 89.9k↓ (91.1%) Output 850↑