AS3/Flex 代码审查工具 – FlexPMD

FlexPMD 是什么?
FlexPMD 是一款免费开源的 AS3/Flex 代码审查工具,由Adobe 发布。

FlexPMD 通过审查 AS3/Flex 目录检测常见的不良用法,例如:

  • 未使用的代码(函数,变量,常量等)
  • 无效代码 (滥用动态过滤器, 厚重的构造器等)
  • 过于复杂的代码 (嵌套循环, 条件过多等)
  • 代码过长 (类, 方法等)
  • 错误使用组件的生命周期 (commitProperties 等)

FlexPMD 使用了PMD部分机能,原理与其类似,也是通过规则定义检测内容。
截止发稿时 FlexPMD 已有 20 个规则组,包括 100 多个规则,当然你也可以自定义规则以扩展当前机能。

你可以通过以下方式调用 FlexPMD:

  • The command line
  • Maven
  • Ant
  • Automator on Mac OS X
  • Eclipse
  • TextMate on Mac OS X
  • FlashDevelop

官方站点
http://opensource.adobe.com/wiki/display/flexpmd/FlexPMD

安装 FlexPMD 插件

  1. 打开 Eclipse
  2. 点击 Help > Install New Software… > Add…
  3. 在打开的对话框中输入以下内容,之后点击 OK 按钮
    Name: http://opensource.adobe.com/svn/opensource/flexpmd/plugin/trunk/flex-pmd-eclipse-plugin-site
  4. 待信息显示后全选
  5. 按安装向导下一步直至完成
  6. 安装成功后你会在以下两处位置看到 FlexPMD 
    选择 Window > Preferences,在打开的对话框中
    右键 Flex 工程,打开的菜单中

下载 FlexPMD 插件所需 jar 包

  1. 从 FlexPMD 下载页下载最新的 All-in-one-bundle 类型压缩包
  2. 解压上诉文件到某一目录(我放在 C:\FlexPMD 下)

生成 FlexPMD 规则配置文件
FlexPMD 有一个在线规则组创建器,你可以通过它了解、屏蔽、追加规则,之后导出这些规则到某一目录(我放在 C:\FlexPMD\rules 下)

配置 FlexPMD 插件

  1. 选择 Window > Preferences > FlexPMD
  2. 在打开的对话框中入力以下内容,其他默认
    FlexPMD command line installation:C:\FlexPMD\flex-pmd-command-line-1.2.jar
    FlexPMD custom ruleset:C:\FlexPMD\rules\pmd_rule.xml
    FlexCPD command line installation:C:\FlexPMD\flex-pmd-cpd-command-line-1.2.jar
  3. 点击 OK

运行 FlexPMD
FlexPMD 已安装完成,使用很简单:

  1. 选择 Flex 工程根或工程下的目录,文件
  2. 右键 FlexPMD > Run FlexPMD

FlexPMD設定规则一览

FlexPMD是什么?
FlexPMD是一种 ActionScript 工具,类似于 JavaPMD。FlexPMD 通过审核 ActionScript 3.0 或 Flex 源目录来提高代码品质。

FlexPMD能检测出什么问题?
它可以检测出品质欠佳的代码编写方法,例如未使用的代码、过度复杂的代码、过长的代码和使用不正确的 Flex 组件生命周期。
如:
   1. 绑定规则:
      绑定类时使用了硬编码的字符串;
      绑定表达式过长。

   2. Cairngorm规则:
      ModelLocator必须是不可绑定的;
      一个Cairngorm事件的名称应在实际事件名称前包含功能区名称。

   3. 使用组件规则:
      不要通过updateDisplayList方法添加或删除可显示的子对象;
      不要明确的调用callLater。

   4. 表达式规则:
      Catch语句不能为空;
      If语句不能为空。

   5. 可维护性规则:
      导入类的数量过多;
      包含了硬编码的布尔值;
      方法为空;
      

   6. 命名规则:
      在变量名字最后使用数字,不利于理解它的含义;
      类名字必须以大写字母开头;
      接口名字应该以字母I开头;
      

   7. 实装规则:
      存在全局绑定类;
      在循环内部定义了变量;
      过多的嵌套if语句;
      

   8. 数量规则:
      方法内容过长;
      类中公有属性和公有方法过多;
      字段定义过多。

   9. Switche使用规则:
      Switch表达式中缺少default表达;
      Switch不应该嵌套使用;
      Switch表达式中不应该含有相同的case;
      

  10. 空定义规则:
      方法的参数未被使用;
      定义的变量未被使用;
      定义的私有方法未被使用;
      ...

