2017년 7월 18일 화요일

Defining Procedures: Reusing Blocks


Programming languages like App Inventor provide a base set of built-in functionality-in App Inventor's case, a base set of blocks. Programming languages also provide a way to extend that functionality by adding new functions (blocks) to the language. In App Inventor, you do this by defining procedures-named sequences of blocks-that your app can call just as it calls App Inventor's predefined blocks. As you'll see in this chapter, being able to create such abstractions is very important for solving complex problems, which is the cornerstone of building truly compelling apps.


When parents tell their child, "Go brush your teeth before bed," they really mean "take your toothbrush and toothpaste from the cabinet, squeeze out some toothpaste onto the brush, swivel the brush on each tooth for 10 seconds (ha!)," and so on. "Brush your teeth" is an abstraction: a recognizable name for a sequence of lower-level instructions. In this case, the parents are asking the child to perform the instructions that they've all agreed mean "brush your teeth."
In programming, you can create such named sequences of instructions. Some programming languages call them functions or subprograms. In App Inventor, they're called procedures. A procedure is a named sequence of blocks that can be called from any place in an app.
Figure 21-1 is an example of a procedure whose job is to find the distance, in miles, between two GPS coordinates you send to it.
Figure 21-1. Procedure for computing the distance between points
Don't worry about the internals of this procedure too much just yet; just realize that procedures like this let you extend the language by which you design and build programs. If every parent had to explain the steps of "brush your teeth" to his or her child each night, that kid might not make it to the fifth grade. It's much more efficient to just say, "Brush your teeth," and everyone can move on with getting to bed at a reasonable hour.
Similarly, once you define the procedure distanceBetweenPoints, you can ignore the details of how it works and simply refer to the procedure's name (or call it) when designing or coding a larger app. This type of abstraction is key to solving large problems and lets us break down a large software project into more manageable chunks of code.
Procedures also help reduce errors because they eliminate redundancy in your code. With procedures, you can put a chunk of code in one place and then call it from various places in your app. So, if you're building an app that needs to know the minimum distance between your current location and 10 other spots, you don't need to have 10 copies of the blocks shown in Figure 21-1. Instead, you just define the procedure and then call it whenever you need it. The alternative-copying and pasting blocks-is much more error-prone because when you make a change, you have to find all the other copies of those blocks and change each one in the same way. Imagine trying to find the 5–10 places where you pasted a particular chunk of code in an app with 1,000 lines or blocks! Instead of forcing you to copy and paste, a procedure lets you encapsulate blocks in one place.
Procedures also help you build up a library of code that can be reused in many apps. Even when building an app for a very specific purpose, experienced programmers are always thinking of ways to reuse parts in other apps should the need arise. Some programmers never even create apps, but instead focus solely on building reusable code libraries for other programmers to use in their apps!
Eliminating Redundancy
Take a look at the code blocks in Figure 21-2. See if you can you identify the redundant ones.




Figure 21-2. A Note Taker app with redundant code
The redundant blocks are the ones involving a foreach block (actually the foreach and the set NotesLabel.Text to above it). In all three foreachinstances, the block's job is to display the notes list. In this app, this behavior needs to take place when a new item is added, when an item is removed, and when the list is loaded from the database at application launch.
When experienced programmers see such redundancy, a bell goes off in their heads, probably even before they've copied and pasted the blocks in the first place. They know that it's best to encapsulate such redundancy into a procedure, both to make the program more understandable and so that changes will be much easier later.
So an experienced programmer would create a procedure, move the redundant blocks into it, and then call the procedure from the three places containing the redundant blocks. The app will not behave any differently, but it will be easier to maintain and easier for other programmers to work with. Such code (block) reorganization is called refactoring.
Defining a Procedure
Let's build a procedure to do the job of the redundant code blocks from Figure 21-2. In App Inventor, you define a procedure in a manner similar to how you define variables. From the Procedures drawer, drag out either a to procedure block or a to procedure result block. Use the latter if your procedure should calculate some value and return it (we'll discuss this approach a bit later in the chapter).
After dragging out a to procedure block, you can change its name from the default "procedure" by clicking the word "procedure" and typing the new name. The redundant blocks we're refactoring performed the job of displaying a list, so we'll name the procedure displayList, shown in Figure 21-3.


 Figure 21-3. Giving the procedure a name
