ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

【用代数讲计算机科学】函数的设计方法 – 译学馆
未登陆,请登陆后再发表信息
最新评论 (0)
播放视频

【用代数讲计算机科学】函数的设计方法

CS in Algebra: DesignRecipe

现在 我们来关注函数的两个元素:
Up to this point, you focused on two elements of functions:
一 “协议” 它是用来描述
the contract which describes
函数接受什么样的数据类型 及其返回值的范围
what data types your function takes as its domain and returns as its range
二 “定义” 就是函数如何与传递给它的数据交互
and the definition which defines how your function interacts with the data passed to its domain to
同时产生返回值范围的定义
produce its range.
“协议”能帮助我们确保函数拥有正确的结构
The contract helps us to ensure that our function the right structure but it doesn’t help us
但是却不能帮助我们理解
understand what your function should do with
函数是怎样将传入的值变成我们预期值的
the values passed to it to return the expected value.
“示例”是“协议”和“定义”之间缺少的链接
Examples are the missing link between the contract and the definition.
通过写出函数应该如何处理输入的两三个示例
By writing two or more examples of how your function should operate given specific inputs,
你就赋予计算机测试函数“定义”的能力
you give the computer something to test your function definition against
软件开发者通常称之为“单元测试”或者“测试用例”
soft-developer often call this unit test or test cases
写测试用例可以实现两个目的
Writing examples serve two purposes.
首先 它们让你在定义函数之前具体的了解你的函数应该怎样运行
First they help you to see concretely how your function should operate before writing the definition.
第二 它们可以用来测试你的函数 确保函数是按你预期的在工作
Second, they give your function something to test against to make sure it works like you expected
全部的这三部分 “协议” “示例” 和“定义” 组合起来形成流程
All three of these processes taken together, the contract, examples, and the definition,
被叫做 “设计方法” 它们可以让你以一步接一步的方式设计出真正强大的函数
are called the design recipe and they give you a step-by-step approach to designing really great functions.
让我们跟着设计方法 设计一个名为green-triangle(绿色三角形)的函数
Let’s follow the design recipe for a simple function called green-triangle that should
它用数字作为参数并且返回相应大小的绿色三角形
take a number for size and return a solid green triangle with the given size.
首先 我们将写一个协议来描述这个函数通用的结构
First we’ll write a contract that describes the general structure of our function.
它应该有一个名字叫 green-triangle 一个简单的数字作为它的域 以及图片的范围
It should have a name of green-triangle, a single number as its domain and a range of image.
既然我们已有了“约束” 让我们来写一些 “示例”
Now that we have a contract, let’s write some examples.
这些是我们这个函数在处理特定输入时的具体示例
These will be concrete examples of how our function should operate with specific inputs.
如果我们将 15 传入函数 它应该返回一个15px大小的绿色三角形
If we pass the number 15 to green-triangle, it should return a 15 pixel solid green triangle.
如果我们将 100 传入函数 它应该返回一个100px大小的绿色三角形
If we pass the number 100 to green-triangle, it should return 100 pixel solid green triangle.
如果我们比较这两个例子 我们就会看到它们之间的唯一的改变就是
If we compare the two examples, we’ll see that the only thing that changes between them
传入三角函数的尺寸参数大小
is the size parameter passed to the triangle function and those directly correlate to the
它们就是函数的输入参数
input the function was passed.
明白了这个 我们可以就写一个函数定义 用一个变量替换正在改变的元素
With that knowledge, we can write a function definition that replaces that changing element with a variable.
每一个测试用例旁边都有一个按钮
Each example has a test button next to it.
点击 test 将会运行示例中的代码 并且在左边的play区域显示它的输出
Clicking test will run the code in your example and display its output in the play area on the left.
运行测试还将真实返回的值与你定义会返回的值进行比较
Running a test will also compare the value returned by your test against the value returned
告诉你的示例是否符合
by your function definition given the same values and tell you whether or not your example
你的定义
matches your definition.
接下来 你可以用 设计模式 去写出一些函数来实现火箭发射的动画了
In this stage, you’ll use the Design Recipe to write functions that will help animate a rocket blasting off.

发表评论

译制信息
视频概述

本视频主要讲述如何在编程中如何对函数进行设计的一些方法

听录译者

收集自网络

翻译译者

【B】Cod

审核员

知易行难

视频来源

https://www.youtube.com/watch?v=0iybPiGep4w

相关推荐