欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

北航OO(2020)第一单元博客作业

程序员文章站 2022-06-27 21:47:19
北航OO(2020)第一单元博客作业 [TOC] 基于度量的程序结构分析 Homework 1 代码度量 | Method | CONTROL | ev\(G\) | iv\(G\) | LOC | v\(G\) | | | | | | | | | "Expression\.Expression\( ......

北航oo(2020)第一单元博客作业

基于度量的程序结构分析

homework 1

代码度量

method control ev(g) iv(g) loc v(g)
"expression.expression()" 0 1 1 1 1
"expression.expression(string)" 7 1 8 38 10
"expression.computederivative()" 0 1 1 5 1
"expression.tostring()" 1 1 2 7 2
"main.main(string[])" 0 1 1 5 1
"polynomial.polynomial()" 0 1 1 3 1
"polynomial.addterm(biginteger,biginteger)" 2 2 2 11 3
"polynomial.computederivative()" 3 3 2 12 3
"polynomial.termtostring(entry<biginteger, biginteger>)" 6 2 7 34 7
"polynomial.tostring()" 3 2 3 16 4
class csa cso loc ocavg wmc
"constants" 9 12 14 n/a 0
"expression" 1 16 54 3.00 12
"main" 0 13 7 1.00 1
"polynomial" 1 17 79 3.60 18

uml类图

北航OO(2020)第一单元博客作业

优缺点分析

本次作业要求较为简单,因此我的架构较为清晰。方法的最大长度仅为38行,类的最大长度也只有79行,复杂度整体可控。

但是,我将解析输入与expression类的构造方法合并,增加了耦合度。从量化分析中也可看出,这一做法显著增加了expression类及其构造方法的复杂度。此外,我还错误地预判了未来的扩展方向,架构的可扩展性也出现了偏差。

homework 2

代码度量

method control ev(g) iv(g) loc v(g)
"expression.expression()" 0 1 1 3 1
"expression.addterm(term,biginteger)" 2 2 2 11 3
"expression.computederivative()" 4 4 3 14 4
"expression.equals(object)" 2 3 2 11 4
"expression.getrandomterm()" 0 1 1 4 1
"expression.hashcode()" 0 1 1 4 1
"expression.isoneterm()" 0 1 1 3 1
"expression.termentrytostring(entry<term, biginteger>)" 5 2 6 30 6
"expression.tostring()" 4 2 4 19 5
"factortype.factortype(functiontype,expression)" 0 1 1 4 1
"factortype.deriveembedded()" 0 1 1 3 1
"factortype.derivetype()" 6 2 2 14 4
"factortype.equals(object)" 2 3 3 12 5
"factortype.getembeddedexpression()" 0 1 1 3 1
"factortype.getfunctiontype()" 0 1 1 3 1
"factortype.hashcode()" 0 1 1 4 1
"factortype.tostring()" 8 8 2 25 8
"main.main(string[])" 1 1 2 12 2
"pair.pair(t1,t2)" 0 1 1 4 1
"pair.getfirst()" 0 1 1 3 1
"pair.getsecond()" 0 1 1 3 1
"parser.parseexpression(string)" 5 2 3 23 6
"parser.parseterm(string)" 6 4 6 29 8
"term.term()" 0 1 1 3 1
"term.addfactor(factortype,biginteger)" 2 2 2 13 3
"term.computederivative()" 6 1 9 36 9
"term.equals(object)" 2 3 2 11 4
"term.hashcode()" 0 1 1 4 1
"term.tostring()" 3 2 3 17 4
"wrongformatexception.wrongformatexception(string)" 0 1 1 3 1
class csa cso loc ocavg wmc
"constants" 21 12 34 n/a 0
"expression" 1 21 123 2.78 25
"factortype" 2 20 72 2.50 20
"functiontype" 6 27 3 n/a 0
"main" 0 13 14 1.00 1
"pair" 2 15 14 1.00 3
"parser" 0 14 54 6.50 13
"term" 1 18 87 3.17 19
"wrongformatexception" 6 44 5 1.00 1

uml类图

北航OO(2020)第一单元博客作业

优缺点分析

在本次作业中,我按照求导规则进行建模,分加法、乘法以及将来可能出现的嵌套三级,分别由expressionterm以及factortype类进行管理。这些类分别实现了tostring()方法以及求导方法。

本次作业复杂度依旧可控,但我对上次的架构进行了重构,并为下次作业留出了大量扩展空间,这使得程序的结构变得相对复杂很多。然而,由于我对下次作业的扩展方向有了比较正确的预判,我的第三次作业的整体架构几乎没有做出任何改动。这一点可以从uml类图中看出,没有增添新的类,各个类的属性和方法也没有大的变动。此外,从度量中也可以看出,方法的长度依旧可控,最大长度甚至低于第一次作业。

但一些方法,如term.computederivative()方法的复杂度和耦合度都较高,它依赖于factortype类中的每个具体类型执行不同的求导操作,这方面可以进一步改进,如让factortype类执行这些操作等。