The next step is to add the blocks within the procedure. In this case, we're using blocks that already exist, so we'll drag one of the original redundant blocks out of its event handler and place it within the to displayList block, as shown in Figure 21-4.
Figure 21-4. The displayList procedure encapsulates the redundant code
We can now display the notes list using a procedure that you can easily call from elsewhere in your app!
Calling a Procedure
Procedures, like displayList and "brush your teeth," are entities with the potential to perform a task. However, they'll only perform that task if they are called upon to do so. Thus far, we've created a procedure but haven't called it. To call a procedure means to invoke it, or to make it happen.
In App Inventor, you call a procedure by dragging out a call block from the Procedures drawer. Each time you define a procedure, a new block appears in that drawer. When you define a procedure, a call block is added, as shown in Figure 21-5.
Figure 21-5. A call block appears in the Procedures drawer when you define a procedure
You use call blocks all the time to call App Inventor's predefined functions, like Ball.MoveTo and Texting.SendMessage. When you define a procedure, you have in essence created your own block; you've extended the App Inventor language. The new call block lets you invoke your creation.
For the Note Taker app sample, you'd drag out three call displayList blocks and use them to replace the redundant code in the three event handlers. For instance, the ListPicker1.AfterPicking event handler (for deleting a note) should be modified as shown in Figure 21-6.