具体规则如下:
class type message(英) description(英) message(中) description(中)
BindingUtilsRule Binding BindingUtils class uses hard coded strings, which won't be picked up by the compiler if you rename this attribute. You should probably consider refactoring using events   绑定类时使用了硬编码的字符串,编译器不会识别,如果你重命名此属性,你应该考虑事件的重构。  
TooLongBindingExpressionRule Binding This binding expression is too long ({0} dots maximum, but {1} actually) A Binding expression is executed as soon as one of the bindable attributes changed. If a binding expression contains too many expression, there could be some performance issue. 绑定表达式过长。  
BindableModelLocatorRule Cairngorm A modelLocator must not be Bindable at a class level A bindable ModelLocator could leads to performance issues due to bindings modelLocator必须是类级别且是不可绑定的。 绑定的modelLocator会造成性能问题。
BadCairngormEventNameFormatRule Cairngorm A Cairngorm event name should contain the function area name before the actual event name You would have something like 'productManagement.getProducts' as an event name. 一个Cairngorm事件的名称应在实际事件名称前包含功能区名称。 你可以取如下的名字作为事件名称: 'productManagement.getProducts'
UpdateChildrenNumberInUpdateDisplayListRule Custom Component Flex specific - Do not add or remove displayable children from updateDisplayList UpdateDisplayList is called everytime a child is invalidated. So calling addChild or removeChild in this function could be really CPU consuming 不要通过updateDisplayList方法添加或删除可显示的子对象。 对于一个有效的页面元素子对象,updateDisplayList方法时刻被调用,在该方法中添加或删除子对象会非常消耗CPU。
CallLaterDirectlyRule Custom Component Flex specific - Don't call 'callLater' explicitly If you needed to call 'callLater' explicitly, then you probably did not extend the correct component life cycle. 不要明确的调用callLater。 如果你明确地调用了“callLater,那么你可能没有了正确的组件生命周期。
EmptyCatchStatementRule Empty Statement This catch statement is empty   catch语句是空的。  
EmptyIfStmtRule Empty Statement No statements in this if statement Empty If Statement finds instances where a condition is checked but nothing is done about it. if语句是空的。  
ExcessiveImportRule Maintanability A high number of imports can indicate a high degree of coupling within an object. ({0} maximum but {1} actually) A high number of imports can indicate a high degree of coupling within an object. Rule counts the number of unique imports and reports a violation if the count is above the user defined threshold. 导入(import)对象的数量过高表明对象内的耦合程度高。 导入(import)对象的数量过高表明对象内的耦合程度高。可以设置导入对象数的上限,超过时产生提示。
TrueFalseConditionRule Maintanability This test contains a hard coded boolean value. You could remove it by having '{0}'   包含了硬编码的布尔值。  
NonStaticConstantFieldRule Maintanability A constant field should be static ({0})   constant类型的变量应该设置成静态的。  
UseGenericTypeRule Maintanability Use strongly typed objects instead of *   使用强类型的对象取代禁止使用的类。  
UselessOverridenFunctionRule Maintanability This method is empty. This should be removed ({0})   方法为空。  
EmptyStatementRule Maintanability This statement is empty   表达式为空。  
TooShortVariableRule Naming This variable name is too short ({0} characters minimum, but {1} actually) Detects when a field, local, or parameter has a very short name. 名字太短。 字段,参数等变量名字太短。
PackageCaseRule Naming A package name should be lower case ({0}) Detects when a package definition contains upper case characters. 路径名应该小写。 发现存在大写的路径名。
VariableNameEndingWithNumericRule Naming Using digits at the end of a symbol does not help understanging the meaning of it. ({0})   在变量名字最后使用数字,不利于理解它的含义。  
PropertyHiddenByLocalVariableRule Naming A class property is hidden by this local variable ({0})   类的属性被本地局部变量隐藏。  
IncorrectClassCase Naming A class name must start by a majuscule character   类名字必须以大写字母开头。  
InterfaceNamingRule Naming Interface name should start with I   接口名字应该以字母I开头。  
CyclomaticComplexityRule performance This method is too complex. Maximum complexity is {0}, but its cyclomatic complexity was {1}   方法的圈复杂度(cyclomatic complexity)过高。 注1  
BindableClassRule performance Globally bindable classes can lead to unexpected behaviour especially when you have a setter to a property, and hits the performance of the application   全局绑定类可能会造成预料不到的结果,尤其是该类含有setter的属性。  
AvoidInstanciationInLoopRule performance Instanciating a variable in a loop can be expensive   在循环中定义变量会增加系统开销。  
DeeplyNestedIfRule performance Nested if statements are not a good design   嵌套if语句不是好的设计。  
TooLongFunctionRule Sizing This function is far too long ({0} maximum, but {1} actually) Violations of this rule usually indicate that the method has too much responsibility. Try to reduce the method size by creating helper methods and removing any copy/pasted code. 方法内容过长。 方法过长会包含过多的业务逻辑,尽量从业务逻辑上切分独立的方法。
TooManyPublicRule Sizing Too many public fields or functions detected ({0} maximum, but {1} actually) A large number of public methods and attributes declared in a class can indicate the class may need to be broken up as increased effort will be required to thoroughly test it. 类中公有属性和公有方法过多。 类中公有属性和公有方法过多,会给代码重构,维护,测试等增加负担。
TooManyFieldsRule Sizing Too many field detected ({0} maximum, but {1} actually) Classes that have too many fields could be redesigned to have fewer fields, possibly through some nested object grouping of some of the information. For example, a class with city/state/zipcode fields could instead have one Address field. 字段定义过多。 可以尝试重新设计,和并相同业务逻辑的字段,如: 用address字段取代city/state/zipcode3个字段。
ImportFromSamePackageRule Style Imports from the same package are not necessary   没必要导入相同的包。  
SwitchStatementsShouldHaveDefaultRule Switches A switch statement does not contain a default statement Switch statements should have a default label in order to detect corner cases. Switch表达式中缺少default表达式。 Switch表达式中应该有default表达式来处理例外情况。
NestedSwitchRule Switches Switch must not be nested As a general practice, switch statement should not be used. Prefer using inheritance. It is even harder to read when switch statements are nested. Switch不应该嵌套使用。 嵌套使用的Switch语句会增加阅读的复杂度。
NonBreakableSwitchCaseRule Switches Switch case must include break statement   Switch表达式中应包含break语句。  
IdenticalSwitchCasesRule Switches Two switch cases should not be identical   Switch表达式中不应该含有相同的case。  
UnusedParameterRule Unused This parameter ({0}) of this function is not used   方法的参数未被使用。  
UnusedLocalVariableRule Unused This variable ({0}) is not used   定义的变量未被使用。  
UnusedPrivateMethodRule Unused This private method ({0}) does not seem to be used   定义的私有方法未被使用。  
UnusedFieldRule Unused This private attribute ({0}) does not seem to be used   定义的私有属性未被使用。  
EmptyPrivateMethodRule Unused This private method ({0}) is used but its content is empty   定义的私有方法内容为空。  

注1,圈复杂度(cyclomatic complexity):
一种代码复杂度的衡量标准。在软件测试的概念里,圈复杂度用来衡量一个模块判定结构的复杂程度,数量上表现为独立现行路径条数,即合理的预防错误所需测试的最少路径条数,圈复杂度大说明程序代码可能质量低且难于测试和维护。具体内容,见《软件复杂度概述》。

转:http://blog.sina.com.cn/s/blog_6b6028350100usg2.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值