Write Small Functions Using Examples

Source: Internet
Author: User

Write Small Functions Using Examples

Keith Braithwaite

WE would like-to-WRITE CODE that is CORRECT, and has evidence on hand the It is CORRECT. It can help with both issues to think about the "size" of a function. The sense of the amount of code, implements a function-although that's interesting-but rather the size of the Mathematical function that is our code manifests.
For example, in the game of Go there are a condition called Atari in which a player's stones may be captured by her Opponen T:a stone with both or more free spaces adjacent to it (called liberties) was not in Atari. It can be tricky to count how many liberties a stone have, but determining Atari are easy if and is known. We might begin by writing a function like this:
boolean atari (int libertycount)
Libertycount < 2
This is larger than it looks. A mathematical function can be understood as a set, some subset of the Cartesian product of the sets, is its domain ( here, int) and range (here, Boolean). If those sets of values were the same size as in Java, then there would be 2l* (integer.max_value+ (–1l*integer.min_value) +1 L) or 8,589,934,592 members in the set Intxboolean. Half of these be members of the subset that's our function, so to provide complete evidence, we function is correct , we would need to check around 4.3x109 examples.
?
??????????????? This was the essence of the claim that tests cannot prove the absence of bugs. Tests can demonstrate the presence of features, though. But still we have the this issue of size.
The problem domain helps us out. The nature of Go means that the number of liberties of a stone are not an int, but exactly one of {1,2,3,4}. So we could alternatively write:
Libertycount = {1,2,3,4}
Boolean Atari (Libertycount Libertycount)
Libertycount = = 1
This are much more tractable:the function computed are now a set with at the most eight members. In fact, four checked examples would constitute evidence of all certainty that the function is correct. This was one reason why it's a good idea to use types closely related to the problem domain to write pro-grams, rather tha n Native types. Using domain-inspired types can often make our functions much smaller. One-to-find out-what those types should are is-find the examples to check in problem domain terms, before writing th e function.

Write Small Functions Using Examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.