Living Museum of Learning

Small circle, Big thinkers 🌱
Ivy’s First Code Refactoring Moment

Ivy’s First Code Refactoring Moment

When a complex function became just two calls—and a new way of thinking appeared.

For two weeks, Ivy had been working with a function called star().

It was responsible for drawing “star points” on a Chinese chess board.

The code was long, detailed, and carefully constructed.

Nothing about it felt easy to change.

Recently, we had successfully introduced a helper function called half().

It worked.

It made sense.

And it quietly simplified part of the system.

After we finished refactoring half(), I asked a simple question:

"What if I told you the entire star() function could be rewritten in just two lines?"

She paused.

Then I asked again:

"Is star() actually just two halves?"

That was the moment something shifted.

She saw it.

But her instinct was still to copy code inside the function.

So we stopped.

And I showed her something different:

You don’t copy structure.

You call it.

After refactoring, nothing changed visually.

The chessboard stayed the same.

The behavior stayed the same.

But the code became:

def star(starX, starY):
half(starX, starY, True)
half(starX, starY, False)

And the reusable building block:

def half(halfX, halfY, isUpper):
gap = 5
length = 10

a = 1 if isUpper else -1

line(halfX - gap - length, halfY + a*gap,
halfX - gap, halfY + a*gap)

goto(halfX - gap,
halfY + a*gap + a*length)

line(halfX + gap + length, halfY + a*gap,
halfX + gap, halfY + a*gap)

goto(halfX + gap,
halfY + a*gap + a*length)

When the transformation was complete, Ivy looked at the result and quietly said:

"…that's it?"

Yes.

That was it.

And that was the point.

The surprise was not in Python.

It was in realization:

A complex-looking structure can sometimes be expressed as repetition of a simpler one.

This is what abstraction feels like the first time:

not power,

but reduction.

Not more code,

but less.

Complex visual patterns can often be expressed through simple reusable components.

Once students recognize repeated structure, they begin to think in functions instead of lines.

Abstraction is one of the first moments where programming becomes a way of thinking, not just writing code.