In the early days of computers, software computed things and solved big, hairy math formulas. Because of this, software languages were modeled after a recipe, a set of math instructions: do A, then B, then C. Programming languages were developed such that the top-level structure is a "main" program consisting of high-level steps.
The Recipe Analogy is Dead
With the advent of the modern user interface and certainly with the do-everything devices we now carry around with us, the recipe analogy doesn't hold. Software is now a relational entity that reacts to various stimuli from the user and external environment. Instead of an app being a single recipe or main program, it is, in essence, a stimulus-response machine.A stimulus, more commonly called an event, is something that happens to your app. Your job as a programmer is to specify how the app should respond to events.
Probably the most common event is a button click event: what should the app do when the user clicks a button.
In the "I Have a Dream" app, there are two button-click event handlers, one specifying what happens when MLK's button is clicked, and the other specifying what happens when Malcolm X's button is clicked.
A button click is an example of a user-initiated event. But not all events are directly initiated by the user. There are also:
- Timer events, sort of like an alarm clock going off
- Sensor events, such as when GPS coordinates are beamed to the device
- Phone events, including incoming texts.
- Animation events, such as two objects colliding
- Web events, such as some requested data arriving from the web
- App (Screen) launch events.
You might find all event types in a single app. In a game, for instance, the app might respond to the app startup by requesting high game information from the web. When the information arrives to the app, the app might respond by displaying it in on the screen. Once the game gets going, the app might respond to time passing by moving characters or shortening an energy bar. You can also conceive of a game that responds to an incoming text from another player, or to the phone being moved to another physical location.
An App is a Set of Event Handlers
With many of today's popular languages-- Python, Java, C, C++, Objective C-- events are second-class citizens. One reason App Inventor is intuitive is that it is based directly on the stimulus-response paradigm. Instead of specifying a "main" program, you specify a set of event handlers: when this event happens, perform the following operations.These first-level blocks are called event-handlers.
Your job as a programmer is to specify what should happen in response to each event. You start by dragging out event handler
blocks, yellowish blocks that read, "when App Inventor apps still have recipes-- do A, do B...- but the recipes are embedded in event handlers. Indeed, each event handler is like its own "main" program and can include step-by-step instructions along with conditionals and iteration just like in traditional languages.
What Happened to the Recipe?
App Inventor apps still have recipes-- do A, do B...- but the recipes are embedded in event handlers. Indeed, each event handler is like its own "main" program and can include step-by-step instructions along with conditionals and iteration just like in traditional languages.Event Handling Learning Nugget (includes video)