<< Chapter < Page Chapter >> Page >

Allowable variable names and lengths

Most programming languages have very specific requirements regarding the allowable characters for variable names. I suspect that this is also true ofScratch, but I haven't found that specification anywhere. You will probably be okay as long as you stick with letters, numbers, the underscore character"_", and the hyphen or minus sign "-".

It appears experimentally that the allowable length of the variable name is longer than you would ever want to use so length doesn't seem to be alimitation.

Many programming languages won't allow you to use a numeric character for the first character in a variable name, but that restriction doesn't seem to applyto Scratch. However, since you might later move up to more mainstream languages, you might want to avoid getting into the habit of using numeric characters asthe first character in variable names.

Meaningful variable names

When using Scratch and all other programming languages, you should strive to use variable names that are meaningful. My preference is to begin variable nameswith a lower-case letter, use multiple words in the name where appropriate, and separate the words using a format commonly called camelCase to cause the human eye to separate the words.

(The upper-case characters are analogous to the humps on a camel.)

Here is an example of the camelCase format:

aVariableName

Some people prefer the following format:

A_Variable_Name

I prefer the camelCase format because it is easier to type, requires fewer characters, takes less space, and in my opinion, is just as effective.

Writing the program

Generally speaking, programs are written in Scratch by:

  1. Selecting buttons in the upper-center of Image 1 to expose the programming blocks in the tool boxes associated with each of the categories listed earlier .
  2. Dragging programming blocks from the toolbox in Image 1 to the rightmost pane in Image 1 and snapping those blocks together in groups to form program scripts.
  3. Entering literal numeric values or dragging other blocks and dropping them into text fields with the same shapeto fill out the details of the program.

There are other steps involved in writing a complex program that I didn't include above, but we will get to them later in this collection of modules.Hopefully you already know the physical steps in writing a program as a result of working through some online tutorials.

Although Scratch 2.0 has some new features and a different screen layout, the general procedure for writing a program in Scratch 2.0 isessentially the same as the procedure for writing a program in Scratch 1.4. Thus, the older v1.4 tutorials are still very relevant.

As you can see in Image 1 , this program consists of three scripts in the rightmost pane. Each script contains onetan block and two orange blocks. We already know that orange blocks have to do with data or variables. As you can see from the colors on the left ends of the buttonsin Image 1 , tan blocks are related to Events .

Programming blocks in the Events category

Image 3 shows some of the programming blocks that are available in the Events category. (Scratch 1.4 had tools for the Events category and the Control category combined into a single toolbox.)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Teaching beginners to code. OpenStax CNX. May 27, 2013 Download for free at http://cnx.org/content/col11498/1.20
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?

Ask