homework 3

代码度量

method control ev(g) iv(g) loc v(g)
"expression.expression()" 0 1 1 3 1
"expression.addterm(term,biginteger)" 2 2 2 11 3
"expression.computederivative()" 4 4 3 13 4
"expression.equals(object)" 2 3 2 11 4
"expression.getrandomterm()" 0 1 1 4 1
"expression.hashcode()" 0 1 1 4 1
"expression.isoneterm()" 0 1 1 3 1
"expression.issimplefactor()" 3 4 3 15 4
"expression.iszero()" 0 1 1 3 1
"expression.termentrytostring(entry<term, biginteger>)" 5 2 6 30 6
"expression.tostring()" 4 2 4 22 5
"factortype.factortype(functiontype,expression)" 0 1 1 4 1
"factortype.deriveembedded()" 0 1 1 3 1
"factortype.derivetype()" 6 2 2 14 4
"factortype.equals(object)" 2 3 3 12 5
"factortype.getembeddedexpression()" 0 1 1 3 1
"factortype.getfunctiontype()" 0 1 1 3 1
"factortype.hashcode()" 0 1 1 4 1
"factortype.tostring()" 9 9 5 30 10
"main.main(string[])" 1 1 2 12 2
"pair.pair(t1,t2)" 0 1 1 4 1
"pair.getfirst()" 0 1 1 3 1
"pair.getsecond()" 0 1 1 3 1
"parser.parseexpression(string)" 9 6 6 41 11
"parser.parseterm(string)" 13 8 10 56 14
"parser.preprocess(string)" 7 4 7 25 9
"term.term()" 0 1 1 3 1
"term.addfactor(factortype,biginteger)" 2 2 2 13 3
"term.computederivative()" 7 1 9 38 9
"term.equals(object)" 2 3 2 11 4
"term.getfactors()" 0 1 1 3 1
"term.hashcode()" 0 1 1 4 1
"term.isempty()" 0 1 1 3 1
"term.issimplefactor()" 4 5 2 14 5
"term.tostring()" 5 2 5 25 6
"wrongformatexception.wrongformatexception(string)" 0 1 1 3 1
class csa cso loc ocavg wmc
"constants" 22 12 36 n/a 0
"expression" 1 23 122 2.73 30
"factortype" 2 20 77 2.62 21
"functiontype" 6 27 3 n/a 0
"main" 0 13 14 1.00 1
"pair" 2 15 14 1.00 3
"parser" 0 15 124 10.67 32
"term" 1 21 117 3.22 29
"wrongformatexception" 6 44 5 1.00 1

uml类图

北航OO(2020)第一单元博客作业

复杂度分析

本次作业的复杂度较前两次有了跃升,主要是因为嵌套规则的出现,使得表达式出现了递归组合的可能。然而,得益于上次良好的架构设计,本次我只对parser和constants类做出了比较大的修改。

但是,从度量中可以看出,parser.parseexpression(string)方法和parser.parseterm(string)方法已经变得十分复杂,还可进一步解耦,例如将针对不同因子的判断从中移出,采用其他方法或机制完成。

bug分析

本单元作业中,我由于对wrong format判断不全,在homework 3的强测中痛失两点。不过,由于程序整体架构尚可,在互测中无任何bug被发现。

homework 3中的bug发生在parser类中的parseexpression(string)方法中,由于我对于正则表达式的匹配机制理解不深,忽视了括号中的通配符会将潜在的wrong format匹配其中导致整个表达式匹配成功的问题,最终导致了bug的出现。例如sin(x)+++x+cos(x),就会按照sin(.*)进行匹配,而不会失败。

hack策略分析

由于时间、精力和能力所限,本次的hack策略仅仅局限在提交手动构造的测试用例以及提交曾经在自己的程序上测出问题的测试用例上,也并未仔细阅读他人代码。当然,效果也十分一般,每次平均只能hack成功一两个人。

对象创建模式应用分析

在进行表达式的解析时,可以利用工厂模式构建不同的因子。可以为x、sin、cos、expr分别建立因子类,并用统一的因子接口进行管理,用统一的因子工厂进行构造。这样将可以大大简化parser.parseterm(string)等方法。

设计问题分析与心得体会

本次作业的设计中存在的问题主要有:

  • 对于抽象层次与设计模式的运用不够,主要依赖类的组合和条件判断来解决问题;
  • 一些类与方法之间的耦合度较高,修改困难;
  • 一些方法过于冗长且内聚性不够。

通过本单元的学习和作业练习,我初步了解了面向对象程序设计的一些基本的思想和方法,并迭代开发了一个最终代码在500行量级的java面向对象程序。通过3次的迭代开发,我充分认识到了良好的架构和可扩展性的重要性。在庞大的工程面前,规范化以及高内聚低耦合的设计对于迭代开发的顺利进行至关重要。

此外,同学之间的讨论与交流也对于我的开发有着极大的启发和引领作用。尤其是讨论区的一些帖子,能够帮助我迅速找到架构设计的方向、明确处理的细节问题。