Figure 21-6. Using the displayList call to invoke the blocks now in the procedure
The Program Counter
To understand how the call block works, think of an app as having a pointer that steps through the blocks performing functions. In computer science, this pointer is called the program counter.
When the program counter is performing the blocks within an event handler and it reaches a call block, it jumps over to the procedure and executes the blocks in it. When the procedure completes, the program counter jumps back to its previous location (the call block) and proceeds from there. So, for the Note Taker example, the remove list item block is performed; then the program counter jumps to the displayList procedure and performs its blocks (setting the NotesLabel.Text to the empty text, and the foreach); and finally the program counter returns to perform the TinyDB1.StoreValue block.
Adding Parameters to Your Procedure
The displayList procedure allows redundant code to be refactored into a single place. The app is easier to understand because you can read the event handlers at a high level and generally ignore the details of how a list is displayed. It is also helpful because you may decide to modify how you display the list, and the procedure allows you to make such a modification in a single place (instead of three).
The displayList procedure has limits in terms of its general usefulness, however. The procedure works for a specific list (notes) and displays that list in a specific label (NotesLabel). You couldn't use it to display a different data list-say, a list of the app's users-because it is defined too specifically.
App Inventor and other languages provide a mechanism called parameters for making procedures more general. Parameters comprise the information a procedure needs to do its job-the specifics of how the procedure should be performed. In our bedtime tooth-brushing example, you might define "toothpaste type" and "brushing time" as parameters of the procedure "brush your teeth."
You define parameters for a procedure by clicking on the blue icon at the left-top of the procedure definition. For the displayList procedure, we would define a parameter named "list," as shown in Figure 21-7.
Figure 21-7. The procedure now accepts a list as a parameter
Even with the parameter defined, the blocks still refer directly to the specific list notes (it's plugged into the "in list" slot of the foreach). Because we want the procedure to use the list we send in as a parameter, we replace the reference to global notes with a reference to value list, as demonstrated in Figure 21-8.
Figure 21-8. Now the foreach will use the list sent in
The new version of the procedure is more generic: whatever calls displayList can now send it any list, and displayList will display it. When you add a parameter to a procedure, App Inventor automatically puts a corresponding slot in the call block. So, when the parameter list is added to displayList, the call blocks to displayList look like Figure 21-9.
Figure 21-9. Calling displayList now requires you to specify which list to display
The name list within the procedure definition is called a formal parameter. The corresponding slot within the call block is called an actual parameter. When you call a procedure from somewhere in the app, you must supply an actual parameter for each formal parameter of the procedure.
For the Note Taker app, you'd add a reference to the notes list as the actual parameter. Figure 21-10 shows how ListPicker.AfterSelection should be modified.
Figure 21-10. Calling the displayList with notes sent as the actual parameter
Now when displayList is called, the list notes is sent over to the procedure and placed in the parameter list. The program counter proceeds to execute the blocks in the procedure, referring to the parameter list but really working with the variable notes.
Because of the parameter, the procedure displayList can now be used with any list, not just notes. For example, if the Note Taker app was shared among a list of users and you wanted to display the list, you could call displayList and send it the user List, as shown in Figure 21-11.
Figure 21-11. The displayList procedure can now be used to display any list, not just notes

Returning Values from a Procedure

There is still one issue with the displayList procedure in terms of its reusability-can you figure out what it is? As it's currently written, it can display any list of data, but it will always display that data in the label NotesLabel. What if you wanted the list to be displayed in a different user interface object (e.g., you had a different label for displaying the userList)?
One solution is to reconceptualize the procedure-to change its job from displaying a list in a particular label to simply returning a text object that can be displayed anywhere. To do this, you'll use a procedure result block, shown in Figure 21-12, instead of the procedure block.
Figure 21-12. The procedure result block
You'll notice that, when compared to the procedure block, the procedure result block has an extra slot at the bottom. You place a variable in this slot and it's returned to the caller. So, just as the caller can send data to a procedure with a parameter, a procedure can send data back with a return value.
Figure 21-13 shows the reworked version of the preceding procedure, now using a procedure result block. Note that because the procedure is now doing a different job, its name is changed from displayList to convertListToText.
Figure 21-13. convertListToText returns a text object that the caller can place in any label
In the blocks shown in Figure 21-13, a variable displayText is defined to hold the text as the procedure iterates through each item of the list. This text variable replaces the overly specific NotesLabel component that was previously being used. When the foreach completes, the variable displayTextcontains the list items, with each item separated by a newline character, \n (e.g., "item1\nitem2\item3"). This displayText variable is then plugged into the return value slot.
When a procedure result is defined, its corresponding call blocks look different than those for a procedure. Compare the call to listToText with the call to the displayList in Figure 21-14.
Figure 21-14. The call on the right returns a value and so must be plugged into something
The difference is that the call listToText has a plug on the left. This is because when the call is executed, the procedure will run through its task and then return a value to the call block. That return value must be plugged into something.
In this case, the callers to displayList can plug that return value into any label they want. For the notes example, the three event handlers that need to display a list will call the procedure as shown in Figure 21-15.
Figure 21-15. Converting the list notes into text and displaying it in NotesLabel
The important point here is that, because the procedure is completely generic and doesn't refer to any lists or labels specifically, another part of the app could use listToText to display any list in any label, as exemplified in Figure 21-16.
Figure 21-16. The procedure is no longer tied to a particular Label component
Reusing Blocks Among Apps
Reusing code blocks through procedures need not be restricted to a single app. There are many procedures, like listToText, that could be used in just about any app you create. In practice, organizations and programming communities build up code libraries of procedures for their domains of interest-for example, a code library of animation procedures.
Typically, programming languages provide an "import" utility that allows for including library code in any app. App Inventor doesn't yet have such a utility, but one is being developed. In the meantime, you can create procedures in a special "library app" and begin new app development by saving a new copy of that app and working from it.
A Second Example: distanceBetweenPoints
With the displayList (listToText) example, we characterized procedure definition as a way to eliminate redundant code: you start writing code, find redundancies as you go along, and refactor your code to eliminate them. Generally, however, a software developer or team will design an app from the beginning with procedures and reusable parts in mind. This sort of planning can save you significant time as the project progresses.
Consider an app to determine the local hospital closest to one's current location, something that would come in very handy in case of an emergency. Here's a high-level design description of the app:
 When the app launches, find the distance, in miles, between the current location and the first hospital. Then find it for the second hospital, and so on. When you have the distances, determine the minimum distance and display the address (and/or a map) to that location.
From this description, can you determine the procedures this app needs?
Often, the verbs in such a description hint at the procedures you'll need. Repetition in your description, as indicated with the "so on," is another clue. In this case, finding the distance between two points and determining the minimum of some distances are two necessary procedures.
Let's think about the design of the distanceBetweenPoints procedure. When designing a procedure, you need to determine its inputs and outputs: the parameters the caller will send to the procedure for it to do its job, and the result value the procedure will send back to the caller. In this case, the caller needs to send the latitude and longitude of both points to the procedure shown in Figure 21-17. The procedure's job is to return the distance, in miles.
Figure 21-17. The caller sends four input parameters and receives a distance
Figure 21-18 shows the procedure we encountered at the start of the chapter, using a formula for approximating the mileage between two GPS coordinates.
Figure 21-18. distanceBetweenPoints procedure
Figure 21-19 shows blocks that make two calls to the procedure, each of which finds the distance from the current location to a particular hospital.
For the first call, the actual parameters for point1 are the GPS coordinates for St. Mary's Hospital, while point2 uses the current readings from the LocationSensor. The result value is placed in the variable distanceStMarys. The second call is similar, but instead uses the data for CPMC Hospital for point1.
The app goes on to compare the two distances returned to determine which hospital is closest. But if there were more hospitals involved, you'd really need to compare a list of distances to find the minimum. From what you've learned, can you create a procedure called findMinimum that accepts a list of numbers as a parameter and returns the index of the minimum?
Figure 21-19. Two calls to the distanceBetweenPoints procedure
Summary
Programming languages like App Inventor provide a base set of built-in functionality. Procedures let app inventors extend that language with new abstractions. App Inventor doesn't provide a block for displaying a list, so you build one. Need a block for computing the distance between GPS coordinates? You can create your own.
The ability to define higher-level procedure blocks is the key to engineering large, maintainable software and solving complex problems without being constantly overwhelmed by all of the details. Procedures let you encapsulate code blocks and give those blocks a name. While you program the procedure, you focus solely on the details of those blocks. But in programming the rest of the app, you now have an abstraction-a name-that you can refer to at a high level.

댓글 1개:

  1. Quantum Binary Signals

    Professional trading signals sent to your cell phone every day.

    Start following our trades right now & earn up to 270% per day.

    답글삭제