主题:  Guidelines for good behavior

jerrowolf

职务:普通成员
等级:1
金币:0.0
发贴:83
注册:2001/1/4 20:28:12
#12001/5/19 20:34:21
感觉对现在的初级程序员很有帮助。

Guidelines for good behavior
摘自MM.com
Behaviors are powerful because they let one developer create scripts that others can easily use and reuse. As a result, a typical behavior is used by multiple developers, some of whom are not Lingo programmers.

The following are some useful guidelines for authoring a set of behaviors that are easy and predictable to use by others:

Save and distribute behaviors in a separate cast library. The behaviors are easier to distribute and the user can't accidentally modify the original behavior.

Provide a ReadMe file that describes all the behaviors. A field cast member at the beginning of the behavior library is a good place for this. Users could get the same information by reading the descriptions of behaviors in the Behavior Inspector and scrolling through behaviors, but scanning one list is easier than reviewing individual entries.

Provide a comment block, describing the overall structure of the behavior and hints about how to modify it, at the start of each script. These comments should contain advice for other Lingo programmers who are editing the behaviors script, not for the end user. Even just a few lines about which handlers are called from outside and how execution flows through the handlers can be a great help to users when they first study a script.

Try to make the behavior's Lingo as easy to understand as possible. This can be a challenge with complicated behaviors that require built-in error-checking. For example, a hypertext behavior may need to verify whether the behavior is attached to a field. Keep in mind that the added scripting makes the behavior's script more difficult to understand, and requires even clearer comments.Sometimes you can get around this by using a separate error checking routine when the behavior begins. This lets you put some of the "challenging" Lingo in a subroutine at the bottom of the script.

Encapsulate a behavior's properties and global variables as much as possible. A behavior can be used in many different environments, so the more contained it is, the better. Global variables can introduce crosstalk problems when the same variable name is unintentionally used by different behaviors. Minimize this possibility by giving global variables very specific names, such as gCurrentPositionVariableNumber23 . If data must be outside a script, consider using a single global list of variables for the entire behavior library and treat each variable as a property in the list. No matter how many behaviors in the library are used, each behavior still deals only with a single global variable.

Use unique IDs when calling handlers outside a behavior. For example, the sendAllSprites command sends a custom message to all current sprites, which could create unexpected results if behaviors from different sources have handlers with the same name. Prevent problems by avoiding simple handler names, which are more likely to be duplicated by other developers. For example, it's much less likely that another developer would name a handler teamDeveloperInit rather than Init



jerrowolf

职务:普通成员
等级:1
金币:0.0
发贴:83
注册:2001/1/4 20:28:12
#22001/5/19 20:35:01
(第一次翻译外文资料,有误之处,多多指教阿。^_^)


如何做一个好behavior的指导
Behaviors 之所以如此强大,就在于它可以由一位开发者创建,而被多人使用.因此,一个有代表性的behavior应该可以被众开发者使用,而其中可能有一些非lingo程序员.

以下就是提供给开发者的一些非常有用的指导,以便于可以开发出可供多人使用的behavior.

1,将behavior保存在独立的cast库中,这样可以很容易地分发给其他使用者,而不会因为意外而破坏原behavior.

2,提供一个readme文件用来描述所有的behavior,建议在这个cast的首位置放置一个field成员来充当readme文件.虽然我们可以在behavior检查器之中查看这个behavior的描述,但是直接查看整体的描述文件要比单独地看某个描述要轻松得多.

3,提供一个注释块,用来描述behavior的整体架构和一些知道用户修改的提示.一般来说,这个注释块放在每一个script之前.此注释块应该包括:a,提供给其他开发者的建议以便于其他开发者对其进行二次开发(此用户不是最终使用者).b,尽量向用户详细地描述此behavior中所调用的外部句柄,以及这些句柄的工作流程,这样对那些刚开始学习script的用户很有帮助.

4,尽量使behavior中的lingo程序通俗易懂.对于复杂一些地behavior,在其中添加内建的错误检查是有难度的.例如,一个超文件的behavior可能需要判断是否本behavior被添加到一个field精灵上.注意:添加的程序越多,这个程序的易读性就越差,因此需要更多简洁明了的注释.有时,你可以在behavior刚开始时使用一个独立的错误检查程序来实现这个功能,这样你只要在每个script之后放一小段相关程序即可.

5,尽可能将一个behavior的属性和全局变量封装在一起,behavior是可能被应用在很多不同的环境当中,所以这个behavior封装的东西越多就越好.如果不同behavior中的全局变量很凑巧的有着相同的名字,那么就很有可能产生干涉而导致错误.解决这个问题的最好办法就是尽可能地给全局变量起很特殊的名字,比如gCurrentPositionVariableNumber23 ,如果数据必须是在script外部的话,就考虑为整个behavior库创建一个单独的全局变量表,表中的属性就是变量,这样无论这个库中用了多少变量,都只需声明这样的一个变量表即可.

6,在调用外部句柄时使用唯一的ID,例如,thesendAllsprites命令给所有的当前精灵传递一个参数.当不同的behavior中存在相同名称的句柄时,可能会导致错误.避免这种错误的方法就是尽量不使用简单的句柄名,这样就不会在不同的开发者之间发生命名重复.例如:如果用teamDeveloperInit来代替Init,就极少会和其他开发者发生命名重复.