0Pricing
Mojo Academy · Lesson

Copies vs Shared State

When changes do and don't ripple out.

Copies vs Shared State is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When Do Changes Spread?

Sometimes editing one variable also edits another, and sometimes it does not. The difference is copy versus shared state. 🔍

A Copy Is Self-Contained

When you get a copy, you hold separate data. Editing it cannot reach back and disturb the value you copied from.

var a = 1
var b = a
b = 2

Shared State Is One Thing, Two Names

With shared state, two variables point at the same underlying data. A change through one is visible through the other.

The Telltale Sign

Ask: after I edit one variable, did the other one change too? If yes, they shared; if no, you had a copy.

Mojo Copies By Default

For value types, Mojo gives you a copy on assignment. So in everyday code, changes stay nicely contained.

var first = 7
var second = first
second = 8

Structs Copy Their Fields

Copy a struct and you copy each field inside it. The new struct is fully independent from the original.

Why Sharing Causes Surprises

Shared state means a distant edit can silently change your value. That spooky action at a distance is a classic source of bugs.

Sharing Has Its Uses

Sharing is not evil; sometimes you want one source of truth. The key is to choose it on purpose, not by accident.

Reading Code With This Lens

When you see an assignment, picture whether it makes a copy or a link. That habit predicts how edits will travel.

var original = 50
var backup = original

A Quick Comparison

Copy: edits stay put. Shared: edits ripple. Mojo's default leans toward copies, so ripples are the exception, not the rule.

Independence You Can Trust

Knowing copies are independent lets you change a value boldly, sure that other parts of your program stay stable. ✅

Quick Check

Two variables point at the same underlying data. You edit one. What do you expect?

Recap

A copy keeps edits contained; shared state lets them ripple. Mojo copies value types by default, so surprises are rare. 🎯

Frequently asked questions

Is the “Copies vs Shared State” lesson free?

Yes — the full text of “Copies vs Shared State” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Copies vs Shared State”?

When changes do and don't ripple out. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Copies vs Shared State” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Value Semantics Means
  2. Copies vs Shared State
  3. Mojo's Safe Default
  4. A First Look at Mutation
← Back to Mojo Academy