LEARNING JAVASCRIPT - Trang 208

Very often, a subroutine is used to package an algorithm, which is simply an under‐

stood recipe for performing a given task. Let’s consider the algorithm for determining

whether the current date is in a leap year or not:

const

year

=

new

Date

().

getFullYear

();

if

(

year

%

4

!==

0

)

console.log(

`

${

year

}

is NOT a leap year.`

)

else

if

(

year

%

100

!=

0

)

console.log(

`

${

year

}

IS a leap year.`

)

else

if

(

year

%

400

!=

0

)

console.log(

`

${

year

}

is NOT a leap year.`

)

else

console.log(

`{$year} IS a leap year`

);

Imagine if we had to execute this code 10 times in our program…or even worse, 100

times. Now imagine further that you wanted to change the verbiage of the message

that gets printed to the console; you have to find all the instances that use this code

and change four strings! This is exactly the problem that subroutines address. In Java‐

Script, a function can fill this need:

function

printLeapYearStatus

() {

const

year

=

new

Date

().

getFullYear

();

if

(

year

%

4

!==

0

)

console.log(

`

${

year

}

is NOT a leap year.`

)

else

if

(

year

%

100

!=

0

)

console.log(

`

${

year

}

IS a leap year.`

)

else

if

(

year

%

400

!=

0

)

console.log(

`

${

year

}

is NOT a leap year.`

)

else

console.log(

`{$year} IS a leap year`

);

}

Now we’ve created a reusable subroutine (function) called

printLeapYearStatus

.

This should all be pretty familiar to us now.
Note the name we chose for the function:

printLeapYearStatus

. Why didn’t we call

it

getLeapYearStatus

or simply

leapYearStatus

or

leapYear

? While those names

would be shorter, they miss an important detail: this function simply prints out the

current leap year status. Naming functions meaningfully is part science, part art. The

name is not for JavaScript: it doesn’t care what name you use. The name is for other

people (or you in the future). When you name functions, think carefully about what

another person would understand about the function if all they could see was the

name. Ideally, you want the name to communicate exactly what the function does. On

the flip side, you can be too verbose in the naming of your functions. For example, we

might have named this function

calculateCurrentLeapYearStatusAndPrintToCon

sole

, but the extra information in this longer name has passed the point of diminish‐

ing returns; this is where the art comes in.

Functions as Subroutines That Return a Value

In our previous example,

printLeapYearStatus

is a subroutine in the usual sense of

the word: it just bundles some functionality for convenient reuse, nothing more. This

simple use of functions is one that you won’t find yourself using very often—and even

less so as your approach to programming problems becomes more sophisticated and

184 | Chapter 13: Functions and the Power of Abstract Thinking

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.