Write Small Functions Using Examples
来源:互联网 发布:简述什么是移动云计算 编辑:程序博客网 时间:2024/05/29 18:13
We would like to write code that is correct, and have evidence on hand that it is correct. It can help with both issues to think about the "size" of a function. Not in the sense of the amount of code that implements a function — although that is interesting — but rather the size of the mathematical function that our code manifests.
For example, in the game of Go there is a condition called atari in which a player's stones may be captured by their opponent: A stone with two or more free spaces adjacent to it (calledliberties) is not in atari. It can be tricky to count how many liberties a stone has, but determining atari is easy if that 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 that are its domain (here,int
) and range (here, boolean
). If those sets of values were the same size as in Java then there would be2L*(Integer.MAX_VALUE+(-1L*Integer.MIN_VALUE)+1L)
or 8,589,934,592 members in the setint
×boolean
. Half of these are members of the subset that is our function, so to provide complete evidence that our function is correct we would need to check around 4.3×109 examples.
This is 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 this issue of size.
The problem domain helps us out. The nature of Go means that number of liberties of a stone is not anyint
, 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 is much more tractable: The function computed is now a set with at most eight members. In fact, four checked examples would constitute evidence of complete certainty that the function is correct. This is one reason why it's a good idea to use types closely related to the problem domain to write programs, rather than native types. Using domain–inspired types can often make our functions much smaller. One way to find out what those types should be is to find the examples to check in problem domain terms, before writing the function.
by Keith Braithwaite
This work is licensed under a Creative Commons Attribution 3
- Write Small Functions Using Examples
- Write Small Functions Using Examples
- Examples for Creating Oracle Functions
- Examples for using socat
- Using OpenMP - The Examples
- Examples of using minFunc
- Yii Examples of Using CDbCriteria
- Aggregating data using group functions
- Calling API functions using C#
- Using Functions after the Pipeline
- Using ctags to find functions
- Using Multiple JavaScript Onload Functions
- Using ctags to find functions
- Effective C#之Item 31:Prefer Small, Simple Functions
- Three Educative Examples on Using Binders
- Using Keyframes – WebKit CSS Animation Examples
- Examples of Performance Analysis using NS
- Examples in using config spec in ClearCase
- A Model of Learning Objectives[一种学习目标模型]
- 微软推出新款日历服务 功能和界面均有完善
- When Programmers and Testers Collaborate
- Write Code as If You Had to Support It for the Rest of Your Life
- 苹果面临10大威胁:法律官司缠身 创新减少
- Write Small Functions Using Examples
- Spring与Mybatis三种常用整合方法
- Write Tests for People
- 联通高管频频出走:通信业已成鸡肋?
- You Gotta Care about the Code
- grid.getSelectionModel的所有操作【转】
- Your Customers Do not Mean What They Say
- 阚凯力:四核终端发展需软硬件相互促进
- Ubuntu 12.10 软件更新后进不去桌面