Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

angl_yaz / Lesson 10

.doc
Скачиваний:
9
Добавлен:
10.02.2016
Размер:
37.38 Кб
Скачать

Lesson 10

THE READING MODULE

Read the text: Using Functions

Unlike prose, where repeating the same word or phrase may seem redundant, in programming, it's perfectly fine to use the same construction over and over again. Of course, you may want to turn a repeated chunk of code into a function: this is even more readable because it gives the block of code a descriptive name. (At least you ought to make it descriptive!)

You can also increase readability by using standard functions and data structures (such as the STL). Doing so avoids the confusion of someone who might ask, "why did you create a new function when you had a perfectly good one already available?" The problem is that people may assume that there's a reason for the new function and that it somehow differs from the standard version.

Moreover, by using standard functions you help your reader understand the names of the arguments to the function. There's much less need to look at the function prototype to see what the arguments mean, or their order, or whether some arguments have default values.

Use Appropriate Language Features

There are some obvious things to avoid: don't use a loop as though it were an if statement. Choose the right data type for your data: if you never need decimal places in a number, use an integer. If you mean for a value to be unsigned, used an unsigned number. When you want to indicate that a value should never change, use const to make it so.

Try to avoid uncommon constructions unless you have good reason to use them; put another way, don't use a feature just because the feature exists. One rule of thumb is to avoid do-while loops unless you absolutely need one. People aren't generally as used to seeing them and, in theory, won't process them as well. I've never run into this problem myself, but think carefully about whether you actually need a do-while loop. Similarly, although the ternary operator is a great way of expressing some ideas, it can also be confusing for programmers who don't use it very often. A good rule of thumb is to use it only when necessary (for instance, in the initialization list of a constructor) and stick with the more standard if-else construction for everything else. Sure, it'll make your program four lines longer, but it'll make it that much easier for most people to read.

There are some less obvious ways of using standard features. When you are looping, choose carefully between while, do-while, and for. For loops are best when you can fill in each part (initialization, conditional, and increment) with a fairly short expression. While loops are good for watching a sentinel variable whose value can be set in multiple places or whose value depends on some external event such as a network event. While loops are also better when the update step isn't really a direct "update" to the control variable--for instance, when reading lines from a text file, it might more sense to use a while loop than a for loop because the control depends on the result of the method call, not the value of the variable of interest.

I. Reading Exercises:

Exercise 1. Read and memorize using a dictionary:

Perfectly, construction, descriptive name, readability, confusion, available, standard version, arguments, loop, statement, integer, for loop, ternary operator, fairly short expression.

Exercise 2. Answer the questions:

1. What do you want to turn? Why?

2. How can you increase readability?

3. How do you help your reader understand the names of the arguments to the function?

4. What the good rule of thumb?

Exercise 3. Match the left part with right:

1. People aren't generally as

a) ought to make it descriptive!

2. This is even more readable

b) though it were an if statement.

3. At least you

c) used to seeing them and, in theory, won't process them as well.

4. don't use a loop as

d) because it gives the block of code a descriptive name.

Exercise 4. Open brackets choosing the right words:

A (good/bad) rule of thumb is to use it only when necessary ((for instance/for example), in the initialization list of a constructor) and (stick/put) with the (more/most) standard if-else construction for everything else.

THE SPEAKING MODULE

II. Speaking Exercises:

Exercise 1. Explain.

1) using language features;

2) some less obvious ways of using standard features;

3) using standard features.

Exercise 2. Ask questions to the given answers:

1)Question: _______________________________________________________?

Answer: There are some less obvious ways of using standard features.

2)Question: _______________________________________________________?

Answer: When you want to indicate that a value should never change, use const to make it so.

3)Question: _______________________________________________________?

Answer: There is much less need to look at the function prototype to see what the arguments mean.

THE WRITING MODULE

III. Writing exercises:

Exercise 1. Complete the sentences with the suggested words isn’t, lines, are, might, to use, depends on.

While loops______ also better when the update step______ really a direct "update" to the control variable--for instance, when reading______ from a text file, it______ more sense______ a while loop than a for loop because the control______ the result of the method call, not the value of the variable of interest.

Exercise 2. Give the meaning on your own language.

over and over again; _______________________

of course; ________________________

at least; _________________________

moreover; ________________________

much less need; _______________________

choose carefully. _______________________

Exercise 3. Write a short retelling of the text (60-80 words).

Соседние файлы в папке angl_yaz