Variables & kinds of data
The total that said 55
Alice, who sold handmade candles and whose totals had gone wrong once before, asked an AI to add a small order calculator to her site. A customer ordered five candles and then five more as a second line. The page showed a total of 55. The next three test orders showed the same odd result. She looked at the code the AI had written and saw a row of labels and values that all looked like numbers to her. She wondered why 5 and 5 didn't make 10.
- A variable is a name for a value, like total or customerName. The value can change, the name stays.
- Values come in kinds: numbers, text (strings) and true or false (booleans). The computer handles each kind in its own way.
- The numbers 5 and 5 add to 10, but the text 5 and 5 joins into 55.
- A value created inside a function or request usually vanishes when that function or request ends. Anything needed after an app restart belongs in durable storage such as a database.
Text '5' and '5' gives "55", not The number 10
Dos and don'ts
- When a total looks wrong, ask the AI what kind each value is.
- Ask the AI if an amount was saved as text.
- Put anything that must survive a restart into a database.
- Assume a value that looks like a number is a number.
- Store important data in a plain variable that disappears on restart.
- Three jars, each with a label. Each jar holds one value.
- A $30 item is added: the total jar now holds 30.
- Another $25 item: same jar, new value, 55.
- Payment goes through: isPaid flips from false to true.
A name for a value
The code refers to a value by its name, like total or customerName. The value can change while the app runs. The name stays the same.
Kinds of values
A number is for counting and math. Text, which programmers call a string, is anything inside quotation marks. A true or false value, called a boolean, answers a yes or no question, like isPaid. The computer treats each kind differently. Example: 5 + 5 → 10; "5" + "5" → "55". When the AI says "the amount was saved as text", this is the bug it means.
How long a value lives
A value made inside one small block of code, such as a function (a later topic explains functions), usually disappears when that block ends. The curly braces { } show where the block starts and stops. A value made during one page visit disappears when the visit ends. A value the running app keeps lasts until the app restarts. Anything you need after a restart belongs in durable storage, such as a database.
What to look for in an AI change
Anything that came from a form, a file or another system arrives as text until something converts it. Example: ask "is price a number or the text '19.99'?" and make the AI point at the line that converts it. Ask the AI: "Which of these values are text, and where do they become numbers before they're added or compared?"
Search my project for places where a number arrives as text. List each one with the file and line. Show me how you would convert it to a number before any math.
Paste into your agent inside a project folder. Prompts that say "this project" are for the app you build in the Lab, or any app you already have. No project yet? Read them now and paste them later, in the Lab.
Look at the Deal in this project. List each field with its kind (number, text or true/false) and say what would go wrong if one of them were stored as the wrong kind.
Find one place where a number from a form is used in math. Tell me whether it is converted from text to a number first, and show the line.
Explain in plain words the difference between a value that lives in a variable and a value that lives in the database. Use a contact's name as the example.