text
stringlengths
0
897
It looks like this:
```miniscript
greeting = "Hey " * 3
print greeting
```
Line 1 in this example also computes `"Hey Hey Hey "`, but then stores this result in a variable called `greeting`. Line 2 then retrieves that value, passing the value of `greeting` to the `print` function.
![Variables are like storage bins for data.](variableBins.svg)
And that's all there is to it. Variable names can be pretty much anything, as long as they start with a letter or underscore character, and contain only letters, numbers, and underscores. In particular, this means that a variable name can't contain spaces. So `hamOnRye` would be a perfectly fine variable name, but `...
D> MiniScript uses spaces and punctuation to divide your program code into words, and expects each word to have some meaning. That's why you can't use spaces, commas, or other punctuation in a variable name; it would look like multiple words. A variable is a single thing, so it needs to be a single word as far as the...
E> Does "hamOnRye" look strange to you? This style of internal capital letters, known as "camel case," is common in computer programming, but not in normal English. It's a way to let us humans see it as multiple words, while still appearing to the computer as a single word.
The example above was admittedly a bit pointless. Our greeting expression was very simple and used immediately, so we didn't really need a variable there -- it was like making a simple sandwich and eating it right away. So let's look at a more realistic example.
{caption: "Always-8 number trick"}
```miniscript
num = 42
print "starting with: " + num
x = num - 1
print "- 1: " + x
x = x * 3
print "* 3: " + x
x = x + 12
print "+ 12: " + x
x = x / 3
print "/ 3: " + x
x = x + 5
print "+ 5: " + x
result = x - num
print "- " + num + ": " + result
print "...it's always 8!"
```
This program encodes a little arithmatic trick I found somewhere. You start with any number -- the listing above uses `42` -- and then do a series of simple math operations to it. At the end, no matter what you started with, you end up with 8.
Variables are handy in this program for several reasons. First, it lets us take a complex series of steps and do them one at a time, which makes the code easier to write and understand. Second, it lets us print out the result of each of those steps as we go. Without variables, we could either print the result of a c...
{pageBreak}
D> Try it! Enter the program on the previous page carefully, and run it several times, changing the value assigned to `num` in the first line each time. Neat, huh?
## Assignment Is Not Math
Some mathematically-minded people may be disturbed by lines like
```miniscript
x = x + 12
```
from the above example. If you think of this as algebra, it makes no sense at all! In high-school math, this would be a statement that says, "there is a number x such that x is equal to x plus 12." There can be no such number; this is an equation with no solution.
But this isn't algebra, and the above line is not a declaration of equality. It's computer programming, and this is an assignment. The computer does this in two steps: first, it computes the value of whatever's on the right-hand side of the `=` sign. And second, it stores that value under the label on the left-hand ...
Going back to the kitchen analogy, this would be like having a container labeled "lunch". In the morning, you store some bread in this container. At your mid-morning break, you retrieve your "lunch" container, take out the bread, insert some ham, and then put it back into that same "lunch" container. You've simply r...
The same thing is going on with `x = x + 12`. We first take out the value of `x`, whatever it is at this point, and add 12 to it. Then we store this new value back into `x`, replacing the previous value.
## Leaving Notes For Yourself
The second (and last) concept for today is simple but important: leaving little messages in your programs, so that readers of the code (including your future self) will better understand what it's all about. These messages are called *comments*.
comment
: a message left in a program, ignored by the computer, meant for human readers of the code
A comment in MiniScript starts with two forward slashes, `//`, and continues until the end of the line. So anywhere you see `//` in a MiniScript program, except of course inside a string, you are looking at a note left for you by whoever wrote the code.
Here's an example program containing some comments:
{caption: "Simple dice program"}
```miniscript
// Dice program
// Rolls two 6-sided dice and prints the sum.
// Note: rnd returns a number between 0 and 1.
// So if we multiply by 6, we get a number between 0 and 6.
// The ceil function then "rounds" this to a whole number 1-6.
d1 = ceil(rnd * 6) // get the first die
d2 = ceil(rnd * 6) // get the second die
print "Rolled: " + d1 + " and " + d2
print "Total: " + (d1 + d2) // parens needed here!
```
The comments at the top of the listing are sometimes called *header comments*, and are generally there to provide an overview of what the program does. Those are lines 1-2 in the listing above. Lines 4-6 are providing some implementation notes, explaining the calculation that follows.
Those were all longer comments that got entire lines to themselves, but you can also put a shorter comment at the end of a line of code, as shown on lines 7, 8, and 10. In this case the computer will execute the MiniScript up until `//`, and then ignore the rest of the line. As a human, you may often do the opposite,...
As a beginner, you will be tempted to skip writing comments. You are juggling a lot of things in your head, and comments feel like just one more. But this is a mistake. On the contrary, as a beginner you should write *more* comments, and you should write most of them first, before you write the actual code. Use the...
## Writing Clear Code
That brings us to possibly the most important lesson in this book, and it's well put by the quote at the start of this chapter.
The computer programs you've written so far have been simple, no more than a dozen lines, and often much less. It's simple enough to read through them and understand what they're doing.
But that won't be the case much longer. Even in a high-level language like MiniScript, nontrivial programs routinely take hundreds or thousands of lines of code. Commercial games and other apps are often millions of lines of code. No human can grasp all those details at once.